Socket: getRemoteSocketAddress()


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MainClass {
  public static void main(String[] args) throws Exception {
    String serverName = args[0];
    int port = Integer.parseInt(args[1]);

    try {
      System.out.println("Connecting to " + serverName + " on port " + port);
      Socket client = new Socket(serverName, port);

      System.out.println("Just connected to " + client.getRemoteSocketAddress());

      OutputStream outToServer = client.getOutputStream();
      DataOutputStream out = new DataOutputStream(outToServer);
      out.writeUTF("Hello from " + client.getLocalSocketAddress());

      InputStream inFromServer = client.getInputStream();
      DataInputStream in = new DataInputStream(inFromServer);
      System.out.println("Server says " + in.readUTF());

      client.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
Home 
  Java Book 
    Networking  

Socket:
  1. Socket
  2. new Socket(InetAddress address, int port) throws IOException
  3. Socket: connect(SocketAddress endpoint, int timeout) throws IOException
  4. Socket: getInputStream() throws IOException
  5. Socket: getLocalSocketAddress()
  6. Socket: getOutputStream() throws IOException
  7. Socket: getRemoteSocketAddress()
  8. Socket: readUTF() and writeUTF(String str)