// import java.util.*;importjava.io.*;importjava.net.*;publicclassServer{privateServerSocketserver_socket;publicServer(intport_no)throwsIOException{server_socket=newServerSocket(port_no);System.out.println("Waiting for client to connect");while(true){try{Socketconnection=server_socket.accept();System.out.println("Connected to client");ClientHandlerclient_handler=newClientHandler(connection);client_handler.start();}catch(Exceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args)throwsIOException{newServer(5000);}}classClientHandlerextendsThread{publicClientHandler(Socketconnection)throwsIOException{try{DataOutputStreamout=newDataOutputStream(connection.getOutputStream());Stringserver_message="Hi there! Enter something";out.writeUTF(server_message);DataInputStreamin=newDataInputStream(connection.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));in.close();out.close();connection.close();}catch(Exceptione){e.printStackTrace();}}}
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("localhost",5000);}}
Last Updated: 2023-01-25 ; Contributors: AhmedThahir