在客户端中提供服务器地址,不连接到服务器

时间:2019-07-05 13:29:19

标签: c linux sockets

我有一个套接字服务器代码和一个客户端代码,它们都在同一台机器上运行。在服务器端,

sockaddr.sin_address.s_address = htonl(INADDRESS_ANY) ;

在客户端

if sockaddr.sin_address.s_address = inet_addr("127.0.0.1") ;

然后套接字连接良好,但如果

sockaddr.sin_address.s_address = inet_addr("192.168.0.118") ;

它没有连接

ifconfig打印:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:22:60:70:a0  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 987  bytes 88629 (88.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 987  bytes 88629 (88.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.118  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::6e8a:bbc8:eca7:7c12  prefixlen 64  scopeid 0x20<link>
        ether e4:02:9b:84:a4:72  txqueuelen 1000  (Ethernet)
        RX packets 20641  bytes 15138450 (15.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12544  bytes 2528692 (2.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

完整代码 服务器,它与INADRESS_ANY绑定。客户端连接到它

struct sockaddr_in servaddr, cli;
  int sockfd, newSockfd, len;
  int err;
  char buf[128];

  len = sizeof(cli);

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if(sockfd ==-1)perror("error in socket opening");

  bzero(&servaddr,sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  //servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  servaddr.sin_port= htons(7800);

  err = bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  if( err != 0)perror("error in socket binding");

  err = listen(sockfd, 5);
  if( err != 0)perror("error in listen");

  newSockfd = accept(sockfd, (struct sockaddr *) &cli, (socklen_t *) &len);
  if(newSockfd ==-1)perror("error in socket accept");

  bzero(buf,sizeof(buf));
  strcpy(buf, "SANDEEP");
  write(newSockfd, buf, sizeof(buf) );

  close(newSockfd);

客户端,它连接到上述服务器

int clientSockfd, newSockfd, err;
 struct sockaddr_in servaddr;//, cli;
 char buf[128];

 clientSockfd = socket(AF_INET, SOCK_STREAM, 0);

 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr = inet_addr/*("127.0.0.1")*/("192.168.0.118");
 servaddr.sin_port = htons(7800);

 err = connect(clientSockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
 if(err != 0) perror("error in connect");

 bzero(buf,sizeof(buf));
 read(clientSockfd, buf, sizeof(buf));
 cout<<buf<<endl;

 close(clientSockfd);

在使用端口7800时我做错了什么吗? 连接错误:连接被拒绝

使用

解决
sockaddr.sin_address.s_address = htonl(INADDRESS_ANY) ;

奇怪,以前我在使用同一个错误

1 个答案:

答案 0 :(得分:2)

服务器正在侦听本地主机地址:

servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

因此,当您尝试通过外部地址进行连接时,由于服务器未在该IP /端口上进行侦听,您将收到连接拒绝错误。

服务器应在INADDR_ANY上进行监听,这使其可以在所有IP上进行监听:

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);