Hi everyone,
I have made a client-server code in Linux. The server is running properly and is sitting at its
method. But the client is unable to connect to the server. The Server and Client codes are as follows :
Filename : Server.cpp -
#include<sys/types.h>
-
#include<sys/socket.h>
-
#include<netdb.h>
-
#include<stdio.h>
-
#include<stdlib.h>
-
//#include<fcntl.h>
-
#include<string.h>
-
#include<netinet/in.h>
-
#include<unistd.h>
-
#include<sys/stat.h>
-
#include<errno.h>
-
-
-
#define SERVERPORT 10000
-
#define MAXBUFF 1024
-
-
main()
-
{
-
int socket1,socket2;
-
int addrlen;
-
struct sockaddr_in ss,cc;
-
int retstatus=0;
-
int i,j,rcounter;
-
-
char *s;
-
-
char* bufptr;
-
char buf[MAXBUFF];
-
char data[MAXBUFF];
-
-
printf("in main of server");
-
-
socket1=socket(AF_INET,SOCK_STREAM,0);
-
printf("socket no :%d",socket1);
-
-
ss.sin_family=AF_INET;
-
ss.sin_addr.s_addr=INADDR_ANY;
-
ss.sin_port=htons(SERVERPORT);
-
-
if(socket1==-1)
-
{
-
printf("Error in creating socket");
-
exit(1);
-
}
-
printf("socket created");
-
-
printf("\nbind:%d",retstatus);
-
retstatus=bind(socket1,(struct sockaddr*)&ss,sizeof(struct sockaddr));
-
getchar();
-
printf("\nbind:%d",retstatus);
-
if(retstatus==-1)
-
{
-
printf("cannot bind to socket");
-
exit(1);
-
}
-
-
retstatus=listen(socket1,5);
-
-
if(retstatus==-1)
-
{
-
printf("cannot listen on socket");
-
exit(1);
-
}
-
-
//for(;;)
-
//{
-
-
-
addrlen=sizeof(cc);
-
perror(s);
-
socket2=accept(socket1,(struct sockaddr*)&cc,(socklen_t*)&addrlen);
-
perror(s);
-
-
if(socket2==-1)
-
{
-
printf("cannot accept connection");
-
exit(1);
-
}
-
//}
-
-
if((rcounter=recv(socket2,data+i,strlen(data),0))>0)
-
{
-
i+=rcounter;
-
}
-
-
if(rcounter==-1)
-
{
-
printf("could not read from socket");
-
j=shutdown(socket1,2);
-
}
-
-
//data[i+1]='/0';
-
printf("data is:%s",data);
-
-
printf("server shutting down");
-
i=shutdown(socket1,2);
-
-
}
-
Filename : Client.cpp -
#include<sys/types.h>
-
#include<sys/socket.h>
-
#include<netinet/in.h>
-
#include<arpa/inet.h>
-
#include<netdb.h>
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<sys/stat.h>
-
#include<string.h>
-
#include<errno.h>
-
-
#define SERVERPORT 10000
-
#define MAXBUFF 1024
-
-
main(int argc,char* argv[])
-
{
-
int sockd=0,i;
-
struct sockaddr_in ss;
-
int retstatus=0;
-
char buf[MAXBUFF];
-
char *s;
-
-
if(argc<3)
-
{
-
printf("something");
-
//exit(1);
-
}
-
-
printf("in main of client");
-
sockd=socket(AF_INET,SOCK_STREAM,0);
-
printf("\n%d",sockd);
-
if(sockd==-1)
-
{
-
printf("Error in creating socket");
-
exit(1);
-
}
-
printf("Socket created");
-
ss.sin_family=AF_INET;
-
ss.sin_addr.s_addr=inet_addr("127.0.0.1");
-
ss.sin_port=htons(SERVERPORT);
-
-
-
retstatus=connect(sockd,(struct sockaddr*)&ss,sizeof(ss));
-
perror(s);
-
printf("after connect %d",retstatus);
-
-
if(retstatus==-1)
-
{
-
printf("Error in connecting ");
-
exit(1);
-
}
-
-
-
retstatus=send(sockd,"hello"/*argv[2]*/,strlen("hello"/*argv[2]*/)+1,0);
-
if(retstatus==-1)
-
{
-
printf("could not send data");
-
exit(1);
-
}
-
-
printf("client closing");
-
i=shutdown(sockd,2);
-
}
-
The client on execution prints -1 in this line : - printf("after connect %d",retstatus);
.
Waiting for reply.
Pawan
2 2062
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |