Windows中未建立TCP连接-C ++

时间:2019-05-18 16:27:16

标签: c++ visual-studio sockets tcp

使用Windows 10,Visual Studio 2017。

我已经阅读了很多示例,并花了无数时间来解决这个问题。 我希望程序根据某些变量充当客户端和服务器(在两台不同的计算机上复制此块)。

bool IPTunnel::runBlock(void)
{
   int ready = inputSignals[0]->ready(); //int ready2 = inputTCPConnetion[0]->ready();
WSADATA wsaData;

   int iResult;

   iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
   if (iResult != 0) {
       printf("WSAStartup failed: %d\n", iResult);
       return 1;
   }

   //server
   SOCKET sockfd, newsockfd;
   int portno;
   socklen_t clilen;
   char buffer[256];
   struct sockaddr_in serv_addr, cli_addr;
   int n;

   char sendbuf[15] = "this is a test";
   char recvbuf[256];
   int resul;




   // create a socket(int domain, int type, int protocol)
   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0)
       printf("\n ERROR opening socket");

   // clear address structure
   bzero((char *)&serv_addr, sizeof(serv_addr));

   portno = 5500;

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr("127.0.0.1");
   serv_addr.sin_port = htons(portno);

   //if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
   bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
   //   printf("\n ERROR on binding");

   listen(sockfd, 5);

   clilen = sizeof(cli_addr);


   newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
   if (newsockfd < 0)
       printf("ERROR on accept");

   printf("server: got connection from %s port %d\n",inet_ntop(serv_addr.sin_family,
       &serv_addr.sin_addr.s_addr, buffer, clilen), portno);

   //client
   int socketId = socket(PF_INET, SOCK_STREAM, 0);
   struct sockaddr_in serverAddr;
   socklen_t addrSize = sizeof(serverAddr);
   bzero((char*)&serverAddr, sizeof(serverAddr));
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons(8080);
   serverAddr.sin_addr.s_addr = INADDR_ANY;
   connect(socketId, (struct sockaddr*)&serverAddr, addrSize);

   resul = send(socketId, sendbuf, (int)strlen(sendbuf), 0);
   if (resul == SOCKET_ERROR) {
       printf("send failed: %d\n", WSAGetLastError());
       closesocket(newsockfd);
       WSACleanup();
       return 1;
   }
   printf("Bytes Sent: %ld\n", resul);

   do{
       resul = recv(newsockfd, recvbuf, 256, 0);
       if (resul > 0)
           printf("Bytes received: %d\n", resul);
       else if (resul == 0)
           printf("Connection closed\n");
       else
           printf("recv failed: %d\n", WSAGetLastError());
   } while (resul > 0);


   bzero(buffer, 256);


   resul = recv(socketId, buffer, 256, 0);

   fprintf(stdout, "%s %s\n", "Response from server", resul);

   while (true) { Sleep(1000); }
   return 0;
}

这是输出:

  

服务器:从0.0.0.0端口5500连接

     

连接失败:10038

(我检查了此错误10038 https://docs.microsoft.com/en-us/windows/desktop/winsock/windows-sockets-error-codes-2

我在做什么错?预先感谢。

1 个答案:

答案 0 :(得分:1)

resul = send(socketId, sendbuf, (int)strlen(sendbuf), 0);

strlen中使用send是一个坏主意,您确定您的字符串以null终止并且您不希望发送null吗?

int socketId = socket(PF_INET, SOCK_STREAM, 0);

应该是SOCKET,而不是int

connect(socketId, (struct sockaddr*)&serverAddr, addrSize);

您检查返回值了吗?在监听另一个端口的同时连接到8080端口?

考虑改用WSAConnectByName,比填充IPv6兼容的sockaddrs容易得多。

此外,您需要线程。服务器必须调用accept(),这将阻止。 recv()/ send()是阻止函数。