<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2314.1000" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hi all,<BR><BR>I am just learning sockets. I wrote 
two programs - serv.c and clie.c .<BR>my aim is to run them separately in two 
virtual terminals and if i type<BR>something in one of them and press enter, it 
should go to the other one.<BR>I've copied both of them into this email. now , 
my problem is that if i<BR>type in client, it is reaching the server, but when i 
type in the server, it<BR>is not reaching the client. In fact, that particular 
block never gets control.</FONT></DIV>
<DIV><FONT face=Arial size=2>Please tell me where i am making mistake 
!!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Thanks,</FONT></DIV>
<DIV><FONT face=Arial size=2>Srinath<BR><BR></DIV></FONT>
<DIV><FONT face=Arial 
size=2>*******************clie.c*******************<BR>#include&lt;stdio.h&gt;<BR>#include&lt;sys/socket.h&gt;<BR>#include&lt;sys/types.h&gt;<BR>#include&lt;netinet/in.h&gt;<BR>#include&lt;netdb.h&gt;<BR>#include&lt;unistd.h&gt;<BR>#include&lt;string.h&gt;<BR><BR>#define 
SERVERPORT 3999<BR>#define CLIENTPORT 4999<BR><BR>int main()<BR>{<BR>&nbsp;int 
sockno, scklisten, sockno1;<BR>&nbsp;struct sockaddr_in serv, 
sacli;<BR>&nbsp;char buffer[35];<BR>&nbsp;struct servent *svt;<BR>&nbsp;struct 
hostent *ht;<BR>&nbsp;int len, size;<BR>&nbsp;fd_set 
clientset;<BR><BR>&nbsp;FD_ZERO(&amp;clientset);<BR><BR>&nbsp;ht=gethostbyname("SRI");<BR><BR>&nbsp;serv.sin_addr.s_addr=INADDR_ANY;<BR>&nbsp;serv.sin_family=AF_INET;<BR>&nbsp;serv.sin_port=htons(SERVERPORT);<BR>&nbsp;memset(&amp;(serv.sin_zero),'\0',8);<BR>&nbsp;bcopy((char 
*)ht-&gt;h_addr,(char 
*)&amp;serv.sin_addr,ht-&gt;h_length);<BR><BR>&nbsp;sacli.sin_family=AF_INET;<BR>&nbsp;sacli.sin_port=htons(CLIENTPORT);<BR>&nbsp;sacli.sin_addr.s_addr=INADDR_ANY;<BR><BR>&nbsp;sockno=socket(AF_INET,SOCK_STREAM,0);<BR>&nbsp;connect(sockno,(struct 
sockaddr *)&amp;serv, sizeof(struct 
sockaddr));<BR><BR>&nbsp;strcpy(buffer,"Client has initiated 
connection\n");<BR>&nbsp;write(sockno,buffer,strlen(buffer));<BR>&nbsp;close 
(sockno);<BR><BR>&nbsp;memset(buffer,'\0',34);<BR>&nbsp;scklisten=socket(AF_INET,SOCK_STREAM,0);<BR>&nbsp;bind(scklisten, 
(struct sockaddr *)&amp;sacli,sizeof(struct 
sockaddr));<BR>&nbsp;listen(scklisten,15);<BR><BR>&nbsp;FD_SET(0,&amp;clientset);<BR>&nbsp;FD_SET(scklisten,&amp;clientset);<BR><BR>&nbsp;while(1)<BR>&nbsp;{<BR>&nbsp; 
select(scklisten+1,&amp;clientset,NULL,NULL,0);<BR><BR>&nbsp; 
if(FD_ISSET(scklisten,&amp;clientset))<BR>&nbsp; {<BR>&nbsp;&nbsp; 
sockno=accept(scklisten, (struct sockaddr *)&amp;serv, 
&amp;size);<BR>&nbsp;&nbsp; read(sockno,buffer,34);<BR>&nbsp;&nbsp; 
printf("\n%s\n",buffer);<BR>&nbsp;&nbsp; close(sockno);<BR>&nbsp; }<BR>&nbsp; 
if(FD_ISSET(0,&amp;clientset))<BR>&nbsp; {<BR>&nbsp;&nbsp; 
read(0,buffer,34);<BR>&nbsp;&nbsp; 
sockno=socket(AF_INET,SOCK_STREAM,0);<BR>&nbsp;&nbsp; connect(sockno,(struct 
sockaddr *)&amp;serv, sizeof(struct sockaddr));<BR>&nbsp;&nbsp; 
write(sockno,buffer,strlen(buffer));<BR>&nbsp;&nbsp; close (sockno);<BR>&nbsp; 
}<BR>}<BR>}<BR><BR>*******************serv.c*******************<BR>#include&lt;stdio.h&gt;<BR>#include&lt;sys/types.h&gt;<BR>#include&lt;sys/socket.h&gt;<BR>#include&lt;netinet/in.h&gt;<BR>#include&lt;netdb.h&gt;<BR>#include&lt;pwd.h&gt;<BR><BR>#define 
SERVERPORT 3999<BR>#define CLIENTPORT 4999<BR><BR>int main()<BR>{<BR>&nbsp;int 
sockfd, newsockfd, wsock;<BR>&nbsp;struct servent *st;<BR>&nbsp;struct hostent 
*ht;<BR>&nbsp;struct sockaddr_in serv, cli;<BR>&nbsp;char&nbsp;&nbsp; 
localhost[26];<BR>&nbsp;int&nbsp;&nbsp;&nbsp; size;<BR>&nbsp;char&nbsp;&nbsp; 
buffer[35] ;<BR>&nbsp;int&nbsp;&nbsp;&nbsp; bufsize;<BR>&nbsp;fd_set&nbsp; 
masterset;<BR><BR>&nbsp;FD_ZERO(&amp;masterset);<BR>&nbsp;ht=gethostbyname("SRI");<BR><BR>&nbsp;serv.sin_family=AF_INET;<BR>&nbsp;serv.sin_port=htons(SERVERPORT);<BR>&nbsp;serv.sin_addr.s_addr=INADDR_ANY;<BR>&nbsp;memset(&amp;(serv.sin_zero),'\0',8);<BR><BR>&nbsp;if((sockfd=socket(AF_INET,SOCK_STREAM,0))&lt;0)<BR>&nbsp;{<BR>&nbsp; 
perror("socket initialisation error"); 
exit(0);<BR>&nbsp;}<BR><BR>&nbsp;bind(sockfd,(struct sockaddr 
*)&amp;serv,sizeof(struct 
sockaddr));<BR>&nbsp;listen(sockfd,5);<BR><BR>&nbsp;FD_SET(0,&amp;masterset);<BR>&nbsp;FD_SET(sockfd,&amp;masterset);<BR><BR>&nbsp;while(1)<BR>&nbsp;{<BR>&nbsp;&nbsp; 
select(sockfd+1,&amp;masterset,NULL,NULL,0);<BR><BR>&nbsp;&nbsp; 
if(FD_ISSET(0,&amp;masterset))<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; /* 
CONTROL IS NEVER COMING HERE AS NOTHING IS PRINTED 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp; printf("keyboard getting 
ctrl");<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
wsock=socket(AF_INET,SOCK_STREAM,0);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; 
cli.sin_port=htons(CLIENTPORT);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
cli.sin_family=AF_INET;<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
cli.sin_addr.s_addr=INADDR_ANY;<BR>&nbsp;&nbsp;&nbsp;&nbsp; bcopy((char 
*)ht-&gt;h_addr,(char 
*)&amp;cli.sin_addr,ht-&gt;h_length);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
memset(&amp;(cli.sin_zero),'\0',8);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; 
connect(wsock, (struct sockaddr *)&amp;cli, sizeof(struct 
sockaddr));<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
read(0,buffer,34);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
write(wsock,buffer,strlen(buffer));<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
close(wsock);<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; 
if(FD_ISSET(sockfd,&amp;masterset))<BR>&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp; newsockfd=accept(sockfd, (struct sockaddr 
*)&amp;cli, &amp;size);<BR>&nbsp;&nbsp;&nbsp;&nbsp; read(newsockfd, buffer, 
34);<BR>&nbsp;&nbsp;&nbsp;&nbsp; printf("\nClient: 
%s\n",buffer);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
memset(buffer,'\0',34);<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
close(newsockfd);<BR>&nbsp;&nbsp; 
}<BR>&nbsp;}<BR>}<BR></DIV></FONT></BODY></HTML>