LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   multiple clients for echo server (https://www.linuxquestions.org/questions/linux-networking-3/multiple-clients-for-echo-server-277246/)

msriram_linux 01-13-2005 04:17 AM

multiple clients for echo server
 
Hi all!
I am new to n/w programming...
I was trying out with an echo server which can handle multiple clients simultaneously.

-------------------------Here is the code for server----------------------------

#include<sys/select.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<time.h>

int main(int argc, char **argv)
{
int connfd0,connfd1;
typedef struct sockaddr SA;

int listenfd,n0=0,n1=0,maxfd,rdfd0=0,rdfd1=0,wfd0=0,wfd1=0;
fd_set rset,wset,rset1;
socklen_t len;
struct sockaddr_in servaddr,clinaddr0,clinaddr1;
char buff0[81],buff1[81];
struct timeval tout;


tout.tv_sec=0;
tout.tv_usec=50;

FD_ZERO(&rset); FD_ZERO(&rset1);
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr.sin_zero,8);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port=htons(13);
servaddr.sin_port=htons(13);
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
listen(listenfd,5);

len=sizeof(clinaddr0);
connfd0=accept(listenfd,(SA*)&clinaddr0,&len);
FD_SET(connfd0,&rset);

len=sizeof(clinaddr1);
connfd1=accept(listenfd,(SA*)&clinaddr1,&len);
FD_SET(connfd1,&rset);

close(listenfd);

if(connfd0 > connfd1) { maxfd= connfd0+1; } else { maxfd=connfd1+1; }
rset1=rset;
for( ; ; )
{
n0=0;n1=0;
rset=rset1;
select(maxfd,&rset,NULL,NULL,&tout);

if(FD_ISSET(connfd1,&rset))
{
rdfd1 =1;
}
if(FD_ISSET(connfd0,&rset))
{
rdfd0 =1;
}
}
if(rdfd0)
{
n0=read(connfd0,buff0,80);
write(connfd0,buff0,n0);
}
if(rdfd1)
{
n1=read(connfd1,buff1,80);
wriite(connfd1,buff1,n1);
}

tout.tv_sec=0;
tout.tv_usec=50;

}
exit(0);
}
------------------------------------------here is the code for client --------------------------------------------

#include<netinet/in.h>
#include<stdio.h>
#include<errno.h>
#include<sys/select.h>
#include<sys/types.h>
#include<unistd.h>
typedef struct sockaddr SA;

int main(int argc, char **argv)
{
int sockfd,connfd,n1=0,n2=0,maxfd,srd=0,inrd=0;
fd_set rset,rset1;
char recvline[81];//buffer for read socket
char buff[81];//buffer for read std input
struct sockaddr_in servaddr;

struct timeval tout;
tout.tv_sec =0;
tout.tv_usec=50;
FD_ZERO(&rset);FD_ZERO(&rset1);

if(argc !=2) exit(1);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) exit(1);

bzero(&servaddr,32);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);

if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0) exit(1);

if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)
{
printf("%d",errno);exit(1);
}

FD_SET(0,&rset); FD_SET(sockfd,&rset);
rset1=rset; //save rset into rset1
if(sockfd>0) { maxfd =sockfd+1;} else { maxfd = (0+1); }

for( ; ; )
{
rset=rset1;
select(maxfd,&rset,NULL,NULL,&tout);

if(FD_ISSET(0,&rset))
{
inrd=1; //if input is ready for reading then
//read-input flag is set to 1
}
if(FD_ISSET(sockfd,&rset))
{
srd=1; //if input is ready for reading then
//read-input flag is set to 1
}

if(srd)
{
n1=read(sockfd,recvline,80);//read socket and write to stdout
write(1,recvline,n1);
}

if(inrd)
{
n2=read(0,buff,80);//read stdin and write to socket
write(sockfd,buff,n2);
}

tout.tv_sec =0;
tout.tv_usec=50;

}
exit(0);
}
-------------------------------------------------------------------------------------------------
Server accepts only two clients.
while the server is running and accepts the conn from two clients,client 1 and client 2 :
client1 : user i/p : hello
ech o/p: hello
client2 : user i/p : hi
echo o/p : hi
client2: user i/p : good morning
echo o/p: /* nothing comes out now */

as said above : here when client2 initiates the reponse there is nothing echoed until the
client1 input anything....
I have struggled for 3 days to debug...but failed..
!! could anyone please help me debug it...:cry:

sm1443 12-05-2007 03:27 PM

starting client and server
 
what is the instruction to start server and cleint?


thanks


All times are GMT -5. The time now is 08:30 AM.