<!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> </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<stdio.h><BR>#include<sys/socket.h><BR>#include<sys/types.h><BR>#include<netinet/in.h><BR>#include<netdb.h><BR>#include<unistd.h><BR>#include<string.h><BR><BR>#define
SERVERPORT 3999<BR>#define CLIENTPORT 4999<BR><BR>int main()<BR>{<BR> int
sockno, scklisten, sockno1;<BR> struct sockaddr_in serv,
sacli;<BR> char buffer[35];<BR> struct servent *svt;<BR> struct
hostent *ht;<BR> int len, size;<BR> fd_set
clientset;<BR><BR> FD_ZERO(&clientset);<BR><BR> ht=gethostbyname("SRI");<BR><BR> serv.sin_addr.s_addr=INADDR_ANY;<BR> serv.sin_family=AF_INET;<BR> serv.sin_port=htons(SERVERPORT);<BR> memset(&(serv.sin_zero),'\0',8);<BR> bcopy((char
*)ht->h_addr,(char
*)&serv.sin_addr,ht->h_length);<BR><BR> sacli.sin_family=AF_INET;<BR> sacli.sin_port=htons(CLIENTPORT);<BR> sacli.sin_addr.s_addr=INADDR_ANY;<BR><BR> sockno=socket(AF_INET,SOCK_STREAM,0);<BR> connect(sockno,(struct
sockaddr *)&serv, sizeof(struct
sockaddr));<BR><BR> strcpy(buffer,"Client has initiated
connection\n");<BR> write(sockno,buffer,strlen(buffer));<BR> close
(sockno);<BR><BR> memset(buffer,'\0',34);<BR> scklisten=socket(AF_INET,SOCK_STREAM,0);<BR> bind(scklisten,
(struct sockaddr *)&sacli,sizeof(struct
sockaddr));<BR> listen(scklisten,15);<BR><BR> FD_SET(0,&clientset);<BR> FD_SET(scklisten,&clientset);<BR><BR> while(1)<BR> {<BR>
select(scklisten+1,&clientset,NULL,NULL,0);<BR><BR>
if(FD_ISSET(scklisten,&clientset))<BR> {<BR>
sockno=accept(scklisten, (struct sockaddr *)&serv,
&size);<BR> read(sockno,buffer,34);<BR>
printf("\n%s\n",buffer);<BR> close(sockno);<BR> }<BR>
if(FD_ISSET(0,&clientset))<BR> {<BR>
read(0,buffer,34);<BR>
sockno=socket(AF_INET,SOCK_STREAM,0);<BR> connect(sockno,(struct
sockaddr *)&serv, sizeof(struct sockaddr));<BR>
write(sockno,buffer,strlen(buffer));<BR> close (sockno);<BR>
}<BR>}<BR>}<BR><BR>*******************serv.c*******************<BR>#include<stdio.h><BR>#include<sys/types.h><BR>#include<sys/socket.h><BR>#include<netinet/in.h><BR>#include<netdb.h><BR>#include<pwd.h><BR><BR>#define
SERVERPORT 3999<BR>#define CLIENTPORT 4999<BR><BR>int main()<BR>{<BR> int
sockfd, newsockfd, wsock;<BR> struct servent *st;<BR> struct hostent
*ht;<BR> struct sockaddr_in serv, cli;<BR> char
localhost[26];<BR> int size;<BR> char
buffer[35] ;<BR> int bufsize;<BR> fd_set
masterset;<BR><BR> FD_ZERO(&masterset);<BR> ht=gethostbyname("SRI");<BR><BR> serv.sin_family=AF_INET;<BR> serv.sin_port=htons(SERVERPORT);<BR> serv.sin_addr.s_addr=INADDR_ANY;<BR> memset(&(serv.sin_zero),'\0',8);<BR><BR> if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)<BR> {<BR>
perror("socket initialisation error");
exit(0);<BR> }<BR><BR> bind(sockfd,(struct sockaddr
*)&serv,sizeof(struct
sockaddr));<BR> listen(sockfd,5);<BR><BR> FD_SET(0,&masterset);<BR> FD_SET(sockfd,&masterset);<BR><BR> while(1)<BR> {<BR>
select(sockfd+1,&masterset,NULL,NULL,0);<BR><BR>
if(FD_ISSET(0,&masterset))<BR> {<BR> /*
CONTROL IS NEVER COMING HERE AS NOTHING IS PRINTED
*/<BR> printf("keyboard getting
ctrl");<BR>
wsock=socket(AF_INET,SOCK_STREAM,0);<BR><BR>
cli.sin_port=htons(CLIENTPORT);<BR>
cli.sin_family=AF_INET;<BR>
cli.sin_addr.s_addr=INADDR_ANY;<BR> bcopy((char
*)ht->h_addr,(char
*)&cli.sin_addr,ht->h_length);<BR>
memset(&(cli.sin_zero),'\0',8);<BR><BR>
connect(wsock, (struct sockaddr *)&cli, sizeof(struct
sockaddr));<BR>
read(0,buffer,34);<BR>
write(wsock,buffer,strlen(buffer));<BR>
close(wsock);<BR> }<BR><BR>
if(FD_ISSET(sockfd,&masterset))<BR>
{<BR> newsockfd=accept(sockfd, (struct sockaddr
*)&cli, &size);<BR> read(newsockfd, buffer,
34);<BR> printf("\nClient:
%s\n",buffer);<BR>
memset(buffer,'\0',34);<BR>
close(newsockfd);<BR>
}<BR> }<BR>}<BR></DIV></FONT></BODY></HTML>