Question

My C client can connect to my server on local, but when I try connecting from a remote machine it does not connect, it reaches as "error connect failed".

I am able to telnet into the server from remote machines.

     char* user_IP;
            printf("Please enter an IP Address\n");
731
732            scanf(" %s", user_IP);
733            /*while (isdigit(IP) && IP>0)        // has some isues           
734            {                                                                
735                printf("Please enter an IP Address\n");                      
736                                                                             
737                scanf(" %s", &IP);                                           
738                }*/
739            play_num=2;
740            IP=atoi(user_IP);
741
742
743            if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
744            {
745                printf("\n Error : Could not create socket \n");
746                return 1;
747            }
748
749            memset(&serv_addr,0,sizeof(serv_addr));
750            serv_addr.sin_family = AF_INET;
751            serv_addr.sin_port = htons(5001);
752
753            if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_a\
   ddr)) < 0)
754            {
755                printf("\n Error : Connect Failed \n");
756                return 1;
757            }
Était-ce utile?

La solution

There are a few problems.

  1. You have not used IP address anywhere in the connection.

  2. You don't store anything when you use scanf(). Use something like:

    Char user_IP[16];
    
    Scanf("%s", user_IP);
    
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top