Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Saphrosit

macrumors newbie
Original poster
Oct 10, 2010
12
0
Hi,

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

Code:
	int listenfd,connfd,n;
	struct sockaddr_in servaddr,cliaddr;
	socklen_t clilen;

	char *mesg = (char*) malloc(1000*sizeof(char));
	
	listenfd=socket(PF_INET,SOCK_STREAM,0);
	
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = INADDR_ANY;
	servaddr.sin_port=htons(20600);
	
	bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
	
	listen(listenfd,5);

	clilen=sizeof(cliaddr);
	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));
	printf("-------------------------------------------------------\n");
	mesg[n] = 0;
	printf("Received the following:\n");
	printf("%s\n",mesg);
	printf("-------------------------------------------------------\n");
		

	close(connfd);
	close(listenfd);
I managed to establish a connection using telnet and running

Code:
    telnet 192.168.1.2 20600

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


port 20600 seems to be open, according to http://www.canyouseeme.org/ and http://www.yougetsignal.com/tools/open-ports/ (in fact I can read in the console that a packet has been received), but if I run

Code:
    telnet 151.53.150.45 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?
 

r0k

macrumors 68040
Mar 3, 2008
3,611
75
Detroit
Can you set up a DMZ where 192.168.1.2 is in the DMZ?
 
Last edited by a moderator:

Saphrosit

macrumors newbie
Original poster
Oct 10, 2010
12
0
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...
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
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...

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.
 

Saphrosit

macrumors newbie
Original poster
Oct 10, 2010
12
0
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?
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
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?

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?
 

Saphrosit

macrumors newbie
Original poster
Oct 10, 2010
12
0
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).
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
...or you could support DNS, and connect to for example [servername].dyndns.org instead of an IP address.

You can then edit the /etc/hosts file (or equivalent) on your LAN computer(s) to point to 192.168.1.2 instead of resolving the public IP through name servers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.