2
votes

I am creating a simple echo server that listens to a SOCK_STREAM and continues to accept infinite connections. In the function int listen(int sockfd, int backlog) . I have set backlog to 1 so does this not mean a maximum of 1 client can wait in the queue ?

Consider the following case that I have encountered.

Server has started and accepted connection from client 1

Now client 2 attempts to connect and is connected

Now client 3 attempts to connect and is connected

Now Client 2 sends "hello2" and waits for reply

Now Client 3 sends "hello3" and waits for reply

Now Client 1 sends "hello1" and gets reply "hello1" from server

Now Client 2 gets reply "hello2" from server

Then Client 3 gets reply "hello3" from the server

How is this possible? Should Client 2 and Client 3 not get an error while connecting as server is already connected to Client 1 and the max queue size is 1 ?

 Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <string.h>

#define max_request 4
char buffer[50];

void clear()
{
int i;
for(i=0;i<50;i++)
buffer[i]=NULL;
}

int main()
{
    int status;

    struct sockaddr_in addrport;
    addrport.sin_family = AF_INET;
    addrport.sin_port = htons(5100);
    addrport.sin_addr.s_addr = htonl(INADDR_ANY);

        int sockid = socket(PF_INET, SOCK_STREAM, 0);

    if(bind(sockid, (struct sockaddr *) &addrport, sizeof(addrport))== -1) 
    {
        printf("\nBinding Failed\n");
        return(0);
    }


    int listen_status=listen(sockid,1);
    if(listen_status==-1)
    printf("\nListening Error\n");
    else    
    printf("\nWaiting for connections...\n");


    while(1)
    {
        struct sockaddr_in clientaddr;
        int addlen=sizeof(struct sockaddr_in);

        int new_client_socket=accept(sockid,(struct sockaddr *)&clientaddr,&addlen);

        char *client_ip;
        client_ip=inet_ntoa(clientaddr.sin_addr);
        printf("\n Connection from %s \n",client_ip);


        status=recv(new_client_socket,buffer,50,0);
        if(status!=-1)
        status=send(new_client_socket,buffer,50,0);     
        clear(); // clears the variable buffer

    }



    close(sockid);
}

Client :

int main()
{
    int s,k;char buff[50],c[50];

    struct sockaddr_in serverport;

    serverport.sin_family = AF_INET;
    serverport.sin_port = htons(5100);
    serverport.sin_addr.s_addr = inet_addr("0.0.0.0");

    int sockid=socket(PF_INET,SOCK_STREAM,0);   

    int connect_status=connect(sockid,(struct sockaddr *) &serverport,sizeof(serverport));
    if(connect_status<0)
    {
        printf("\nConnection Error\n");
        return(1);
    }
    printf("\nConnected\n");

    gets(buff); // message to be sent to server

     s=send(sockid,buff,50,0);
        k=recv(sockid,c,50,0);

    printf("\nServer : %s \n ",c);
    close(sockid);
}
1
Soon, you will have other problems with lack of null-terminators when calling printf, failure to thread off or non-block the client<>server comms at the server, failing to correctly account for the values returned by recv() and send(), other stuff.Martin James

1 Answers

1
votes

Should Client 2 and Client 3 not get an error while connecting as server is already connected to Client 1 and the max queue size is 1 ?

No, no error.

'max QUEUE size is 1' - if the accept succeeds, then the queue is empty and another client can connect.