which IP to use to connect to two different computers using telnet C - 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.

Related

Recommended port for proxy server

I'm trying to create a proxy server, and am having trouble decide on a trivial thing -- the port number on which it will listen. Is there some kind of convention on which port a proxy server should run on? Or should it just be greater than 1024?
Normally, you just pick a port and make it changeable for the user (or yourself). Simply, if it is already used on the system you run your proxy on, use a different one.
Many free proxy servers even listen (e.g. this) to default ports, like 8080. As long as the port is not in use on your system, it does not matter, as the real port is negotiated after a connection attempt.
For your interest, a list of registered ports can be found on the IANA (Internet Assigned Numbers Authority), which manages stuff like that. That is only a hint. I normaly use a random port like 8888 or so, even if someone somewhere already uses that.

How to run Client-Server on different computers using Sockets in C

I am able to run a client and server on the same computer on two different terminals in Linux. Now I want to run the same model on two different computers. I am guessing there is more to this than just changing the IP address and the port number to that of the Server.
Any guidance on the matter is highly appreciated.
Thanks.
Make sure the port is forwarded in your router and exceptions for the port is added to the firewall. Then you should be ready to go!
If you have the computers in the same network you can do it without forwarding your port.

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.

simulate different PC with different IP addresses in linux environment

im new to linux environment and any help/feedback would be appreciated. Im actually trying to develop a client-server (MULTICAST) program, so, i would like to test one client sending information to different servers (one-to-many relationship). thus, i would like to simulate different server side in linux with different IP addresses in one computer.
Did you try using different ports instead? I didn't try it myself, but perhaps that can help you in the mid-time.
If you're really multicasting, you don't need to worry about physical host-specific IP:s, all you should need to do is make sure all the programs (clients and servers) are using the same multicast group addresses. Then they should all see each other's traffic automatically.
There's nothing stopping you from running multiple clients on the same machine that also runs the server, in this case.
I sounds like you want to test your code with different IP's. You can create IP aliases on your interface and simulate multiple IP's on one computer.
for e.g. if eth0 is you're active interface with IP, say 192.168.5.11 you can assign another IP to eth0:0 (an alias to eth0) as below.
ifconfig eth0:0 192.168.5.12 netmask255.255.255.0 up
ifconfig eth0:1 192.168.5.13 netmask255.255.255.0 up
run your server on one of the IP's and distribute clients to all your aliases
Use either of the following when you do not have sufficient hardware:
Multicast loop which has the IP stack redirect outbound packets to local receivers.
Virtual machines.
Be aware that semantics of the socket option for #1 change depending on the operating system; for #2 only some virtual machines support multicast, refer to the vendor for details.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms739161(v=vs.85).aspx
Ultimately though you must test with different machines due to specific artifacts of how hosts manage multicast groups. You can for instance create send-only membership which will block every other application on the host. Also consider that an internet, lower case 'I', will introduce further artifacts regarding group joining and propagation delays and drops that your application may need to be aware of.
You can create multiple IP for same machine with help of IP alias. As mentioned above.
But to create multiple Server at one PC you must need different port for each server if you want to simulate the all server behavior with network as well.
I mean for one port multicast traffic always goes to that and some process in the PC will be receiving the packet and has to serve for all server in the PC, Means you have one packet only and all server is receiving with locally manipulation.
But really simulation would be you have multiple server at 1 PC and all are receiving multicast traffic from network rather then from local process.
my Solution: You keep number for server == number of port at the PC. Client send the multicast traffic over all port simultaneously and all server at the PC end will be receiving multicast packet from corresponding port from the Network.
Please correct me if my understanding is wrong.

What client-side situations need bind()?

I'm learning C socket programming. When would you use bind() on the client-side? What types of program will need it and why? Where can I find an example?
On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use. Generally you don't care, but in some cases, there may be a firewall on the client that only allows outgoing connections on certain port. In that case, you will need to bind to a specific port before the connection attempt will work.
An example would be the data connection of an active FTP connection. In this case, the server connects from its port 20 to the IP and port specified by a PORT or EPRT command.
A classic example of a client program using bind() is the (obsolete) rlogin / rsh family of network clients. These clients were intended to be used within networks with strong trust relationships - in some cases the server machine trusts the client machine to tell it the username of the user that is connecting. This required that the client program connect from a low port (a port less than 1024), because such ports are restricted to the root user and thus (in theory) prove that the client being used is authorised by the system administrator.
The NFS protocol has similar trust relationships, and similarly the client makes connections from a low port number, using bind().
Another example is IRC clients that allow the user to specify a particular source IP address to connect from. This is to accomodate users with many IP addresses assigned to their machine, each with a different "vanity" domain name assigned to it. Choosing which IP to connect from (with bind()) allows the user to choose which domain name to appear as on IRC.
A good situation would be in a p2p case, you’re communicating with a STUN Server with a bound socket, and the STUN Server tells you the port on which he is receiving messages from your socket (that can be different from the one you specified when you bound your socket depending on your network and more specifically on your NAT type). This will allow you to be aware of the real port translation that your NAT is doing, and you’ll be able to give this information to potential peers that want to connect to you.
Binding the socket is useful as some NATs are dynamically giving you ports (binding on port x twice might not give you the same “real” port). So you’ll be able to directly use the socket you bound to listen on the port.
I suppose you should bind() in the case of UDP sockets.
bind function is one of "key" functions. It associates your socket (server or client) with address (ip + port). As for Windows you must use bind for WinSockets. There is good book about it "Network Programming for Microsoft Windows" by Anthony Jones and Jim Ohlund.
Bind can be used to attach names to a sockets. Thus, say you create a software that uses a particular TCP port, you need to bind it and then, you will know the TCP port it is using.

Resources