Hello I've written a small UDP server program. I know the code to display the hosts IP is easy with the following code:
System.out.println("Listening Port: " + serverSocket.getLocalPort());
System.out.println("IP: " + myIp.getHostAddress());
Is there a way of displaying the IP and port number of the Client who is connected to the server?
When you receive your UDP
DatagramPacket
you can retrieve the distant IP from where the packet originated byDatagramPacket.getAddress()
EDIT If you wish to get the string representation of the IP address, just use
DatagramPacket.getAddress().toString()
.Example:
About that sample:
String clientip = DatagramPacket.getAddress();
why don't you just cast it toString
.Though i don't really have a Java Compiler at the moment. but just try that. Will get back really soon.