If you're using a DatagramSocket, every packet that you receive will contain the address and port from which it was sent.
Example
DatagramPacket packet = null;
// Receive next packet
myDatagramSocket.receive(packet);
// Print address + port
System.out.println ("Packet from : " +
packet.getAddress().getHostAddress() + ':' + packet.getPort());
If you're using a ServerSocket, then every socket connection you accept will contain similar information.
Example
Socket mySock = myServerSocket.accept();
// Print address + port
System.out.println ("Connection from : " + mySock.getInetAddress().getHostAddress() + ':' + mySock.getPort());
No comments:
Post a Comment
Note: only a member of this blog may post a comment.