Search code examples
csocketstcp

can't connect TCP socket between Ubuntu and Windows


I made a server socket in Ubuntu and a client socket in Windows but the connection is refused on the server. It says: "Connection refused". I'm using Ubuntu 16.04 LTS and Windows 10. Both sockets are written in C. I compile the server socket code in Eclipse (ran on Ubuntu) using "Linux GCC". I compile the client socket code in Eclipse (ran on Windows) using "Cygwin GCC" and Gnu Make Builder. Server's IP address 192.168.1.21 and client's IP address 192.168.1.20

Please check the below code snippets:

Server Socket:

//to run this program in the terminal I enter one argument,
//the port to listen to, 34550.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     char* register_emergency = "register emergency";
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
         error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]); //usually 34550
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0)
          error("ERROR on binding");
     listen(sockfd, 5);
     clilen = sizeof(cli_addr);
     while(1){
         printf("now waiting for a new connection...\n"); //this appears on server's screen to me.
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     //on the "accept" function the control flow waits and holds for a connection 
     //As far as it goes for the server, it keeps waiting here and doesn't react... As if it doesn't see any incoming connection
     if (newsockfd < 0) {
         error("ERROR on accept"); 
     } else {
         bzero(buffer,256);
         n = read( newsockfd, buffer, 255);
         if (n < 0) error("ERROR reading from socket");
         printf("Here is the message I got from Mr. Client: '%s'\n",buffer);

         struct sockaddr_in *s = (struct sockaddr_in *) &cli_addr;
         char ipstr[INET6_ADDRSTRLEN];
         inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeofipstr); 
         printf("The IP of the client: %s\n", ipstr);
         printf("The port of the sender: %d\n",ntohs(s->sin_port));
         if (strcmp( register_emergency, buffer) == 0){

         }

         n = write(newsockfd, "Hey Mr. Client, this is server talking...", 18);
         if (n < 0) error("ERROR writing to socket");

         }
     }
}

Now for the client ran on Windows:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}

//In the command prompt, I enter 2 arguments 
//192.168.1.21 34550

int main(int argc, char *argv[])
{
    int sockfd, portno, n;

    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    serv_addr.sin_port = htons(portno);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("192.168.1.21");          
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR connecting"); //program flow always enters here... then perror returns "Connection refused"
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0)
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0)
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    return 0;
}

I tried another port but it didn't work. I tried to reverse the client and server on both machines but again the connection was refused.

I checked the packets in Wireshark, the generated packet from server has always Seck=1, Ack=1, but Rst=1, so it's not a router that's causing the problem. And firewall is off on both machines.


Solution

  • You're forgetting this line in your client code

    portno = atoi(argv[2]);