Java Socket class

Description

Java Socket class

// Demonstrate Sockets. 
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Main {
   public static void main(String args[]) throws Exception {
      int c;//ww w  . j  a v  a 2  s.  c o  m

      // Create a socket connected to internic.net, port 43.
      Socket s = new Socket("whois.internic.net", 43);

      // Obtain input and output streams.
      InputStream in = s.getInputStream();
      OutputStream out = s.getOutputStream();

      // Construct a request string.
      String str = "google.com";
      // Convert to bytes.
      byte buf[] = str.getBytes();

      // Send request.
      out.write(buf);

      // Read and display response.
      while ((c = in.read()) != -1) {
         System.out.print((char) c);
      }
      s.close();
   }
}



PreviousNext

Related