使用c connect()拒绝连接?

时间:2016-11-22 19:17:14

标签: c linux sockets tcp connect

我遇到了一些问题,使用c connect()打开与本地服务器的TCP连接(我在使用VirtualBox的虚拟机上使用Ubuntu 16.04)。当我启动服务器时,一切正常,它开始监听S_PORT。当客户端尝试使用connect()打开TCP连接时,程序停止,并且errno设置为111(连接被拒绝)。有关更多信息,请发布以下代码:

客户代码:

int main(int argc,char* argv[]){

   int sock_cli;
   int err;
   unsigned short port = 0;

   struct in_addr addr;
   struct sockaddr_in addr_serv;
   struct hostent* ptr = NULL;
   messaggio* m = (messaggio*)malloc(sizeof(messaggio));

   /* Opening socket */
   sock_cli = socket(PF_INET,SOCK_STREAM,0);

   if(sock_cli == -1){ 
        printf(" (client) problem with socket() function \n");
        return -1;
   }
   else printf(" (client) socket open successfully, fd: %d \n",sock_cli);   

   #ifdef _ARG
   printf(" (client) argc : %d \n",argc);
   for(int i=0;i<argc;i++) printf( " (client) argv[%d] =  %s \n",i,argv[i]);
   #endif

   /* Set address/port */
   if(argc == 3){ 
        if(!strcmp(argv[1],"localhost")){
             ptr = gethostbyname("localhost");
        }  
        else inet_aton(argv[1],&addr);
        port = atoi(argv[2]);

        #ifdef _DEBUG
        printf(" (client) port %d \n",port);
        #endif
   }
   else{ 
        printf(" (client) few arguments, args = [host] [port] \n");
        return -1;  
   }

   /* Set addr_serv */
   addr_serv.sin_family = AF_INET;
   addr_serv.sin_port = htons(port);
   memcpy(&addr_serv.sin_addr.s_addr,ptr->h_addr,ptr->h_length);
   memset(addr_serv.sin_zero,'\0',sizeof(addr_serv.sin_zero));  

   err = connect(sock_cli,(struct sockaddr*)&addr_serv,sizeof(addr_serv));

   if(err == -1){ 
        printf(" (client) connect() failed %d \n",errno);
        return -1;
   }
   else printf(" (client) connect() open successfully \n");

   m->type = 1;
   m->a = rand() % 10 + 1;
   m->b = rand() % 10 + 1;

   if(send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0) > 0){
        printf(" (client) couple send (%d,%d) \n",m->a,m->b);
   }

   recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0); 

   #ifdef _DEBUG
   printf(" (client) msg received, product: %d \n",m->prod);
   #endif

   close(sock_cli);
   free(m);
   return 0;
}

这是服务器的代码:

#define S_PORT 3550
#define N_CLI 5

int main(){

     int sock_serv; // server socket
     int sock_cli; // client socket
     unsigned int sin_size; 
     int err;

     struct sockaddr_in addr_serv;
     struct sockaddr_in addr_cli;
     messaggio* m = (messaggio*)malloc(sizeof(messaggio));

     /* Opening socket */
     sock_serv = socket(PF_INET,SOCK_STREAM,0);

     if(sock_serv == -1) {
          printf(" (server) problem with socket() function \n");
          return -1;
     }
     else printf(" (server) socket open successfully, fd: %d \n",sock_serv);

     /* Set addr_serv */
     addr_serv.sin_family = AF_INET;
     addr_serv.sin_port = htons(S_PORT);
     addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);
     memset(addr_serv.sin_zero,'\0',sizeof(addr_serv.sin_zero));

     /* Bind */
     err = bind(sock_serv,(struct sockaddr*)&addr_serv,sizeof(addr_serv));

     if(err == -1){ 
          printf(" (server) bind() failed \n");
          return -1;
     }
     else printf(" (server) bind() ok, address 'localhost', port %d \n",addr_serv.sin_port);    

     /* Listen */
     err = listen(sock_serv,N_CLI);

     if(err == -1) {
          printf(" (server) listen() failed \n");
          return -1;
     }
     else printf(" (server) listening \n"); 

     /* Accept */
     sin_size = sizeof(addr_cli);
     sock_cli = accept(sock_serv,(struct sockaddr*)&addr_cli,&sin_size);

     if(sock_cli == -1){ 
          printf(" (server) no connection open \n");
          return -1;
     }  
     else printf(" (server) connection open with client, sock_cli: %d \n",sock_cli);    

     recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0); // bloccante

     m->prod = (m->a)*(m->b);

     send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0);

     close(sock_cli);

     printf(" (server) connection close \n");

     close(sock_serv);
     free(m);
     return 0;
}

这是Output

的屏幕

1 个答案:

答案 0 :(得分:0)

端口错误。您绑定到端口3550(0xdde),但随后打印非字节交换版本56845(0xde0d)。然后,您尝试连接到客户端中的那个。

当您在服务器中打印时,您需要切换回本机字节顺序(ntohs(addr_serv.sin_port))或者只需将“3550”传递给客户端,因为这是您在服务器中绑定的内容。