1
votes

I have the following code...

Client Code

#include<stdio.h>
#include<string.h>
//#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{   
        struct sockaddr_in client,server;
    int s,n;
        char b1[100],b2[100];
        s=socket(AF_INET,SOCK_DGRAM,0);
        server.sin_family=AF_INET;
        server.sin_port=2000;
        server.sin_addr.s_addr=inet_addr("127.0.0.1");
        printf("\nClient ready....\n");
        n=sizeof(server);
        while(1)
        {
            printf("\nClient:");
            gets(b2);
            sendto(s,b2,sizeof(b2),0,(struct sockaddr *)&server,n);
            if(strcmp(b2,"end")==0)
                break;
            recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
            printf("\nServer:%s",b1);
        }

}

Server code.....

#include<stdio.h>
#include<string.h>
//#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
    struct sockaddr_in cliaddr,myaddr;
    int servsock,clisock;
    char b1[100],b2[100];
    servsock=socket(AF_INET,SOCK_DGRAM,0);
    myaddr.sin_family=AF_INET;
myaddr.sin_port=2000;
myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(servsock,(struct sockaddr *)&myaddr,sizeof(myaddr));
printf("\nServer ready,waiting for client....\n");
clisock=sizeof(cliaddr);
while(1)
{
    recvfrom(servsock,b1,sizeof(b1),0,(struct sockaddr *) &cliaddr,&clisock);
    if(!(strcmp(b1,"end")))
        break;
    printf("\nCliaddr:%s",b1);
    printf("\nmyaddr:");
    gets(b2);
    sendto(servsock,b2,sizeof(b2),0,(struct sockaddr *) &cliaddr,clisock);

}
}

these codes are for a single client UDP chat....What changes do i need to make to make it a multiple client UDP chat .... I would like more then one client to send messages to the server..

2

2 Answers

0
votes

You will need to use fork() to create a concurrent server. Hope this link helps. http://www.cems.uwe.ac.uk/~ngunton/worksheets/npws3.pdf

0
votes

Alternately you can use an api such as select (which monitors many socket descriptors and returns the one on which data is available) and create a concurrent server.

Concurrent server with select

Although the above example is written with tcp, it can be tailored to use udp.

Other options might include using poll, epoll on Linux 2.6 or threads too apart from fork.

forking creates a separate new process. This might not be feasible when processing many clients i.e. say a web server handles tens of thousands of requests. Now it cannot create that many threads or processes per client. Plus there is an over head of doing this upon receiving requests.

A practical solution is to use a mix of both i.e. create a large set of socket descriptors being monitored by a pool of worker processes or threads. These pick up descriptors for work as soon as any one of them is free.

C10k describes many of possible strategies for writing high performance server applications with pro and cons of each.