WSAECONNRESET 10054 Connection reset by peer. An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET. 远程计算机强行关闭了连接. 是不是那个机子装了什么防火墙东西,不让你连接?
1 0 0 5 4—W S A E C O N N R E S E T 连接被对方重设。一个已经建立的连接被远程主机强行关闭。若远程主机上的进程异常 中止运行(由于内存冲突或硬件故障),或者针对套接字执行了一次强行关闭,便会产生这样 的错误。针对强行关闭的情况,可用S O _ L I N G E R套接字选项和s e t s o c k o p t来配置一个套接字
#include <winsock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib")void main(int argc, char **argv) { WSADATA wsaData; SOCKET s; SOCKADDR_IN ServerAddr; int Port = 5150; int Ret; if (argc <= 1) { printf("USAGE: tcpclient <Server IP address>.\n"); return; } // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) { // NOTE: Since Winsock failed to load we cannot use WSAGetLastError // to determine the error code as is normally done when a Winsock // API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret); return; }
// Create a new socket to make a client connection.
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { printf("socket failed with error %d\n", WSAGetLastError()); WSACleanup(); return; }
// Setup a SOCKADDR_IN structure that will be used to connect // to a listening server on port 5150. For demonstration // purposes, we required the user to supply an IP address // on the command line and we filled this field in with the // data from the user. ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); ServerAddr.sin_addr.s_addr = inet_addr(argv[1]); // Make a connection to the server with socket s. printf("We are trying to connect to %s:%d...\n", inet_ntoa(ServerAddr.sin_addr), htons(ServerAddr.sin_port)); if (connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) { printf("connect failed with error %d\n", WSAGetLastError()); closesocket(s); WSACleanup(); return; } printf("Our connection succeeded.\n");
// At this point you can start sending or receiving data on // the socket s. We will just send a hello message to the server. printf("We will now try to send a hello message.\n"); if ((Ret = send(s, "Hello", 5, 0)) == SOCKET_ERROR) { printf("send failed with error %d\n", WSAGetLastError()); closesocket(s); WSACleanup(); return; } printf("We successfully sent %d byte(s).\n", Ret); // When you are finished sending and receiving data on socket s, // you should close the socket. printf("We are closing the connection.\n"); closesocket(s); // When your application is finished handling the connection, call // WSACleanup. WSACleanup(); }
10054
Connection reset by peer.
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET. 远程计算机强行关闭了连接.
是不是那个机子装了什么防火墙东西,不让你连接?
server: client:
Sleep(10000); send();
recv() close(sock);
知道怎么回事了吧
什么叫非法数据,我没有拒绝任何数据,所有的都是可以接收的!
连接被对方重设。一个已经建立的连接被远程主机强行关闭。若远程主机上的进程异常
中止运行(由于内存冲突或硬件故障),或者针对套接字执行了一次强行关闭,便会产生这样
的错误。针对强行关闭的情况,可用S O _ L I N G E R套接字选项和s e t s o c k o p t来配置一个套接字
基本情况就是客户端已经将数据全部写入了缓冲区,在调用SEND后根据返回值判断可以关闭连接,但是服务器端由于各种原因接收缓慢,导致数据还未接收完毕,再调用recv时就会有这个问题了。
另外我想问一下:如果ti = select(NULL,&fdRead,&fdWrite,NULL,&mWaitTime);返回-1,但是
FD_ISSET(ts,&fdRead)却为可读应该怎么处理?
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")void main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET s;
SOCKADDR_IN ServerAddr;
int Port = 5150;
int Ret; if (argc <= 1)
{
printf("USAGE: tcpclient <Server IP address>.\n");
return;
} // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to make a client connection.
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}
// Setup a SOCKADDR_IN structure that will be used to connect
// to a listening server on port 5150. For demonstration
// purposes, we required the user to supply an IP address
// on the command line and we filled this field in with the
// data from the user. ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr(argv[1]); // Make a connection to the server with socket s. printf("We are trying to connect to %s:%d...\n",
inet_ntoa(ServerAddr.sin_addr), htons(ServerAddr.sin_port)); if (connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("connect failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("Our connection succeeded.\n");
// At this point you can start sending or receiving data on
// the socket s. We will just send a hello message to the server. printf("We will now try to send a hello message.\n"); if ((Ret = send(s, "Hello", 5, 0)) == SOCKET_ERROR)
{
printf("send failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("We successfully sent %d byte(s).\n", Ret); // When you are finished sending and receiving data on socket s,
// you should close the socket. printf("We are closing the connection.\n"); closesocket(s); // When your application is finished handling the connection, call
// WSACleanup. WSACleanup();
}