473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Server not accepting client connection.

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
2 2062
weaknessforcats
9,208 Expert Mod 8TB
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
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

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

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
9
by: User | last post by:
Hi, I tried to find the information over the internet but didn't find any answers. I'm looking for a server side code example of winsock accepting many clients. I know that in VB.NET it is...
3
by: ferbar | last post by:
Hello all, This may sound pretty basic stuff.. but I'm working on a socket example whose client seems to work fine, but the server doesn't send to the client the expected result. The problem is...
3
by: Mochuelo | last post by:
Hi, I'm working with an asynchronous TCP server. I have a few questions that may seem silly, but I can't get to understand all this. Let's say that BeginAccept will cause the AcceptCallback...
5
by: Yossarian | last post by:
I have a handheld running CE .NET 4.2 and I am using c# with framework 1.1 to develop a solution for syncing data that is on the handheld with the local pc. Our handheld cradles only support...
25
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
1
by: cherman | last post by:
I don't know if anybody here worked with gSOAP software, but I do hope so. I've made some simple server app using gSOAP example. Everythig worked fine until I switched it into ssl mode. Using it...
0
by: Sid Price | last post by:
I have a VB.NET 2005 client and server applications using TCP/IP; the applications are working as expected with the server accepting a client connection and creating a thread to handle...
4
by: Veeraraghavan | last post by:
Hi All, I am developing a client server communication using system.net.socket and I am finding it very difficult to get a solution for this. I started with single port communication with single...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.