By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Server not accepting client connection.

P: 52
Hi everyone,
I have made a client-server code in Linux. The server is running properly and is sitting at its
Expand|Select|Wrap|Line Numbers
  1. accept()
method. But the client is unable to connect to the server. The Server and Client codes are as follows :


Filename : Server.cpp

Expand|Select|Wrap|Line Numbers
  1. #include<sys/types.h>
  2. #include<sys/socket.h>
  3. #include<netdb.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. //#include<fcntl.h>
  7. #include<string.h>
  8. #include<netinet/in.h>
  9. #include<unistd.h>
  10. #include<sys/stat.h>
  11. #include<errno.h>
  12.  
  13.  
  14. #define SERVERPORT 10000
  15. #define MAXBUFF 1024
  16.  
  17. main()
  18. {
  19.     int socket1,socket2;
  20.     int addrlen;
  21.     struct sockaddr_in ss,cc;
  22.     int retstatus=0;
  23.     int i,j,rcounter;
  24.  
  25.     char *s;
  26.  
  27.     char* bufptr;    
  28.     char buf[MAXBUFF];
  29.     char data[MAXBUFF];
  30.  
  31.     printf("in main of server");
  32.  
  33.     socket1=socket(AF_INET,SOCK_STREAM,0);
  34.     printf("socket no :%d",socket1);
  35.  
  36.     ss.sin_family=AF_INET;    
  37.     ss.sin_addr.s_addr=INADDR_ANY;
  38.     ss.sin_port=htons(SERVERPORT);
  39.  
  40.     if(socket1==-1)
  41.     {
  42.         printf("Error in creating socket");
  43.         exit(1);
  44.     }
  45.     printf("socket created");
  46.  
  47.     printf("\nbind:%d",retstatus);
  48.     retstatus=bind(socket1,(struct sockaddr*)&ss,sizeof(struct sockaddr));
  49.     getchar();
  50.     printf("\nbind:%d",retstatus);
  51.     if(retstatus==-1)
  52.     {
  53.         printf("cannot bind to socket");
  54.         exit(1);
  55.     }
  56.  
  57.     retstatus=listen(socket1,5);
  58.  
  59.     if(retstatus==-1)
  60.     {
  61.         printf("cannot listen on socket");
  62.         exit(1);
  63.     }
  64.  
  65.     //for(;;)
  66.     //{
  67.  
  68.  
  69.         addrlen=sizeof(cc);
  70.         perror(s);
  71.         socket2=accept(socket1,(struct sockaddr*)&cc,(socklen_t*)&addrlen);
  72.         perror(s);
  73.  
  74.     if(socket2==-1)
  75.     {
  76.         printf("cannot accept connection");
  77.         exit(1);
  78.     }
  79. //}
  80.  
  81.     if((rcounter=recv(socket2,data+i,strlen(data),0))>0)
  82.     {
  83.         i+=rcounter;
  84.     }
  85.  
  86.     if(rcounter==-1)
  87.     {
  88.         printf("could not read from socket");
  89.         j=shutdown(socket1,2);
  90.     }
  91.  
  92.     //data[i+1]='/0';
  93.     printf("data is:%s",data);
  94.  
  95.     printf("server shutting down");
  96.     i=shutdown(socket1,2);
  97.  
  98. }
  99.  



Filename : Client.cpp
Expand|Select|Wrap|Line Numbers
  1. #include<sys/types.h>
  2. #include<sys/socket.h>
  3. #include<netinet/in.h>
  4. #include<arpa/inet.h>
  5. #include<netdb.h>
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<sys/stat.h>
  9. #include<string.h>
  10. #include<errno.h>
  11.  
  12. #define SERVERPORT    10000
  13. #define MAXBUFF    1024
  14.  
  15. main(int argc,char* argv[])
  16. {
  17.     int sockd=0,i;
  18.     struct sockaddr_in ss;
  19.     int retstatus=0;
  20.     char buf[MAXBUFF];
  21.     char *s;
  22.  
  23.     if(argc<3)
  24.     {
  25.         printf("something");
  26.         //exit(1);
  27.     }
  28.  
  29.     printf("in main of client");
  30.     sockd=socket(AF_INET,SOCK_STREAM,0);
  31.     printf("\n%d",sockd);
  32.     if(sockd==-1)
  33.     {
  34.         printf("Error in creating socket");
  35.         exit(1);
  36.     }
  37.     printf("Socket created");
  38.     ss.sin_family=AF_INET;    
  39.     ss.sin_addr.s_addr=inet_addr("127.0.0.1");
  40.     ss.sin_port=htons(SERVERPORT);
  41.  
  42.  
  43.     retstatus=connect(sockd,(struct sockaddr*)&ss,sizeof(ss));
  44.     perror(s);
  45.     printf("after connect %d",retstatus);
  46.  
  47.     if(retstatus==-1)
  48.     {
  49.         printf("Error in connecting ");
  50.         exit(1);
  51.     }
  52.  
  53.  
  54.     retstatus=send(sockd,"hello"/*argv[2]*/,strlen("hello"/*argv[2]*/)+1,0);
  55.     if(retstatus==-1)
  56.     {
  57.         printf("could not send data");
  58.         exit(1);
  59.     }
  60.  
  61.     printf("client closing");
  62.     i=shutdown(sockd,2);
  63. }
  64.  

The client on execution prints -1 in this line :
Expand|Select|Wrap|Line Numbers
  1. printf("after connect %d",retstatus);
.

Waiting for reply.
Pawan
Sep 8 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Is this Windows?? If not disregard my comments below.

Read this.

The -1 is only interesting. There are many returns from connect().

The call looks odd:
retstatus=connect(sockd,(struct sockaddr*)&ss,sizeof(ss));
You need to provide the socket which is a SOCKET and not an int.
You need to provide the address of the socket name which is a struct sockaddr*.
You need to provide the the length of the socket name and not the sizeof the name.

I have no idea what struct sockaddr_in is for.
Sep 8 '07 #2

P: 52
Is this Windows?? If not disregard my comments below.

Read this.

The -1 is only interesting. There are many returns from connect().

The call looks odd:


You need to provide the socket which is a SOCKET and not an int.
You need to provide the address of the socket name which is a struct sockaddr*.
You need to provide the the length of the socket name and not the sizeof the name.

I have no idea what struct sockaddr_in is for.

This is in Linux. I have mentioned this at the beginning of the post. And I will try your suggestion and will reply accordingly.
Thanks and Regards
Pawan
Sep 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.