-1

I have written a simple program with Linux (Cent OS 7.0) and C++. It is a very small server which sends back a string of characters to the client. But my problem is that I don't know how should I access that server using an IP address?

I have used Linux Socket Interface (Berkeley), and in the section which defines the address, my code does the following:

    serverObject.
            sin_family = AF_INET;
    serverObject.sin_addr.
            s_addr = htonl(INADDR_ANY);
    serverObject.
            sin_port = htonl(portNumber);

I use INADDR_ANY as my server's address which is defined in its definition as:

/* Address to accept any incoming messages.  */

Now, how should I run the server, and then use my simple client program to send request to it. My simple client program accepts an IP address as it's destination address, this address should be the one destined toward to the server. How should I relate it then?

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • To connect two applications through the network, you need the IP and Port of the destination. Your server need to provide you those data, and the client to connect to them. Your question is about how the client can know about the server IP/Port? – Adrian Maire Feb 22 '16 at 12:59
  • My question is that how I can locate my server? and send a request from the client to it? the port is already used in the server and client. More importantly, I want to know what address should I use when I have used `INADDR_ANY` – Mostafa Talebi Feb 22 '16 at 13:04
  • Look at example code from Steven's 'Unix Network Programming', or maybe 'man select_tut'. Google for the Steven's example code. – Erik Alapää Feb 22 '16 at 13:04
  • @MostafaTalebi you locate your server by knowing the IP address of the machine it is running on. [Berkeley sockets](https://en.wikipedia.org/wiki/Berkeley_sockets) – crashmstr Feb 22 '16 at 13:10

2 Answers2

2

INADDR_ANY goes to specify that all active network interfaces in the system should be bound to. So if you're connected to more than one network, you'll be able to communicate with connections coming in from all of them. Most systems will usually have just one, though, and this still goes to say that if the IP bound to that interface happens to change, you'll still bind to that interface.

So, once you specify INADDR_ANY, you need to initiate connections according to the following rules:

  1. If you're connecting from the same physical machine, the easiest thing would be to use the loopback interface (127.0.0.1). However, you can still do (2).
  2. If you're connecting from another machine, you need to pick the accessible IP address of your server from that machine. As said above, if your server is only connected to one network, this will simply be the IP address of the server. Within an internal network this will often be something like 192.168.x.y, or 10.0.x.y—but it doesn't have to.
  3. If you're connecting from a different network which uses a gateway to access your server, then you will need to set up port forwarding in the relevant routers so that when they receive connection to port X, they will know to internally transfer it to your server.
Yam Marcovic
  • 7,953
  • 1
  • 28
  • 38
1

As a server programmer, you decide the port on which to listen, but not the address.

The internet address is provided by your internet provider, or 127.0.0.1 to test on your own machine.

There are plenty of web pages on internet that provide tools to tell you your current public address (search for What is my Ip).

Most of the "home" internet routers implement NAT: they have a single internet address and map them to many device, that carry the Port number to be changed (your port 80 become port (e.g.) 2345 for outside). To allows a client from outside your home to access your server, you are required to configure your router to map the server port, so for example your public port 80 map to your server port 80.

With that said, you should be able to connect your client to your server through an address and port.

If then you want to use a name (example.org) instead of an IP (93.184.216.34), a Domain Name Server is used. But that is another topic.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • Thanks for your answer, about the address of the server, I know that, but I should specify the scope which the INADDR_ANY does. – Mostafa Talebi Feb 22 '16 at 13:33