Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple client with single server program in c (TCP)

I wrote server program using select(). In this program I can able to connect multiple clients. But this server is talking only to the latest client. I cannot find the problem. Please help me out. For client program, I'm using telnet command (telnet 192.168.100.143 8181).

SERVER.C

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


#define SERVER_IP       "192.168.100.143"
#define CLIENT_IP       "192.168.100.5"
#define PORT            8181

#define BUF_LEN         256

fd_set read_fd;
int32_t server_id, client_id, fdmax, ret, client_no;
struct sockaddr_in server, client;

int main()
{
        int32_t opt=1;
        socklen_t len;
        char IP[INET_ADDRSTRLEN],buffer[BUF_LEN];

        if((server_id=socket(AF_INET,SOCK_STREAM,0))==-1) {
        perror("Unable to create Server ocket");
        exit(-1);
    }

    if( setsockopt(server_id, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 ) {
                perror("setsockopt error...");
                exit(-1);
        }

        memset((char *) &server,0, sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
        inet_pton(AF_INET, SERVER_IP , &server.sin_addr);
    bzero(&server.sin_zero,8);

    if((bind(server_id,(struct sockaddr*)&server,sizeof(struct sockaddr_in)))==-1) {
        perror("Unable to bind the server address...");
        exit(-1);
    }

    listen(server_id,5);
    printf("\nWaiting for connection\n");

        FD_ZERO(&read_fd);
    FD_SET(server_id,&read_fd);

    fdmax=server_id;

        while(1){

                printf("%d\n",__LINE__);
                printf("fdmax = %d \n",fdmax);
                if((select(fdmax+1,&read_fd,NULL,NULL,NULL)) <0) {
                        perror("Select\n");
                        return -1;
                }
                printf("%d\n",__LINE__);

                if(FD_ISSET(server_id,&read_fd)) {
                        if((client_id=accept(server_id,(struct sockaddr*)&client,&len))==-1) {
                        perror("Cannot accept the client...");
                        exit(-1);
                        }
                        printf("New client  %d connectd  \n",client_id);
                        FD_SET(client_id, &read_fd);
                        fdmax = client_id;
                } else {
                        for(client_no = server_id+1; client_no <=fdmax; client_no++) {
                                if(FD_ISSET(client_no, &read_fd)) {
                                        printf("%d\n",__LINE__);
                                        ret = recv(client_no, buffer, BUF_LEN, 0);
                                        if(ret == 0) {
                                                printf("client  %d disconnectd  \n",client_no);
                                                FD_CLR(client_no, &read_fd);
                                                close(client_no);
                                                break;
                                        } else {
                                                printf("Received from client %d = %s \n",client_no,buffer);
                                        }
                                }
                        }
                }
        }
        close(client_id);
        close(server_id);
        return 0;
}
like image 978
Rajavarman SL Avatar asked Sep 16 '26 12:09

Rajavarman SL


1 Answers

You need to call FD_SET on all file descriptors(including listener fd) in every iteration of while(1).

In your code the server_id is not set inside while(1) loop. And as such, new socket connections and data on them are not notified once the first client was connected and sent in data.'

while(1)
{
   FD_SET(server_id,&read_fd); //This should work.
...
}
like image 123
Prabhu Avatar answered Sep 19 '26 01:09

Prabhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!