LinuxQuestions.org

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

msriram_linux 01-13-2005 02:03 AM

multiple-echo-clients-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);
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);
}
-------------------------------------------------------------------------------------------------
now only 2 clients are handled.
although the connection is established the problem is that on client gets the echo only after the second has typed in something.
ex:
cli1 : user input: hello
echo output: hello
cli2 : user input: hi
echo outout: /* nothig comes out here*/
until the first client has typed in something again!!
I have triend running the clients on same as well as diff machines...but no use.

Can anyone figureout the BUG!!!! :(

xucaen 11-09-2005 11:48 AM

Re: multiple-echo-clients-server
 
Quote:

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


-------------------------------------------------------------------------------------------------
now only 2 clients are handled.
although the connection is established the problem is that on client gets the echo only after the second has typed in something.
ex:
cli1 : user input: hello
echo output: hello
cli2 : user input: hi
echo outout: /* nothig comes out here*/
until the first client has typed in something again!!
I have triend running the clients on same as well as diff machines...but no use.

Can anyone figureout the BUG!!!! :(


Did you remember to disable blocking?


Jim

sm1443 12-05-2007 12:10 PM

regarding running server and cleint
 
hi

i was trying out your program.
i am new to this networkin.

what is the command to run the server?
and what is the command to run the client once i have compiled it and ready to run?


looking forward to you reply.

sm1443


All times are GMT -5. The time now is 06:43 PM.