LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 01-13-2005, 03:03 AM   #1
msriram_linux
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Rep: Reputation: 0
Unhappy 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!!!!
 
Old 11-09-2005, 12:48 PM   #2
xucaen
Member
 
Registered: Feb 2004
Distribution: fedora 10
Posts: 73

Rep: Reputation: 15
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
 
Old 12-05-2007, 01:10 PM   #3
sm1443
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple clients for echo server msriram_linux Linux - Networking 1 12-05-2007 04:27 PM
echo multiple lines of text twistedpair Linux - Software 9 08-08-2007 07:07 PM
Multiple clients communicating to one socket dragondad Programming 3 09-10-2005 09:22 AM
Mail Server for Multiple Hosting Clients bytebrowser Linux - Networking 2 01-24-2005 04:56 PM
Multiple Clients One Authentication jpbarto Linux - Networking 4 10-02-2003 02:50 PM


All times are GMT -5. The time now is 09:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration