Thread: Error in connect() in TCP client code

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Error in connect() in TCP client code

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Add these includes
    #include <string.h>
    #include <errno.h>

    And modify your error code
    Code:
    	if(connect(socket_desc,(struct sockaddr *)&address,sizeof(address))==-1)
    		{
    		char *msg = strerror(errno);
    		cout<<"connecting Error: Reason="<< msg <<endl;
    		exit(1);}
    Your indent style is hard to read.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    The Winsock API seems to be good to work with as well... But I take it you want it portable to *nix or something since your not using that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. dynamic array
    By mouse666666 in forum C Programming
    Replies: 36
    Last Post: 04-11-2010, 02:27 AM
  4. writing dhcp client code using sockets on Linux
    By dash in forum Linux Programming
    Replies: 3
    Last Post: 08-04-2009, 10:20 AM
  5. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM

Tags for this Thread