Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 7, 2011, 12:49 PM   #1
Saphrosit
macrumors newbie
 
Join Date: Oct 2010
Socket connection problem C

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


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?
Saphrosit is offline   0 Reply With Quote
Old Apr 7, 2011, 12:54 PM   #2
r0k
macrumors 68040
 
r0k's Avatar
 
Join Date: Mar 2008
Location: Detroit
Can you set up a DMZ where 192.168.1.2 is in the DMZ?
__________________
-r0k
Macbook Pro (late 2011) iPad mini iPhone5
Got a scan to ftp scanner? Enable ftpd in Mavericks!

Last edited by kainjow; Apr 7, 2011 at 04:52 PM. Reason: removed long quote
r0k is offline   0 Reply With Quote
Old Apr 8, 2011, 12:29 PM   #3
Saphrosit
Thread Starter
macrumors newbie
 
Join Date: Oct 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...
Saphrosit is offline   0 Reply With Quote
Old Apr 8, 2011, 01:12 PM   #4
itickings
macrumors 6502a
 
itickings's Avatar
 
Join Date: Apr 2007
Quote:
Originally Posted by Saphrosit View Post
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.
itickings is offline   0 Reply With Quote
Old Apr 9, 2011, 08:52 AM   #5
Saphrosit
Thread Starter
macrumors newbie
 
Join Date: Oct 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?
Saphrosit is offline   0 Reply With Quote
Old Apr 9, 2011, 01:03 PM   #6
itickings
macrumors 6502a
 
itickings's Avatar
 
Join Date: Apr 2007
Quote:
Originally Posted by Saphrosit View Post
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?
itickings is offline   0 Reply With Quote
Old Apr 10, 2011, 03:43 AM   #7
Saphrosit
Thread Starter
macrumors newbie
 
Join Date: Oct 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).
Saphrosit is offline   0 Reply With Quote
Old Apr 10, 2011, 05:31 AM   #8
itickings
macrumors 6502a
 
itickings's Avatar
 
Join Date: Apr 2007
...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.
itickings is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone 5S headphone socket problem Artmuzz iPhone Tips, Help and Troubleshooting 2 Jan 23, 2014 02:38 PM
Connection problem vickiec iPad Tips, Help and Troubleshooting 1 Jun 2, 2013 06:59 AM
displayport connection problem cgreen777 MacBook Pro 0 Mar 24, 2013 03:15 AM
Problem with data connection cnour iOS 6 2 Oct 31, 2012 11:34 AM
problem with wi-fi internet connection kc2ine Mac mini 6 Oct 10, 2012 04:16 PM

Forum Jump

All times are GMT -5. The time now is 10:25 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC