Read and write through socket : Socket Client « Network « Java Tutorial






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

public class Main {
  public static void whois(String query, String server) throws IOException {
    Socket sock = new Socket(server, 43);
    int c = 0;
    
    OutputStream os = sock.getOutputStream();
    InputStream is = sock.getInputStream();
    query += "\r\n";
    os.write(query.getBytes("iso8859_1"));

    while (c != -1) {
      c = is.read();
      if (c != -1)
        System.out.println((char) c);
    }
  }

  public static void main(String[] args) throws Exception {
    String hostname = "whois.networksolutions.com";
    whois("query", hostname);
  }
}








19.12.Socket Client
19.12.1.Create a socket without a timeout
19.12.2.Create a socket with a timeout
19.12.3.Read and write through socket
19.12.4.Send string to each connected client
19.12.5.Daytime Client
19.12.6.Echo Client
19.12.7.Whois Client
19.12.8.Time Client
19.12.9.Finger Client
19.12.10.Compressed socket
19.12.11.Day time Client (Getting the time)
19.12.12.Zip socket