Socket connection problem C

Discussion in 'Mac Programming' started by Saphrosit, Apr 7, 2011.

  1. macrumors newbie

    Oct 10, 2010

    I wrote a simple server application in C. This server do nothing except print the received message, then exit. Here is the code

    	int listenfd,connfd,n;
    	struct sockaddr_in servaddr,cliaddr;
    	socklen_t clilen;
    	char *mesg = (char*) malloc(1000*sizeof(char));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_addr.s_addr = INADDR_ANY;
    	bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    	connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
    	n = (int) recvfrom(connfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&clilen);
    	sendto(connfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
    	mesg[n] = 0;
    	printf("Received the following:\n");
    I managed to establish a connection using telnet and running

        telnet 20600
    where is the local ip of the server.
    The machine runs behind a router ZyXel p-660HW-61 (
    The problem is I cannot reach the server if I specify the public ip of the machine (
    I set NAT configuration to the server local ip on all port from 20000 to 21000

    port 20600 seems to be open, according to and (in fact I can read in the console that a packet has been received), but if I run

        telnet 20600
    I get a "Connection Refused" error.
    Firewall is disabled, both on the router and on the server machine (that is the same running telnet).

    Any help?
  2. r0k, Apr 7, 2011
    Last edited by a moderator: Apr 7, 2011

    macrumors 68040


    Mar 3, 2008
    Can you set up a DMZ where is in the DMZ?
  3. thread starter macrumors newbie

    Oct 10, 2010
    There should not be any DMZ, firewall is completely disabled...
    Besides, I receive packets from online open port checker (as I mentioned), so I seem to be able to receive packets from outside my LAN...
    what I do not understand is why I cannot establish a connection with a telnet call...
  4. macrumors 6502a


    Apr 14, 2007
    Pretty simple. You are on your LAN, not the Internet. Use your local IP and it will work fine. If you are connected to the Internet from somewhere outside your LAN, use your router's public IP address.

    If you try to connect to your routers public IP address from inside, it won't route back. Some routers can be configured to do that, but most can't.

    Since your firewall appears to be correctly opened, the problem is either that you're trying from within the LAN, or there is another firewall between the computer running telnet and your router that stops the traffic.
  5. thread starter macrumors newbie

    Oct 10, 2010
    It seems you're right! I tried from a friend's computer and it works fine...

    I wonder why some routers block this kind of connection...Do you know what field should I check to see if there's a way to allow this?
  6. macrumors 6502a


    Apr 14, 2007
    You're thinking about it in the wrong way. The router isn't actually blocking that if it doesn't work, it just doesn't take extra measures to route the traffic that unnatural way.

    Why would you need it to behave like that anyway?
  7. thread starter macrumors newbie

    Oct 10, 2010
    No reason in particular, I'm just doing my firsts experiments in socket programming and I thought it could be useful if, for example, I want to connect two client to my server, one in the LAN and one outside. In the client application, I could set the server's ip programmatically and let the client in the LAN connect to the server through the public ip, but now I should think of two versions of the same client (or let the server's ip be set at runtime).
  8. macrumors 6502a


    Apr 14, 2007
    ...or you could support DNS, and connect to for example [servername] instead of an IP address.

    You can then edit the /etc/hosts file (or equivalent) on your LAN computer(s) to point to instead of resolving the public IP through name servers.

Share This Page