How can i connect to my friend through internet with sockets? - c

I've written the game on C with sockets which perfectly works on LAN, but how can i connect to my friend through internet? Our ip addr. are dynamic, but even so (i can recompile program with current values and give him it), what should i enter? hostname gives 127.0.0.1 or 192.168.., both are correct and absolutely logical, can someone help me and say any method for connecting each other?
P.S. UNIX platform;

First off the IP you're looking for is your public IP. To find it, simply google "what's my ip" from both locations, and that'll give you the two IPs. Second you'll need to configure your routers to perform port forwarding.

Related

which IP to use to connect to two different computers using telnet C

I have two laptops, both running Ubuntu.
I want to write a chat server in C, which can talk to my other laptop and receive messages back from it.
I know that when the client and server programs are both in a single machine, I can use local host (127.0.0.1) and make them talk using telnet but what about connecting two different computers (which are on the same network)? Which IP and port should I use for them?
A is the Server with IP[A] address (ex: 192.168.1.2)
B is the Client with IP[B] address
So on your chat Server on A at some point in the code you will open a port, you will specify a port number to open. If the number of the port is X then on the client you will connect to IP[A]:X
To give you an example with X=4444, you will open a connection like: 192.168.1.2:4444
Regards,
Basically all that you need to make sure as far as a port goes is that the port is not already in use by another service. There is a number a ways to do this, but I suggest you start by looking at commonly used ports.
As far as IP goes, you are going to want to determine if you want to use the public or private IP of the machines that you are attempting to connect. If you are using the public IPs you will probably have to set up port forwarding in some capacity. If you are just connecting two private ips (i.e. the one the same network) then you don't need to port forward.
Ultimately, this issue is highly specific to you use case and we would need more information to answer it for your exact case.

How can I connect two sockets on different machines?

I'm sure this seems like a simple question but I can't seem to find the answer anywhere. I've been playing with basic client/server code in C, basically the same stuff that's posted on any intro socket programming guide, and no matter how I vary the few lines of code I can't connect two machines over the internet. I can connect locally with 127.0.0.1 and all the examples work fine, but I want to be able to connect two separate hosts.
I've tried using IP addresses - ifconfig and curl ifconfig.me return different IP's for every machine or remote box I have access to and I've been told this means they're all behind NAT which means I can't connect with an explicit IP.
I've tried using username#hostname with gethostbyname() - I've been using whoami and hostname on the serving machine to determine the hostname the client should use, however the DNS always returns an error. How should I determine the hostname I should supply to the client so that I can connect?
Other than that I'm not sure what the issue could be because I'm using the same socket code posted all over the web and I've checked every line many times against several different examples. This has been frustrating me for a long time, any help would be much appreciated.

How to check if an IP address corresponds to localhost in C

In C on linux, is there a canonical way to check that an IP address corresponds to localhost?
That is, I'm looking for a function is_localhost such that if my computer has an external IP of "1.2.3.4", then calling is_localhost on any of "localhost", "127.0.0.1" or "1.2.3.4" should return true, and any other IP will return false.
On a side note, how difficult is it to spoof this information - does checking that the host is localhost in this way guarantee that the request actually came from this computer?
For context, I'm writing a management interface for a server. I'd like to make the read-only management bits, like viewing a list of connections, available over the network, but for anything dangerous, like manually killing a connection, you should be doing it by running a script on the server itself.
Thanks!
I think you may be trying to solve your problem in the wrong way - If you want to restrict access to your remotely accessible application by checking if its the local host or not then checking the IP address would be a very bad way to do it. A PC can have any number of easily configurable network interfaces with IP addresses of your own choosing. So it would be very easy to work around.
You may want to look into adding some basic authentication or simply don't allow certain functions to be run remotely. There would be many ways to achieve this, but I think the scope of the question ends here with -- Don't rely on checking for the IP address. :)

personal program through a vpn like hamachi or open vpn?

I'm making a simple game than uses udp or tcp sockets to connect in c.
I've gotten vpn set up before and used the default vpn chats.
I'm just wondering how do I make my program work?
What port do I use? How do I make connect through the vpn?
I know just typing in the vpn address doesn't work.
Well, all of this should depend on how you have things set up. The VPN software will connect to each other using the ports and hostnames or IPs you have designated.
The game you say you are designing: is this supposed to be a two person thing, so they will have to talk? In that case, you specific whatever port you desire in the code when you are making it.
Hope this is what you are looking for :-)

Faking a network communication to test client/server on one computer

I have a course project where I'm suppose to present some networking stuff. There are great chances that I have to do it on one computer. So my question is, how do I show a communication between tcp/ip or tcp/udp? I'm not a networking expert, I understand the concept of sending data which is red. And I think I need two IP addresses.
You don't need two IP addresses. Just start the server on the machine, and have the client connect to address 127.0.0.1 and the appropriate port. (This address always refers to the same machine the program is running on.)
You can run a virtual machine on your computer and work on network between them.

Resources