客户端无法绑定到指定的端口,连接到指定的服务器 [英] Client unable to bind to the given port and connect to the given server

查看:369
本文介绍了客户端无法绑定到指定的端口,连接到指定的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的客户端绑定到特定的端口,然后连接到它的服务器。的问题是,如果我绑定到给定端口成功但无法连接到服务器。如果我不尝试绑定到该端口我能够连接到服务器。下面是我的程序:

I need my client to bind to a particular port and then connect to its server. The problem is if I bind to the given port successfully but unable to connect to the server. If I don't attempt to bind to the port I am able to connect to the server. The below is my program:

int main()
{

    int sock, bytes_recieved;
    char send_data[1024],recv_data[1024];
    struct hostent *host;
    struct sockaddr_in server_addr,client_addr;

    host = gethostbyname("127.0.0.1");

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(4000);
    client_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(client_addr.sin_zero),8);

    //host = gethostbyname("192.168.61.67");
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(2404);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero),8);

    if (bind(sock, (struct sockaddr *)&client_addr, sizeof(struct sockaddr))== -1)
    {
        perror("Unable to bind");
        exit(1);
    }
    else
    {
        fprintf(stderr,"Client bound to 4000\n");
    }

    if (connect(sock, (struct sockaddr *)&server_addr,
                sizeof(struct sockaddr)) == -1)
    {
        perror("Connect");
        exit(1);
    }
    else
    {
        printf("Connected to the server\n");
    }

    while(1)
    {

      bytes_recieved=recv(sock,recv_data,1024,0);
      recv_data[bytes_recieved] = '\0';

      if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
      {
       close(sock);
       break;
      }

      else
       printf("\nRecieved data = %s " , recv_data);

       printf("\nSEND (q or Q to quit) : ");
       gets(send_data);
        if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
       send(sock,send_data,strlen(send_data), 0);

      else
      {
       send(sock,send_data,strlen(send_data), 0);
       close(sock);
       break;
      }

    }
return 0;
}

在这个程序中,如果我使用本地主机客户端和服务器我能够绑定到锅里,并连接到服务器成功。如果我给一个不同的IP到服务器出现问题。请建议在code的任何变化来实现我的目标。

In this program, If I use localhost for both the client and the server I am able to bind to the pot and connect to the server successfully. The problem appears if I give a different IP to the server. Kindly suggest any changes to the code to achieve my goal

推荐答案

您的问题是,要绑定到地址 127.0.0.1 。这个地址可以的只有的用于连接到其他回环地址。

Your problem is that you are binding to the address 127.0.0.1. This address can only be used to connect to other loopback addresses.

相反,你应该绑定地址的 sin_addr 成员设置为 INADDR_ANY

Instead, you should be setting the sin_addr member of the bound address to INADDR_ANY:

client_addr.sin_addr.s_addr = INADDR_ANY;

这篇关于客户端无法绑定到指定的端口,连接到指定的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆