Socket application does not work over the internet - c++

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

Related

Winsock client / server tutorial fails with desktop as client, but works with laptop as client

I have successfully built the client and server modules from the Getting Started with Winsock tutorial.
I have a desktop and a laptop both connected to my wireless router – both running Windows 10.
Running the client module on the laptop, I am able to successfully transmit data back-and-forth to the desktop (running the server module) using the desktop's IP address.
Running the client module on the desktop with the laptop's IP address as the command line argument, I get an "Unable to connect to server!" message after a ten second delay.
If I try to run both modules on the desktop in separate console windows using the "localhost" command line argument, the client console displays "Bytes sent: 14" and hangs waiting for a response from the server – however this works if I use either the desktop name or the desktop IP address in place of "localhost".
I am able to run both modules on the laptop using either "localhost", the laptop name, or the laptop IP address as arguments.
I have gone through the same motions with port 27015 forwarded on the router and incoming and outgoing firewall exceptions added to both the desktop and the laptop – there is no difference either way.
Any assistance would be greatly appreciated as I cannot figure out why this works in one direction but not the other.
Thank you for the suggestions Karsten and Andriy. I first tried getting the two computers to ping each other and neither was successful. After researching online, I was able to get them to ping after turning on "echo requests" in the firewall settings, but my original problem persisted. I then tried turning off both firewalls and I was able to get my server and client programs to work both ways. That wasn't a great long-term solution, so I tried selectively disabling the firewalls and realized it was an issue on the laptop's end. I noticed that my "server.exe" program was in the allowed apps list twice – one instance granting private access and one granting public access – but only one instance was active. I deleted both and added "server.exe" again with both public and private access boxes checked, which solved my issue.

TCP connection between two raspberry pis error, “No such host exists”

This is my first C++ project where I am communicating via TCP and I used this client/server method from Here. I changed the hostname on the pi that acts as the server and I believe that I am running the client program with the right hostname and port, however it keeps throwing me the error "No such host exists" even though they are on the same network. The client and server programs work if I run them on the same pi.
Any help would be greatly appreciated. Thanks!

Ports open, but no one can connect

I am programming a server system for a small RPG game made with SFML. This is my first project since we switched our ISP to Comcast and I am having trouble with people connecting to the server. I can connect locally fine and all of the port checking tools say that the port is open, but my friends cannot connect using my external IP. I turned my firewall off on my router and on my computer to see if it'd help the situation at all, but the same thing is occurring.
Comcast gave me an Arris TG862G-CT router.
Here's an image of the port forwarding page:
The port is shown as open, but my client cannot connect:
I'm not sure which language you are using, but many make a distinction between opening a port with the host address 127.0.0.1, 0.0.0.0, and localhost. Many times you will need to use 0.0.0.0 to connect from an external pc where as 127.0.0.1 and localhost are used as a loopback and only accessible by you.

Get COM/DCOM Connections?

I'd like to figure out what IP address a DCOM client is connecting from. I have an OPC server, and from the context of the COM/DCOM code, there is no way to get the client IP that I can find. This article appears to confirm that (http://blogs.msdn.com/b/sanpil/archive/2004/03/01/82302.aspx).
I tried using a C++ library that netstat uses (iphlpapi.lib, GetExtendedTcpTable) to get open connections with no luck. It looks like COM/DCOM defaults to UDP.
The only other thing I can think of is using WireShark pcap to try and capture inbound traffic on port 135?
Does anyone have any clever ideas?
It's said "Using DCOM through firewalls becomes problematic because it dynamically allocates one port per process (configurable through the registry) and requires UPD and TCP ports 135-139 to be open. ".

New To Socket Programming, Need Help Understanding How To Connect

I have a C++ Program listening for incoming socket connections on port 2222.
I have an Adobe AIR/Flex application that attempts to connect to it, when I click a button.
When I Connect To My Socket Over My Intranet, My C++ program hears and accepts the incoming socket connection and Everything Works Fine:
var Sock:Socket=new Socket("192.168.1.100",2222);
But When I Try To Connect Using My Real IP, I Get Error #2031:
var Sock:Socket=new Socket("76.18.24.118",2222);
Both programs are running on my laptop computer. I am behind a router, but have configured that router with port forwarding so that port 2222 maps to my laptop.
What Am I Missing? I am very new to this, so perhaps I'm missing something obvious to you smarties.
What does the error code mean (see your docs for socket)
Are you sure the receiving server process is alive?
The traditional starting point is Beej's guide
Port forwarding does not mean that you can use an external IP address, your laptop is still on the private 192.168 network. What it means is that when someone tries to connect on 78.18.24.118:2222 the router converts the IP address to 192.168.1.100:2222. This effectively allows you to run a server inside your network but allows an outside client to connect to you.
Look up DNAT for more information.