Thread: Not listening right.

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Not listening right.

    Whenever I try to telnet to localhost on port 34242 (the port my program listens on), it just says that the connection was refused. I probably did something horribly wrong, but I don't know what.
    Code:
    #include <stdio.h>
    #include <winsock.h>
    
    
    #pragma comment( lib, "wsock32.lib" )
    
    int main( void ) {
    
    	WSADATA wsaData;
    	int SFD_Listen;
    	int SFD_New;
    	int Result;
    	int size = sizeof( struct sockaddr_in );
    	struct sockaddr_in ServerAddress;
    	struct sockaddr_in ClientAddress;
    
    	WSAStartup( MAKEWORD( 1, 1 ), &wsaData );	
    
    	SFD_Listen = socket( AF_INET, SOCK_STREAM, 0 );
    	if( SFD_Listen == -1 ) {
    
    		perror( "socket" );
    		exit( 1 );
    		
    	}
    
    	ServerAddress.sin_family = AF_INET;
    	ServerAddress.sin_port = 34242;
    	ServerAddress.sin_addr.s_addr = INADDR_ANY;
    	memset( ServerAddress.sin_zero, '\0', 8 );
    
    	Result = bind( SFD_Listen, (struct sockaddr *)&ServerAddress, sizeof( struct sockaddr ) );
    	if( Result == -1 ) {
    
    		perror( "bind" );
    		exit( 1 );
    
    	}
    
    	Result = listen( SFD_Listen, 20 );
    	if( Result == -1 ) {
    
    		perror( "listen" );
    		exit( 1 );
    
    	}
    	
    
    	SFD_New = accept( SFD_Listen, (struct sockaddr *)&ClientAddress, &size );
    	send( SFD_New, "Hello.", 7, 0 );
    
    	closesocket( SFD_Listen );
    
    	WSACleanup( );
    
    	return 0;
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yep. Just noticed that now. Put in the htons( ) and it works fine.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listening socket and thread problem
    By esaptonor in forum Windows Programming
    Replies: 6
    Last Post: 06-19-2010, 03:04 AM
  2. What are you listening to now?
    By Graham Aker in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 01-27-2009, 11:36 AM
  3. Question about forking with sockets listening
    By Phoenix_Rebirth in forum C Programming
    Replies: 2
    Last Post: 11-10-2008, 04:05 AM
  4. single Socket for listening and sending/receiving, Simultaneously?
    By Aidman in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-01-2003, 11:17 PM
  5. Replies: 1
    Last Post: 10-03-2002, 09:52 AM