How can I connect two sockets on different machines? - c

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.

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.

Making a C socket Program to work online on WAN

How can a C socket program be made to communicate online on a Wide Area Network (WAN)? For example, having a server program (in C socket) running on a friend's computer in UK that could be accessed on a computer running a client program(in C socket) located in Russia. I tried my client/server online but it did not work. It only worked on my local machine.
A connection between two computers looks very similar to a phone call, but actually there are lots of details that may turn it into a difficult thing.
Unfortunately, you haven't provided these details. So, let me ask them to you:
Are you connected directly to a telco, using 3G, for instance ? Or are you connected to another device that is connected to the Internet ? For instance, a 3G modem, that offers Wi-Fi connectivity ?
The same questions should be answered by your friend.
These two questions will provide a 1st step to understand and solve your problem.
Good luck, and a fine week for you and family.

How to create a BACnet client in C

I am trying to create a client in C that will talk with a BACnet server. This BACnet server is stored on an industrial device (CAN2GO) and I am not sure how I could talk with this device.
I spent quite some time reading documentation for BACnet and I never found a clear example for a BACnet client. I already did some server and clients using TCP and UDP but I don't know how to start this BACnet client and I must say I am getting quite desperate.
I found a library which seems to correspond to what I want which is called BACnet protocol stack but when I tried the whois exemple no device was found (I expected to found the bacnet server but maybe I shouldn't ?).
So my question is : could you give me an exemple in C, or another language but C would be better, that would communicate with a BACnet server (nothing complicated just a question and analysing response). This example could be using the library I just wrote about or if you prefer another library I am of course open to everything.
Thank you very much for your time and answers.
I have used that stack and it is the best open source one you are going to find. If you cannot see anything using the demo\whois\bacwi example from that library, then there is something wrong with your setup. In particular, are you using IP? Are your BACnet client and BACnet server on different machines (they cannot be on the same without some serious tweaking)? Are the two machines on the same IP subnet? (They must be, once again, unless you do some serious tweaking (in this case, setting up BBMDs (BACnet Broadcast Management Devices))).
You will also want to try the "Read Property" example (demo\readprop\bacrp.exe) to actually read a value from the server.
If you are still stuck, then post your detailed problem at the link on Sourceforge, Steve, the author, is very responsive to questions.
I am currently using the stack - just started. I had a little trouble at first, not sure if my problem is the same but.. I basically am using some BAC components made from Schneider Electric (UNC-500) and an old un-supported platform (Niagara R2). On my laptop I created a host server and addressed it to a private LAN network between it and the UNC. My laptop was also using wifi, which was utilizing DHCP, so I had two separate interfaces going. This was my problem. I couldn't read or get 'I-AM' responses back from the UNC. As soon as I turned the WIFI off, I got the 'I-AM' broadcasts. Make sure that you are on the same network as your device, and that there are not other interfaces active. Maybe there is a way to assign the interface to use, IDK. I just started using it.

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. :)

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

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.

Resources