public class server { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here // read arguments int portNum = java.lang.Integer.parseInt(args[0]); // the second argument is // a port number. // The server just opens a ServerSocket using the specified port number, // and then goes into an infinite loop. // try connecting to the server ServerSocket s = null; try { s = new ServerSocket(portNum); } catch (Exception ex0 ) { System.out.println("Exception opening socket"); System.exit(1); // exit with error. } while(true){ // the server waits (using an accept call) for a client to connect. Socket clientConnection = null; try { clientConnection = s.accept(); } catch (Exception ex1) { System.out.println("Exception with accept"); System.exit(1); // exit with error. } System.out.println("Client connected to Server"); // the socket provides input and output streams we can treat // like any other input and output stream. DataInputStream netInput = null; DataOutputStream netOutput = null; try { netInput = new DataInputStream(clientConnection.getInputStream()); netOutput = new DataOutputStream(clientConnection.getOutputStream()); } catch (java.io.IOException ex1) { System.out.println(" Unable to create input or output stream for network connection."); System.exit(1); // exit with error. } int input; // input from network try { do { input = netInput.read(); netOutput.write(input); System.out.print((char)input); } while(input!=-1); } catch (Exception ex3) { System.out.println("IO Exception" ); } System.out.println("Client Disconnect"); } } // end of main. }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)