开发者

Socket application does not work over the internet

开发者 https://www.devze.com 2023-03-15 22:39 出处:网络
I\'ve got a problem in socket programming. I\'m currently writing a simple server/client application which asks connecting clients to answer a survey (I\'m following the exercises in the book: TCP/IP

I've got a problem in socket programming. I'm currently writing a simple server/client application which asks connecting clients to answer a survey (I'm following the exercises in the book: TCP/IP Sockets in C: Practical guide for Programmers). It works fine in my local network (using localhost to connect), but I can't make it work over the internet. Since I changed some of the code found in the book, I tried to compile the original source code from the book but it still doesn't work. I assume this is not a code problem but a network problem. I did some research and so I turned off my firewall, I forwarded the port I'm using (12543) in my router but it still doesn't work... I've got a remote windows server running Windows 7 for testing: when I run my server on it and try to connect from my computer it fails, when I try to run my server from my computer and try to connect from the remote windows server: it fails again. Even when I run my server on my computer and try to connect with the client from the same computer using my private IPv4 address 192.168.x.x or my public one, it fails ! Oh, and there is no firewall running on the windows server.

I really don't know what to do now... I can ping my windows server from my computer, I can ping my computer from my windows server, but it's impossible to connect to my application.

The source code can be found here: http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/textcode.html (SurveyServer2.cpp, SurveyClient2.cpp, SurveyCommon.h), but I don't think it's a code problem. Please tell me if it's not clear enough. And excuse me if I did some grammatical errors, I'm french!

Any help would be appreciated, thanks!

EDIT : Ok, I know what's going on: it's a compatibility issue between IPv4 and IPv6! It's not properly working yet, but I now know what to fix. Thanks everybody :)

SECOND EDIT : Well开发者_JS百科, I think I finally understood. I was binding my server to a IPv6 address, but the host my server is running on only have a public IPv4 address. When I tried to connect, the DNS resolution only returned a IPv4 address so I was unable to connect to my server. I told to getaddrinfo to return only IPv4 addresses, so now it binds on a IPv4 address and it works fine.

There is surely a way to add a IPv6 address to my host but I don't think I will need it, it works okay right now, I hope this doesn't cause any trouble.


The code makes usage of getaddrinfo and need a hostname not a IP address.


It does sound like you have a connectivity issue or a DNS issue.

Ensure the name resolution is working:

Enter 'ping server_NAME' on the command line of the client machine. If it can't find an IP address for the server name that could be your issue.

Ensure connectivity:

On the server command line:

enter 'netstat -an -p TCP'. This will show a list of all programs listening and their port numbers. You should see your server listening on port 12543.

On the machine where you are running your client program:

Use telnet to see if you can get through to the server. You can set the port number telnet uses with a command line option. Usually something like 'telnet -p 12543 server-ip'. If it says 'connection refused' then there's a connectivity issue (a firewall/etc). If it opens a connection you will get no error message and you will be able to type text to be sent to the server. You really only care if telnet was able to establish a tcp connection here.

If Telnet does connect then your issue is communication between the programs and not a network issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消