My follow-on code from reading the TCP/IP Sockets in Java book
A simple example demonstrating the creation of an InetAddress
and retrieving the metadata around that connection.
A single argument can be provided with an IP address or a DNS resolvable name.
Example use:
gradle inetaddress --args="192.168.1.1"
gradle inetaddress --args="www.google.com"
Demonstrating a basic TCP echo client using a TCP Socket
connection (on port 7 unless specified).
In order for this to work, the client needs to connect to a server; you can do this easily by running the following in a terminal window:
socat -v tcp-l:7,fork exec:'/bin/cat'
You can also use netcat
to create the server for this (installed as part of the nmap
package:
ncat -e /bin/cat -k -l 7
To test the server from the command-line - in a separate terminal session, run:
echo hello | netcat localhost 7
To run the example:
gradle tcpecho --args="localhost hello"
Demonstrating a basic TCP echo Server using a TCP ServerSocket
connection
To run the Server (on TCP Port 7)
gradle tcpechoserver --args="7"
To test with a client, run this in a new terminal tab/window:
echo hello | netcat localhost 7
lsof -i 4tcp:8080 -sTCP:LISTEN