import java.io.*; import java.net.*; import java.util.*; public class TextReceiver { public static void main (String[] argv) { try { // Create a listening service for connections at the designated port number. ServerSocket srv = new ServerSocket (9003); // When a connection is made, get the socket. // The method accept() blocks until then. System.out.println ("Server: waiting for a connection ... "); Socket soc = srv.accept (); // At this stage, the connection will have been made. InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Accepted a connection from " + remoteMachine); // We are going to listen, so get an InputStream and wrap a line-reader around it. InputStream in_stream = soc.getInputStream (); LineNumberReader lnr = new LineNumberReader (new InputStreamReader (in_stream)); String line = lnr.readLine (); while (line != null) { System.out.println ("Received: " + line); // Repeat. line = lnr.readLine(); } } catch (IOException e) { System.out.println (e); } } }