Search code examples
c++visual-studiosocketstcp

TCP connection not established in windows - c++


Using windows 10, visual studio 2017.

I've been reading a lot of examples and spent countless hours on this and can't get it to work. I want a program to act as client and server depending on some variables (to duplicate this block on two different computers).

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;
}

This is the output:

server: got connection from 0.0.0.0 port 5500

connect failed: 10038

(i checked this error 10038 https://learn.microsoft.com/en-us/windows/desktop/winsock/windows-sockets-error-codes-2)

What am i doing wrong? Thanks in advance.


Solution

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

    Using strlen in send is bad idea, are you sure that your string is null terminated and you don't want nulls to be sent?

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

    Should be SOCKET, not int.

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

    did you check return value? Connecting to port 8080 while listening to another port?

    Consider using WSAConnectByName instead, much easier than filling sockaddrs, IPv6 compatible.

    Also, you need threads. A server must call accept() and this would block. recv()/send() are blocking functions.