PDA

View Full Version : Socket connection problem C




Saphrosit
Apr 7, 2011, 12:49 PM
Hi,

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

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
Apr 7, 2011, 12:54 PM
Can you set up a DMZ where 192.168.1.2 is in the DMZ?

Saphrosit
Apr 8, 2011, 12:29 PM
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
Apr 8, 2011, 01:12 PM
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
Apr 9, 2011, 08:52 AM
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
Apr 9, 2011, 01:03 PM
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
Apr 10, 2011, 03:43 AM
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
Apr 10, 2011, 05:31 AM
...or you could support DNS, and connect to for example [servername].dyndns.org (http://dyndns.com) 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.