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

时间:2015-03-12 11:27:05

标签: c sockets unix network-programming

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

中开始无限循环

Server.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
}

请尽快告诉我解决方案。

2 个答案:

答案 0 :(得分:1)

主要问题,即无限循环,只是因为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. 如果客户端关闭连接而未发送&#34; END&#34;和 接收循环中将发生无限循环。你需要检测 连接闭包,由recv()返回0表示。
  3. 在第64行的服务器中不必要地调用了
  4. listen()
  5. 以上列表可能并非详尽无遗。

答案 1 :(得分:0)

您在server.c中的读/写调用中使用了错误的套接字。

现在,所有读/写调用都失败,errno表示套接字未连接。

您应该使用sid1连接的套接字,而不是sid

您还应该检查返回值,以便检测和处理错误。