Socket socket = new Socket("localhost", 8080); OutputStream out = socket.getOutputStream(); out.write("Hello, server!".getBytes()); InputStream in = socket.getInputStream(); byte[] buffer = new byte[1024]; in.read(buffer); System.out.println(new String(buffer)); socket.close();
Socket socket = new Socket(); socket.connect(new InetSocketAddress("localhost", 8080), 10000); InputStream in = socket.getInputStream(); byte[] buffer = new byte[1024]; in.read(buffer); System.out.println(new String(buffer)); socket.close();In both examples, the java.net package is used to access the Socket class.