sending IP address of machine to server sending IP address of machine to server curl curl

sending IP address of machine to server


Another alternative would be sending a simple UDP datagram packet. The server can obtain the sender's address, upon receipt, equally well.

That requires a little bit less overhead than establishing a TCP connection. Of course, UDP offers no guarantee of deliverability, and an occasional UDP datagram would be lost; but since you're pinging the server regularly that should be fine. More importantly, however, is that a UDP sender's IP address is trivially forged. Whether or not this is an issue for your application is something that only you can determine.

If you're going to stick with TCP, one thing you can do is to establish a socket connection yourself. Let's examine what executing system(), just for the purpose of having curl do a dummy connection, involves:

  1. forking a new child process.

  2. the child process executing /bin/bash, passing it the command to parse.

  3. Bash reading $HOME/.bashrc, and executing any commands in there. If your shell is something other than bash, the shell will have its own equivalent of a startup script to execute.

  4. The shell forking a new child process.

  5. The child process executing curl.

  6. The loader finding all of the libraries that curl requires, and opening them.

  7. curl now executes. Only then, after all this work, there will be code running to open a socket and attempt to connect to the remote host.

Steps 1 through 6 can be trivially skipped, simply by creating a socket, and connecting, yourself. It's not rocket science. socket(), connect(), close(), that's it. This task does not require HTTP. The server process only needs to socket(), bind(), listen(), and accept(), and then obtain the IP address from the connection. Done.