Hi, i have written a tcp client code to connect and send a message to the server.
It stops after gives "Connecting Error", so I am not able to send the message to server.
Please help me.
Code:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
int socket_desc;
struct sockaddr_in address;
socket_desc=socket(AF_INET,SOCK_STREAM,0);
if (socket_desc==-1)
{
cout<<"Create socket"<<endl;
exit(1);
}
else
cout<<"Socket created successfully and socket file descripter is : "<<socket_desc;
cout<<endl;
address.sin_family
//address.sin_addr.s_addr=INADDR_ANY;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(8051);
if(connect(socket_desc,(struct sockaddr *)&address,sizeof(address))==-1)
{cout<<"connecting Error"<<endl;
exit(1);}
else
cout<<"connection done"<<endl;
const char *message="Thank you\n\r";
send(socket_desc,message,sizeof(message),0);
if (send(socket_desc,message,sizeof(message),0)!=sizeof(message))
{
cout<<"send error"<<endl;
exit(1);
}
else
{cout<<"Message sent successfully"<<endl;}
cout<<"done";
close(socket_desc);
return 0;
}
Thanks in advance.