在套接字编程中,只有第一条消息从客户端到达服务器,但是从客户端发送的其他消息未到达服务器

时间:2017-02-20 09:33:56

标签: sockets

我在客户端和服务器中都放了一个while循环来发送和接收消息,但只有第一条消息到达服务器。在客户端发送的消息未到达服务器之后。

 server side:

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

    int main(int argc, char *argv[])
    {

    int socks,portno;
    struct sockaddr_in server,client;
    int newsock,n;
    char buffer[5000];
    int cli_len;

    if(argc < 2)
    {
    perror("\ninsufficient inputs");
    exit(1);
    }

    if((socks = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {

    perror("\nsocket creation error");
    exit(1);
    }
    printf("\nsocket successfully created");
    bzero((char *) &server, sizeof(server));
    server.sin_family = AF_INET;
    portno = atoi(argv[1]);
    server.sin_port = htons(portno);
    server.sin_addr.s_addr = INADDR_ANY;
    if((bind(socks,(struct sockaddr *) &server,sizeof(server))) > 0)
    {                                    //socket binding
    perror("\nsocket binding failed");
    exit(1);
    }
    printf("\nsocket binding successfull");



    if(listen(socks,5) < 0)
    {
    perror("\nerror in listening");
    exit(1);
    }

    printf("\nlisten succesfull");

    while(1)
    {
    cli_len = sizeof client;
    newsock = accept(socks, (struct sockaddr *) &client,&cli_len);
    bzero(buffer,5000);
    if( (n = recv(newsock,buffer,5000,0)) < 0) //recieving the message
    {
    perror("\nreading from socket failed");
    exit(1);
    }
    printf("read message:%s",buffer);

    //bzero(buffer,5000);
    //buffer[5000] = "got ur message";
    if(( n = write(newsock,"got ur message",14)) < 0)
    {
    perror("\nwrite failed");
    exit(1);
    }
    }
    return 0;
    }

    Client:

在客户端,它要求发送下一条消息,但第一条消息后的所有消息都没有到达服务器。

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


    int main(int argc, char *argv[])
    {

    struct sockaddr_in server;
    struct hostent *host;
    char buffer[5000];
    int portno,socky,n;

    if(argc < 3)
    {
    perror("insufficient inputs");  //checking the inputs
    exit(1);
    }

    if((socky = socket(AF_INET,SOCK_STREAM,0)) < 0)   
    { 
    perror("\nerror in socket creation");       //socket creation
    exit(1);
    }

    printf("\nsocket successfully created");

    server.sin_family = AF_INET;
    portno = atoi(argv[2]);
    server.sin_port = htons(portno);
    host = gethostbyname(argv[1]);
    if(host == NULL)
    {
    perror("\nerror in getting host address");
    exit(1);
    }

    memcpy (&server.sin_addr.s_addr, host->h_addr,host->h_length);

    if((connect(socky,&server,sizeof(server)))<0) //connecting to server
    {
    perror("\nerror in connection");
    exit(1);
    }
    while(1)
    {

    bzero(buffer,5000);
    printf("\nenter the message");
    fgets(buffer,5000,stdin);
    printf("u have entered the messge:%s",buffer);
    if((n = write(socky,buffer,strlen(buffer))) < 0)   //writing to socket
    {
    perror("\nerror in writing the message");
    exit(1);
    }

    bzero(buffer,1000);
    if (n = (read(socky,buffer,1000)) < 0) //reading from socket
    {
    perror("\nerror in reading from socket");
    exit(1);
    }

    printf("\nmessage recieved:%s",buffer);
    }
    return 0;
    }

0 个答案:

没有答案