0

My goal is making a TCP/IP connection in a sender <-> server -> receiver fashion.

I have a server, sender and receiver. The server initial part looks like:

int main(int argc, char *argv[]){
  int welcomeSocket, senderSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  if (2 != argc) {
       fprintf(stderr, "Usage: %s <port>\n", argv[0]);
           exit(1);

  }
  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  welcomeSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(atoi(argv[1]));
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

//int len=20;
//char sbuffer[len];

//inet_ntop(AF_INET, &(serverAddr.sin_addr), sbuffer, len);
//printf("address:%s\n",sbuffer);

  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  senderSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

//~~~~Some more code~~~
}

however this sets up the server ip address as 0.0.0.0. I have no clue whether this is correct or not, but all until now I had manually set up the ip address as 127.0.1.1 which is what I get when I type hostname -i in the terminal.

So, currently I am having my sender connect to the same IP address, but since I am working all files in the same computer I don't know if it will work across other computers in the network with this weird ip address 0.0.0.0. Can someone clarify (and maybe help fix) this small issue to me? I have tried reading other solutions that try to explain the difference between 0.0.0.0 and 127.0.0.1 but I couldn't find anything related to the performance on the connection/communication between server and sender in a TCP/IP connection.

And then the IPv4 address is listed as 129.15.78.12 in my system settings, but then again, not sure which one should be used for the server or receiver.

Yokhen
  • 4,180
  • 8
  • 27
  • 50
  • `0.0.0.0` is equivalent to `INADDR_ANY` which means you want to receive packets sent to every IP address associated with your computer. If you used `INADDR_LOOPBACK` (`127.0.0.1`), then packets from the outside wouldn't be accepted. Whether this is good or not depends on what you want to accomplish. – user4520 Oct 31 '15 at 15:39
  • You should look at this post http://stackoverflow.com/questions/3655723/is-0-0-0-0-a-valid-ip-address – terence hill Oct 31 '15 at 15:42
  • @szczurcio My ultimate goal is have the sender connect to the server, send and receive information from and to the server, and eventually send information to the server, which subsequently would send it to the receiver. Does that clarify my goals a bit? – Yokhen Oct 31 '15 at 15:43
  • @terencehill I did previously to posting, but it results very confusing. If I have a many computers in the same network, then would they be able to communicate or not? – Yokhen Oct 31 '15 at 15:45
  • Are all programs going to be running on one computer? If yes, you can use `INADDR_LOOPBACK` without problems, otherwise go with `INADDR_ANY`. It'll probably make more sense if I explain that the _loopback_ address is always that _of your own computer_; in other words, if you send something "to" that address, you send it to yourself. – user4520 Oct 31 '15 at 15:47
  • About my first comment, it should be "...every *IPv4* address". Can't edit it anymore so I'm posting it here. – user4520 Oct 31 '15 at 15:47
  • @szczurcio In theory not all programs will be running on the same computer, so I suppose I will be sticking to INADDR_ANY – Yokhen Oct 31 '15 at 15:48
  • Reserved address have special uses. For example, private network address such 192.168.x.x are not routed. 0.0.0.0 is the default route as defined by the IP protocol. You cannot use 0.0.0.0 as the IP address, however your server can listen to the IP 0.0.0.0 which means listen an all the interfaces. – terence hill Oct 31 '15 at 16:18

1 Answers1

1

The difference is that by using INADDR_ANY you bind the service to all interfaces, as explained in

http://man7.org/linux/man-pages/man7/ip.7.html

and

understanding INADDR_ANY for socket programming - c

Other than that, if you keep this config any computer trying to reach your server will connect if it uses a valid external IP address, like the one you mention you see in the system settings. Hope this clarifies your question.

Community
  • 1
  • 1
Horacio75
  • 21
  • 1
  • 9