Mega Code Archive

 
Categories / Java Tutorial / Network
 

Read and write through socket

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);   } }