SOCKET s; // Create a SOCKET for listening for
// incoming connection requests.
SOCKET new_socket; // create a socket for accepting incoming connection
uint16 port =18001;
void CreateSocket()
{
int sin_size;
WSADATA wsa;
struct sockaddr_in server, master; // creating a socket address structure: structure contains ip address and port number
printf("Initializing Winsock\n");
if(WSAStartup(MAKEWORD(2,2), &wsa)!=0)
{
printf("Failed Error Code: %d", WSAGetLastError());
//return -1;
}
printf("Initialised\n");
//CREATING a SOCKET
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Could not Create Socket\n");
//return 0;
}
printf("Socket Created\n");
server.sin_addr.s_addr = inet_addr("192.168.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(port);
//Binding between the socket and ip address
if(bind (s, (struct sockaddr *) &server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code: %d", WSAGetLastError());
}
puts("Bind Done");
//Listen to incoming connections
listen(s, 3);
//Accepting the incoming connection
sin_size = sizeof(struct sockaddr_in);
new_socket = accept(s, (struct sockaddr *)&master, &sin_size);
printf("\n I got a connection from (%s , %d)",
inet_ntoa(master.sin_addr),ntohs(master.sin_port));
closesocket(s);
//new_socket = accept(s, NULL, NULL);
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error: %ld\n", WSAGetLastError());
closesocket(new_socket);
WSACleanup();
return 1;
}
else
printf("Client connected.\n");
我正在创建一个套接字来接受来自Master的连接。当我看到wireshark中的流量时,它第一次连接,响应将作为重置发送,然后再发送 主人会发送并重复这样的一段时间。为什么第一个连接命令没有与主站连接? Wireshark输出
1:请求:90 72.170764 192.168.0.1 192.168.0.2 TCP 18001> 63789 [SYN,ACK] Seq = 0 Ack = 1 Win = 8192 Len = 0 MSS = 1460 WS = 8 SACK_PERM = 1
2:响应:91 72.172839 192.168.0.2 192.168.0.1 TCP 63789> 18001 [ACK] Seq = 1 Ack = 1 Win = 65536 Len = 0