0

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));

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

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 http://img593.imageshack.us/img593/3959/schermata20110405a22492.png

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

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?

alfred
  • 217
  • 1
  • 6
  • Routers often have config setups that tell them to accept/refuse ping, telnet, and more. Can you telnet or browse into the router and see what's going on with an config setups? – Pete Wilson Apr 07 '11 at 18:04
  • You already asked this - http://stackoverflow.com/questions/5558042/remote-socket-connection-c. As Emil mentioned in that thread, you might want to check out http://superuser.com. This doesn't seem like a problem specific to your code. – jwd Apr 07 '11 at 18:05

1 Answers1

0

If you are typing:

telnet 151.53.150.45 20600

from the LAN rather than from the WAN, then your NAT most probably does not handle hairpin situations properly. This means it only expects you to use the translated address from the WAN.

The solution is check whether you can change the configuration of your NAT to enable usage of translated address on the LAN too (it is sometimes a requirement for P2P systems). If such functionalities are not available, then you need a new NAT.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453