In UDP I used:
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println ("From: " + IPAddress + ":" + port);
System.out.println ("Message: " + sentence);
I'm confused about TCP. How do servers get the client's IP and port number using TCP?
Look at Socket.getInetAddress()
and Socket.getPort()
.
They use sockets.
Example:
Socket server = new ServerSocket(6666)
Socket connection = server.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
The source and destination IP address and port are present in every TCP segment or UDP datagram.