-1

For my project I have to develop a TCP client server connection in c. I've done both files and they are both working with the localhost address 127.0.0.1. However, I´d like to run two server simultaneously, one as 127.0.0.1 and the other as 127.0.0.2 for example.

To do that, I would like to set an IP address for each terminal window. I haven't found how to do that. I've found some information about loopback, ip addr add 127.0.0.2 dev lo, but don't know how to run each window with an IP. Is that possible? How?


I know it would work if I had different ports, or if I had different server's code only changing the specified IP as 127.0.0.1 on one and 127.0.0.2 on the other. However, the thing is, I want to know if it is possible to specify one for each terminal for example so I can use the same server code on them. That's for simulating connection to different computers.

As for now, I set the addr as INADDR_ANY, so any connection 127.X.X.X is accepted and works. But them again, on the same port number, which is what I need, I want to run 2 or more server simultaneously, so I need different IPs. And as localhost is set as 127.x.x.x, I want to narrow it down, so each terminal receives "part" of the localhost address

1
  • "Each terminal" doesn't make any sense - you can't assign an IP address to a specific terminal. You can try and configure individual processes which bind to ports to use different IP addresses, but whether each process is able to be configured that way or not is dependent on the individual processes. If your project doesn't have the ability to specify address for binding as well as port to bind to, then it can't do it (and asking how to implement that is a Stack Overflow question, not an Ask Ubuntu one)
    – Thomas Ward
    Nov 8, 2016 at 2:58

1 Answer 1

2

That's not how IP addresses work. All 127.X.X.X addresses resolve to your same localhost/lo interface. Also if your server listens/"binds" on any 127.X.X.X addresses only a client running ON THE SAME BOX can connect to it. e.g. other clients on the same network won't be able to connect to it.

You have to specify in your server's C code which IP address (and port) to listen on, or you can pass the IP in to a server program as an argument. Starting the same code from different terminals makes no difference.

If you want to start running both servers simultaneously you have to run them on different TCP port numbers. (Which is the whole purpose of ports)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .