importjava.util.*;importjava.io.*;importjava.net.*;publicclassServerextendsThread{privateServerSocketserver_socket;publicServer(intport_no)throwsIOException{while(true){try{server_socket=newServerSocket(port_no);System.out.println("Waiting for client to connect");Socketserver=server_socket.accept();System.out.println("Connected to client");DataOutputStreamout=newDataOutputStream(server.getOutputStream());Stringserver_message="Hi there! Enter something";out.writeUTF(server_message);DataInputStreamin=newDataInputStream(server.getInputStream());Stringclient_reply=in.readUTF();System.out.println("User replied with: "+client_reply);server.close();}catch(Exceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsIOException{Threadt1=newServer(5000);t1.start();}}
importjava.util.*;importjava.io.*;importjava.net.*;publicclassClient{publicClient(Stringhost,intport)throwsIOException{try{Socketclient=newSocket(host,port);System.out.println("Connected to server");DataInputStreamin=newDataInputStream(client.getInputStream());Stringserver_message=in.readUTF();System.out.println(server_message);DataOutputStreamout=newDataOutputStream(client.getOutputStream());Scanneruser_in=newScanner(System.in);Stringclient_reply=user_in.next();out.writeUTF(client_reply);user_in.close();client.close();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsIOException{newClient("localhost",5000);}}
importjava.util.*;importjava.io.*;importjava.net.*;publicclassServerextendsThread{privateServerSocketserver_socket;publicServer(intport_no)throwsIOException{while(true){try{server_socket=newServerSocket(port_no);System.out.println("Waiting for client to connect");Socketserver=server_socket.accept();System.out.println("Connected to client");DataOutputStreamout=newDataOutputStream(server.getOutputStream());Stringserver_message="Hi there! Enter something";out.writeUTF(server_message);DataInputStreamin=newDataInputStream(server.getInputStream());intnum1=Integer.parseInt(in.readUTF());Stringop=in.readUTF();intnum2=Integer.parseInt(in.readUTF());Stringclient_reply="Received: "+num1+op+num2;System.out.println(client_reply);intresult=0;switch(op){case"+":{result=num1+num2;break;}case"-":{result=num1-num2;break;}case"*":{result=num1*num2;break;}case"/":{result=num1/num2;break;}case"^":{result=num1^num2;break;}default:{System.out.println("Invalid Operator");}}out.writeUTF(Integer.toString(result));server.close();}catch(Exceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsIOException{Threadt1=newServer(args[0]);t1.start();}}
importjava.util.*;importjava.io.*;importjava.net.*;publicclassClient{publicClient(Stringhost,intport)throwsIOException{try{Socketclient=newSocket(host,port);System.out.println("Connected to server");DataInputStreamin=newDataInputStream(client.getInputStream());Stringserver_message=in.readUTF();System.out.println(server_message);DataOutputStreamout=newDataOutputStream(client.getOutputStream());Scanneruser_in=newScanner(System.in);intnum1=user_in.nextInt();Stringop=user_in.next();intnum2=user_in.nextInt();out.writeUTF(Integer.toString(num1));out.writeUTF(op);out.writeUTF(Integer.toString(num2));Stringresult=in.readUTF();System.out.println(result);user_in.close();client.close();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsIOException{newClient(args[0],args[1]);}}
change server’s parameters as command-line arguments (using args[0], ...)
Need to use BufferedReader and PrintWriter as the encoding used Telnet is different
importjava.util.*;importjava.io.*;importjava.net.*;publicclassServerextendsThread{privateServerSocketserver_socket;publicServer(intport_no)throwsIOException{while(true){try{server_socket=newServerSocket(port_no);System.out.println("Waiting for client to connect");Socketserver=server_socket.accept();System.out.println("Connected to client");PrintWriterout=newPrintWriter(server.getOutputStream());Stringserver_message="Hi there! Enter something";out.print(server_message);BufferedReaderin=newBufferedReader(newInputStreamReader(server.getInputStream()));Stringclient_reply=in.readLine();System.out.println("User replied with: "+client_reply);server.close();}catch(Exceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsIOException{Threadt1=newServer(5000);t1.start();}}