Winsock

Posts 1–7 of 7 · Page 1 of 1
Winsock
Ok, I have made a program that is a server and the client connects to the server to get the number of questions it should ask the client. So, it works, but in order to make the client connect to the server i am confused.

The problem (maybe a problem) is that when the client connects to the server it uses the ip of the computer it will connect to and the hostname is localhost.

for example:

SOCKADDR_IN sockaddr;
sockaddr.sin_addr = inet_addr("MY COMPUTER ADDRESS");



Now, I am guessing that using the computer IP4 address like "192.168.x.xx" will only work if the client is in the same network as the server. What if the people trying to connect to the server outside the network, will it work using my computer ip address, or will I have to use the ip address of my network, the one found by going to whatismyip.com?

It's a really cool program, I will release the source to you guys right after I get this small bug fixed. Thanks.
Ok, apparently nobody understood my confusing post. This is basically what my question is. When I am giving my client that connects to my server to other people. Should I program it to connect to my local ip or global?
You need to use your global one (hence the one found on whatismyip.com). You might also need to do some port forwarding if your using a router for the port your server listens on for incoming connections.

If you are planning to keep your server running, hence host some sort of service, I suggest you get yourself a no-ip host. (No-IP - Dynamic DNS, Static DNS for Your Dynamic IP). There you can set up a domain/host like "myserver.something.com" that can then be updated to forward to your current IP. Because if you hardcore a IP into the clients and your IP on the server machine changes, they won't be able to connect.

I don't remember how it's done using Winsock but I'm just working with a project in SDL where I do pretty much the same, here's how it's done using SDL_net wrapper:

char* server = "mygame.hostingsite.com";
SDLNet_ResolveHost(&serverIP, server, GAME_PORT);

I guess it's done in a similar fashion without a wrapper.
Thank you for your reply. Ok, so i will hardcode my global ip and i will see if it works. I will report back.
Nevermind, I am having trouble understanding how to do it. I am just wondering though before i go through the trouble of explaining. Would it work if I just entered my global ip address in the sockaddr_in structure instead of my local ip. If not how should i do it?


(I read about port forwarding, but i want this to be last resort, i want to solve the issue code wise if possible)
Use your public IP address; external networks don't (and can't) see your internal network for a very good reason.

If you're behind some local network, you'll need to port forward the listening port to your computer, otherwise your router will have no idea where to send this request.


Quote Originally Posted by 258456 View Post
Nevermind, I am having trouble understanding how to do it. I am just wondering though before i go through the trouble of explaining. Would it work if I just entered my global ip address in the sockaddr_in structure instead of my local ip. If not how should i do it?


(I read about port forwarding, but i want this to be last resort, i want to solve the issue code wise if possible)
If you're behind a router of some sort, you must port forward, otherwise your router won't have a clue which of the X devices on the network to send the request to, and will just drop the request.
Oh, ok freedompeace. That makes sense. So now I will google port forwarding. Thanks for your guys' help.
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us