Error while creating socket....

This is a discussion on Error while creating socket.... within the C Programming forums, part of the General Programming Boards category; Hi, Following is the code i have been checking... Code: void createsocket() { struct sockaddr_in server_addr; unsigned int port = ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10: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, 10: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, 03:15 PM
  5. Getting a socket error
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21