Thread: Error while creating socket....

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Error while creating socket....

    Hi,

    Following is the code i have been checking...

    Code:
    void createsocket()
    {
    	struct sockaddr_in server_addr;  
    	unsigned int port = 0;	
    	char server_ip[255];
    
    	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    		perror("Socket");
    		exit(1);
    	}
    	memset( &server_addr , 0 , sizeof(server_addr) );  
    	 
    	server_addr.sin_family = AF_INET;  
    	CheckIPAddress(server_ip);
    	CheckPort(&port);  //This reads the value from flash file     
                 server_addr.sin_addr.s_addr = inet_addr(server_ip);  
    
                 server_addr.sin_port = htons(27015); //While hardcoding the port establishs connection    
    	server_addr.sin_port = htons(port); //This gives an error failed to connect           
    	
       //Now connect to remote server  
        if(connect( sock , (const struct sockaddr*) &server_addr , sizeof(server_addr) ) < 0)  
        {  
      
        }  
    
    }

    1. I have a issue while assiging port to htons() ---> if hardcoded it seems to establish the connection.
    2. If the same is replace with a read value from a file. Connection established fails.............

    Am I doing something wrong with the way the variable is defined.
    I am using an unsigned int -----> while the htons() takes unsinged int.
    But value read from the file is stored in an int variable.
    Is this the problem?

    Let me know where i am wrong?

    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,659
    So have you tried adding say a printf, or using the debugger to set a breakpoint, to actually verify that port contains what you think it should contain.
    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
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by Salem View Post
    So have you tried adding say a printf, or using the debugger to set a breakpoint, to actually verify that port contains what you think it should contain.
    yes.............Its returning the same as the one which is hardcoded(As i have written the hardcoded value to file).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    connect(2): initiate connection on socket - Linux man page
    Perhaps print some error message relating to the error return result. It would be more useful than "it doesn't work".

    > I am using an unsigned int -----> while the htons() takes unsinged int.
    Mmm, all the manual pages I've read indicate that htons() takes a short int, not an int

    You might want to post the code for CheckPort(), in case it is trashing memory past the pointer you give it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Error or nohup error ??
    By jayasekar in forum Linux Programming
    Replies: 2
    Last Post: 01-28-2010, 11:53 AM
  2. Error in creating socket in a client (UDP)
    By ferenczi in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-27-2008, 11:11 AM
  3. Please Help! Socket error
    By robsmith in forum C Programming
    Replies: 9
    Last Post: 05-09-2005, 10:51 AM
  4. SSL and 503 FTP Error :: Socket
    By kuphryn in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-18-2005, 04:15 PM
  5. Getting a socket error
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 03:02 PM