Thread: Socket Server

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Socket Server

    I obviously am doing something wrong. Can someone identify what it could be?

    If I try to connect to this server using "127.0.0.1", all is well. But if I use my WAN ip, I never get connected. (The port is already forwarded).

    Any help appreciated.
    Code:
    #include <stdio.h>
    #include <winsock2.h>
    #include <windows.h>
    
    int main (void)
    {
    	WSADATA wsaData;
    
    	SOCKET sckserver = INVALID_SOCKET;
    	struct sockaddr_in server;
    
    	SOCKET sckclient = INVALID_SOCKET;
    	int addrclientlen;
    	struct sockaddr_in client;
    
    	int result = 0;
    	unsigned int numrecv = 0;
    
            char recvbuf[1024];
            int recvbuflen = 1024;
    
    	unsigned short *temp, port;
    	unsigned long *tempip, ip;
    
    	printf("Initializing Proxy Server...\n");
    	result = WSAStartup(MAKEWORD(2,2), &wsaData);
    	if (result != NO_ERROR)
    	{
    		printf("Error at WSAStartup()\n");
    		return 0;
    	}
    
    	sckserver = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (sckserver == INVALID_SOCKET) 
    	{
    		printf("Error at socket(): %ld\n", WSAGetLastError());
    		WSACleanup();
    		return 0;
    	}
      
    	server.sin_family = AF_INET;
    	server.sin_addr.s_addr = inet_addr("127.0.0.1");
    	server.sin_port = htons(6112);
    
    	if (bind(sckserver, (SOCKADDR*)&server, sizeof(server)) == SOCKET_ERROR) 
    	{
    		printf("bind() failed.\n");
    		closesocket(sckserver);
    		return 0;
    	}
    	printf("Socket successfully bound on port 6112\n");
    
    	if (listen(sckserver, SOMAXCONN ) == SOCKET_ERROR)
    	{
    		printf("Error listening on socket.\n");
    		WSACleanup();
    		return 0;
    	}
    
    	printf("Listening for incoming connections...\n");
    	
    	addrclientlen = sizeof(client);
    	sckclient = accept(sckserver, (SOCKADDR*)&client, &addrclientlen);
    	if (sckclient != INVALID_SOCKET)
    	{
    		printf("Connection Established!\n");
    		result = 1;
    		while (result != 0)
    		{
    			result = recv(sckclient, recvbuf, recvbuflen, 0);
    			if ( result > 0 )
    			{
    				numrecv++;
    				if (numrecv == 1)
    				{
    					printf("SOCKS Version Number: 0x%02X\n", recvbuf[0]);
    					printf("Command Code: 0x%02X\n", recvbuf[1]);
    					temp = (unsigned short *)&recvbuf[2];
    					port = ntohs(*temp);
    					printf("Port Number: %d\n", port);
    					tempip = (unsigned long *)&recvbuf[4];
    					ip = *tempip;
    					printf("%X\n", ip);
    				}
    			}
    		}
    		numrecv = 0;
    		printf("Connection Terminated!\n");
    		closesocket(sckclient);
    	}
    	  
    	WSACleanup();
    	return 1;
    }

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>server.sin_addr.s_addr = inet_addr("127.0.0.1");

    I believe the sin_addr field specifies which network interface you want the socket to listen on. In your case, you're specifying the loopback interface. If you want to listen on *all* interfaces, you can use the following:
    server.sin_addr.s_addr = INADDR_ANY;

    Alternatively, (I haven't tried this myself) you can specify your WAN IP instead of 127.0.0.1.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Thank you, that was it. I guess I never took that variable too seriously. Thanks for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  2. Non-Blocking Server help.
    By unkownname in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-27-2007, 05:04 PM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  5. socket, udp,Halflife Server
    By Tolpan in forum C Programming
    Replies: 2
    Last Post: 06-21-2002, 09:02 AM