[LCP]sockets problem

Srinath © redindian at nettaxi.com
Tue Jan 29 04:08:44 UTC 2002


Hi all,

I am just learning sockets. I wrote two programs - serv.c and clie.c .
my aim is to run them separately in two virtual terminals and if i type
something in one of them and press enter, it should go to the other one.
I've copied both of them into this email. now , my problem is that if i
type in client, it is reaching the server, but when i type in the server, it
is not reaching the client. In fact, that particular block never gets control.
Please tell me where i am making mistake !!
 
Thanks,
Srinath


*******************clie.c*******************
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>

#define SERVERPORT 3999
#define CLIENTPORT 4999

int main()
{
 int sockno, scklisten, sockno1;
 struct sockaddr_in serv, sacli;
 char buffer[35];
 struct servent *svt;
 struct hostent *ht;
 int len, size;
 fd_set clientset;

 FD_ZERO(&clientset);

 ht=gethostbyname("SRI");

 serv.sin_addr.s_addr=INADDR_ANY;
 serv.sin_family=AF_INET;
 serv.sin_port=htons(SERVERPORT);
 memset(&(serv.sin_zero),'\0',8);
 bcopy((char *)ht->h_addr,(char *)&serv.sin_addr,ht->h_length);

 sacli.sin_family=AF_INET;
 sacli.sin_port=htons(CLIENTPORT);
 sacli.sin_addr.s_addr=INADDR_ANY;

 sockno=socket(AF_INET,SOCK_STREAM,0);
 connect(sockno,(struct sockaddr *)&serv, sizeof(struct sockaddr));

 strcpy(buffer,"Client has initiated connection\n");
 write(sockno,buffer,strlen(buffer));
 close (sockno);

 memset(buffer,'\0',34);
 scklisten=socket(AF_INET,SOCK_STREAM,0);
 bind(scklisten, (struct sockaddr *)&sacli,sizeof(struct sockaddr));
 listen(scklisten,15);

 FD_SET(0,&clientset);
 FD_SET(scklisten,&clientset);

 while(1)
 {
  select(scklisten+1,&clientset,NULL,NULL,0);

  if(FD_ISSET(scklisten,&clientset))
  {
   sockno=accept(scklisten, (struct sockaddr *)&serv, &size);
   read(sockno,buffer,34);
   printf("\n%s\n",buffer);
   close(sockno);
  }
  if(FD_ISSET(0,&clientset))
  {
   read(0,buffer,34);
   sockno=socket(AF_INET,SOCK_STREAM,0);
   connect(sockno,(struct sockaddr *)&serv, sizeof(struct sockaddr));
   write(sockno,buffer,strlen(buffer));
   close (sockno);
  }
}
}

*******************serv.c*******************
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<pwd.h>

#define SERVERPORT 3999
#define CLIENTPORT 4999

int main()
{
 int sockfd, newsockfd, wsock;
 struct servent *st;
 struct hostent *ht;
 struct sockaddr_in serv, cli;
 char   localhost[26];
 int    size;
 char   buffer[35] ;
 int    bufsize;
 fd_set  masterset;

 FD_ZERO(&masterset);
 ht=gethostbyname("SRI");

 serv.sin_family=AF_INET;
 serv.sin_port=htons(SERVERPORT);
 serv.sin_addr.s_addr=INADDR_ANY;
 memset(&(serv.sin_zero),'\0',8);

 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
 {
  perror("socket initialisation error"); exit(0);
 }

 bind(sockfd,(struct sockaddr *)&serv,sizeof(struct sockaddr));
 listen(sockfd,5);

 FD_SET(0,&masterset);
 FD_SET(sockfd,&masterset);

 while(1)
 {
   select(sockfd+1,&masterset,NULL,NULL,0);

   if(FD_ISSET(0,&masterset))
   {
    /* CONTROL IS NEVER COMING HERE AS NOTHING IS PRINTED */
     printf("keyboard getting ctrl");
     wsock=socket(AF_INET,SOCK_STREAM,0);

     cli.sin_port=htons(CLIENTPORT);
     cli.sin_family=AF_INET;
     cli.sin_addr.s_addr=INADDR_ANY;
     bcopy((char *)ht->h_addr,(char *)&cli.sin_addr,ht->h_length);
     memset(&(cli.sin_zero),'\0',8);

     connect(wsock, (struct sockaddr *)&cli, sizeof(struct sockaddr));
     read(0,buffer,34);
     write(wsock,buffer,strlen(buffer));
     close(wsock);
   }

   if(FD_ISSET(sockfd,&masterset))
   {
     newsockfd=accept(sockfd, (struct sockaddr *)&cli, &size);
     read(newsockfd, buffer, 34);
     printf("\nClient: %s\n",buffer);
     memset(buffer,'\0',34);
     close(newsockfd);
   }
 }
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20020129/bea779c6/attachment.htm 


More information about the linuxCprogramming mailing list