c - 套接字编程 TCP 。服务器编程不断循环

标签 c sockets unix network-programming

以下是server.c程序。当我将客户端连接到服务器时,服务器开始在接收消息部分无限循环

服务器.c

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>

int main()
{
    int sid,sid1,bin,lis,n,p,port, pid;
    struct sockaddr_in sin,c;
    socklen_t len;
    char buffer[40]="";
    char buffer1[20]="";
    char ip[30]="192.168.65.241";
    // printf("\nEnter the IP Address :");
    // scanf("%s",ip);
    printf("\nEnter the Port no.:");
    scanf("%d",&port);
    sid=socket(AF_INET,SOCK_STREAM,0);//END POINT CONN,
    if(sid<0)
    {
            perror("Socket : ");
            exit(0);
    }
    printf("\nSocket created successfully...");
    sin.sin_family=AF_INET;
    sin.sin_port=htons(port);
    inet_aton(ip,&sin.sin_addr);//SERVER ADDRESS
    bin=bind(sid,(struct sockaddr*)&sin,sizeof(sin));//BIND ADDRESS WITH SOCKET
    if(bin<0)
    {
            perror("\nBind : ");
            exit(0);
    }
    printf("\nBinding has been done successfully...");
    lis=listen(sid,10);//LISTEN TO THE CLIENT
    if(lis<0)
    {
            perror("\nListen : ");
            exit(0);
    }
    len=sizeof(c);

     while(1){

        printf("A Client is connected to the server.....");
        sid1=accept(sid,(struct sockaddr*)&sin,&len);//ACCEPT CONN.FROM CLIENT
        if(sid1<1)
        {
            perror("\nAccept : ");
            exit(0);
        }
        pid=fork();//CREATE PARENT AND CHILD PROCESS

        if(pid<0){

            perror("ERROR on fork");
            exit(1);
        }
        if(pid==0){

            lis=listen(sid,10);
            printf("\nEnter message...(Type END to terminate):\n");
            scanf("%s",buffer);
            while(strcmp(buffer,"END")!=0)
            {
                send(sid,buffer,30,0);//SEND TO SERVER
                scanf("%s",buffer);
            }
            send(sid,"END",5,0);
            printf("\nTerminating the server...\n");
            exit(0);
        }
        else{

            n=recv(sid,buffer1,30,0);//RECEIVE FROM CLIENT
            buffer[n]='\0';
            n=strcmp(buffer1,"END");
            while(strcmp(buffer1,"END")!=0)
            {
                **printf("\nClient: %s\n",buffer1);** // Infinite Looping
                n=recv(sid,buffer1,30,0);
            }
            printf("\nA client has been disconnected from the server...\n");    
            exit(0);
        }
                            close(sid); //CLOSE CONNECTIONS
                                                                        close(sid1);    //CLOSE CONNECTIONS


     }
    shutdown(sid,2);    //SHUTDOWN READ AND WRITE
    close(sid); //CLOSE CONNECTIONS
}

Client.c

客户端程序没有问题。添加了代码供您引用。

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
int main()
{
    int sid,bi,con,n,p,port;
    char buffer[50],ip[30];
    sid=socket(AF_INET,SOCK_STREAM,0);//END POINT CONN.
    if(sid==-1)
    {
            perror("\nSocket :");
        exit(0);
    }
    printf("A Socket created successfully....");
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    printf("\nEnter the IP Address :");
    scanf("%s",ip);
    printf("\nEnter the Port no. :");
    scanf("%d",&port);
    sin.sin_port = htons(port);
    inet_aton(ip,&sin.sin_addr);
    con=connect(sid,(struct sockaddr*)&sin,sizeof(sin));//CONNECT TO SERVER
    if(con == -1)
    {
        perror("Connection : ");
            exit(0);
    }
    printf("\nConnection is successfull to the server(%s)....\n",ip);
    p=fork();
    if(p)
        {
            printf("\nEnter message...(Type END to terminate):\n");
            scanf("%s",buffer);
            while(strcmp(buffer,"END")!=0)
            {
                    send(sid,buffer,30,0);//SEND TO THE SERVER
                    scanf("%s",buffer);
            }
            send(sid,"END",4,0);
        printf("\nTerminating the client...\n");
        exit(0);
        }
        else
        {
                n=recv(sid,buffer,30,0);//RECV.FROM SERVER
        printf("%d",n);
        buffer[n+1]='\0';
            while(strcmp(buffer,"END")!=0)
            {
                    printf("\nServer: %s\n",buffer);
                    n=recv(sid,buffer,30,0);
            }
            send(sid,"END",4,0);
        exit(0);
        }
    printf("\nClient is terminated...\n");
    shutdown(sid,2);//SHUTDOWN READ AND WRITE
    close(sid); //CLOSE CONNECTIONS
}

请尽快让我知道解决方案。

最佳答案

主要问题,即无限循环,只是因为 recv() 是在服务器的主套接字 (sid) 上调用的,而不是 返回的套接字>accept() - sid1。更改此:

n=recv(sid,buffer1,30,0);//RECEIVE FROM CLIENT

n=recv(sid1,buffer1,30,0);//RECEIVE FROM CLIENT

n=recv(sid,buffer1,30,0);

n=recv(sid1,buffer1,30,0);

这样你就可以开展业务了。

服务器代码还存在其他问题:

  1. recv() 被告知将最多 30 个字节读入 buffer1, 然而,buffer1 的大小只有 20 个字节 - 你有一个缓冲区 那里已经超支了。
  2. 如果客户端关闭连接而不发送“END”并且 接收循环中会出现无限循环。你需要检测 连接关闭,由 recv() 返回 0 表示。
  3. listen() 在第 64 行的服务器中被不必要地调用。

上面的列表可能并不详尽。

关于c - 套接字编程 TCP 。服务器编程不断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008526/

相关文章:

c - 指针数组和括号内的指针

c - 在 C 中声明二维数组只知道一个的长度

c++ - 在 VS 2015 中编译和链接第三方库

java - 帮助通过 nat 与套接字通信

c - 关闭 ncurses 窗口后返回正常终端 I/O

linux - Linux中所有用户的上次登录日期

c - 指针用++递增

php - Websockets,并识别独特的同行[PHP]

c - 如何检查套接字是否在c中使用,以在单个套接字上同时执行多个写入

linux - 在 Windows 中利用 Unix 命令的强大功能