首页 新闻 会员 周边

C语言下的Socket编程,Client客户端连接到服务器之后accept函数没反应~

0
悬赏园豆:10 [已解决问题] 解决于 2017-10-05 22:06

分别依次运行下面的两个程序之后,服务器端运行到了35行的new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len)等待客户端连接,此时运行客户端程序,程序输出已经成功连接,但是服务端并未打印运行36行的printf("Get the Client."),并且客户端发送的内容,服务端并未收到,程序如下所示:

服务端程序:(Linux下的运行方式  ./Server 192.168.1.125)

 1 #include <sys/types.h>
 2 #include <sys/socket.h>
 3 #include <netinet/in.h>
 4 #include <netdb.h>
 5 #include <stdio.h>
 6 #include <errno.h>
 7 #include <stdlib.h>
 8 #include <string.h>
 9 #include <unistd.h>
10 
11 int main(int argc, char *argv[])
12 {
13     int fd, new_fd, struct_len, numbytes,i;
14     struct sockaddr_in server_addr;
15     struct sockaddr_in client_addr;
16     struct hostent * he;
17     char buff[100];
18     printf("%s\n",argv[1]);
19 
20     he = gethostbyname(argv[1]);
21     server_addr.sin_family = AF_INET;
22     server_addr.sin_port = htons(4321);
23     server_addr.sin_addr = *((struct in_addr *)he->h_addr);
24     bzero(&(server_addr.sin_zero), 8);
25     struct_len = sizeof(struct sockaddr_in);
26 
27     fd = socket(AF_INET, SOCK_STREAM, 0);
28     while(bind(fd, (struct sockaddr *)&server_addr, struct_len) == -1);
29     printf("Bind Success!\n");
30     while(listen(fd, 10) == -1);
31     printf("Listening....\n");
32     while(1)
33     {
34         printf("Ready for Accept,Waitting...\n");
35         new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
36         printf("Get the Client.");
37         while(1)
38         {
39             while((numbytes = recv(new_fd, buff, strlen(buff), 0)) == -1);
40             if(numbytes != i)
41             {
42                 printf("OK!200~");
43                 printf("%s\n",buff);
44             }
45             i = numbytes;
46         }
47         close(new_fd);
48         exit(0);
49     }
50     close(fd);
51 }

客户端程序:(Linux下的运行方式  ./Client 192.168.1.125)

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

int main(int argc,char *argv[])
{
    int sockfd,numbytes;
    char buf[100];
    struct hostent *he;
    struct sockaddr_in their_addr;
    int i=0;
    printf("break!");
    he = gethostbyname(argv[1]);
    while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1);
    printf("We get the sockfd~\n");
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(4321);
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    bzero(&(their_addr.sin_zero), 8);
    
    while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1);
    printf("Get the Server~Cheers!\n");

    while(1)
    {
        printf("Entersome thing:");
        scanf("%s\n",buf);
        len = strlen(buf);
        while(send(sockfd, buf, len, 0) == -1);
        printf("successfully Send!\n");
    }
    close(sockfd);
    return 0;
}

注:连接应该是成功的,当我把Server的进程关闭的时候,Client发送数据的进程就自动终止了~有没有哪位大侠帮我分析一下,指点一下为什么!谢谢~

小淼博客的主页 小淼博客 | 菜鸟二级 | 园豆:359
提问于:2017-10-03 19:39
< >
分享
最佳答案
-1

35行 最后一个参数,不应该取地址,改成这样new_fd = accept(fd, (struct sockaddr *)&client_addr, struct_len);

收获园豆:10
amwpynnpyu | 菜鸟二级 |园豆:214 | 2017-10-04 15:40

这个accept接口的参数应该是下面这样的:

int accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen)
sockfd:
        socket函数经过listen后套接字描述符
cliaddr:
        客户端套接字接口地址结构
addrlen:
        客户端地址结构长度

改成您回答所述的程序之后提示编译错误~(去掉取地址的部分)

运行还是会出现错误,所以应该不是这个问题~不过还是谢谢你,如果能再帮我看看到底是哪的问题就太感谢啦。。。。

小淼博客 | 园豆:359 (菜鸟二级) | 2017-10-04 21:34
其他回答(1)
0

问题出现在了Server.c的37行到46行代码之间,问题主要是由于recv检测不仅仅要检测是否为负1,而在已经取得数据之后,该表达式一直不是-1,所以while((numbytes = recv(new_fd, buff, strlen(buff), 0)) == -1);这个while似乎没有意义。在这里我们应该改写为如下的程序:

while((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0)
{
  buff[numbytes] = '\0';
  printf("%s\n",buff);
  if(send(new_fd,buff,numbytes,0)<0)
  {
    perror("write");
    return 1;
  }
}

修改之后就正常了~

还需要注意的事client端的数据读取我用的是:scanf("%s\n",buf);这样会导致读入数据之后,通过socket发送过去之后总是会晚一拍,这就会有问题,应该修改成为:scanf("%s",buf);

小淼博客 | 园豆:359 (菜鸟二级) | 2017-10-05 22:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册