public class IoEchoServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(7000); while (active) { Socket socket = serverSocket.accept(); // blocking InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); int read; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { // blocking os.write(bytes, 0, read); // blocking } socket.close(); } serverSocket.close(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter