Vous êtes sur la page 1sur 7

//Server.java import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.

*; public class Server extends JFrame { JTextField txtenter; JTextArea txtadisplay; ObjectOutputStream output; ObjectInputStream input; public Server() { super("SERVER"); Container c=getContentPane(); txtenter=new JTextField(); txtenter.setEnabled(false); txtenter.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { sendData(e.getActionCommand()); } } ); c.add(txtenter,BorderLayout.SOUTH); txtadisplay=new JTextArea(); txtadisplay.setEditable(false); c.add(new JScrollPane(txtadisplay),BorderLayout.CENTER); setSize(300,150); show(); } public void runServer() { ServerSocket ss; Socket s; int counter = 1; try { //create a seversocket ss=new ServerSocket(5000,100); while(true) { //wait for the connection txtadisplay.setText("Waiting for the Connection..."); //establishing connection s=ss.accept(); txtadisplay.append(" Conection "+counter+"received from:"+s.getInetAddress().getHostName()); //getting input/output

output=new ObjectOutputStream(s.getOutputStream()); output.flush(); input=new ObjectInputStream(s.getInputStream()); //processing connection String message="Server>>>Conection Sucessfull..."; output.writeObject(message); output.flush(); txtenter.setEnabled(true); do { message=(String) input.readObject(); txtadisplay.append(" "+message); txtadisplay.setCaretPosition(txtadisplay.getText().length()); }while(!message.equals("CLIENT>>>TERMINATE")); txtadisplay.append(" User Terminated Connection..."); output.close(); input.close(); s.close(); ++counter; } } catch(Exception e) { } } public void sendData(String s) { try { output.writeObject("SERVER>>>"+s); txtadisplay.append(" SERVER>>>"+s); } catch(Exception e) { } } public static void main(String args[]) { Server ser=new Server(); ser.addWindowListener( new WindowAdapter(){ public void WindowClosing(WindowEvent e) { System.exit(0); } } ); ser.runServer(); }

} //Client.java import java.io.*; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Client extends JFrame { JTextField txtenter; JTextArea txtadisplay; ObjectOutputStream output; ObjectInputStream input; String message=""; public Client() { super("CLIENT"); Container c=getContentPane(); txtenter=new JTextField(); txtenter.setEnabled(false); txtenter.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { sendData(e.getActionCommand()); } }); c.add(txtenter,BorderLayout.SOUTH); txtadisplay=new JTextArea(); txtadisplay.setEditable(false); c.add(new JScrollPane(txtadisplay),BorderLayout.CENTER); setSize(300,150); show(); } public void runClient() { Socket s; try { txtadisplay.setText("Attempting Connection..."); //establishing connection s=new Socket("localhost",5000); txtadisplay.append(" Connected to:"+s.getInetAddress().getHostName()); output=new ObjectOutputStream(s.getOutputStream()); output.flush(); input=new ObjectInputStream(s.getInputStream()); txtenter.setEnabled(true); //processing connection

do { message=(String)input.readObject(); txtadisplay.append(" "+message); txtadisplay.setCaretPosition(txtadisplay.getText().length()); }while(!message.equals("SERVER>>>TERMINATE")); txtadisplay.append(" Closing Connection..."); input.close(); output.close(); s.close(); } catch(Exception e) {} } public void sendData(String s) { try { message=s; output.writeObject("CLIENT>>>"+s); output.flush(); txtadisplay.append(" CLIENT>>>"+s); } catch(Exception e) { } } public static void main(String args[]) { Client cli=new Client(); cli.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } } ); cli.runClient(); } }

Code : // To be run at server side and will act as a server // MainServer.java import java.net.*; import java.io.*; public class MainServer { public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4445); } catch (IOException e) { System.err.println("Could not listen on port: 4445."); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine, outputLine; MainProtocol kkp = new MainProtocol(); // // outputLine = kkp.processInput(null); out.println(outputLine); while ((inputLine = in.readLine()) != null) { if (inputLine.equals("bye")) break; outputLine = kkp.processInput(inputLine); out.println(outputLine); outputLine = null; } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } // Server side component used to execute commands and returning result // MainProtocol.java import java.lang.*; import java.io.*; public class MainProtocol { String sa,sb; // String sb,sa; public String processInput(String args) { try { sb = new String(); sa = new String(); Runtime rt=Runtime.getRuntime(); Process p=rt.exec("cmd /c "+ args);

InputStream is = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); while ((sb = br.readLine()) != null) { sa += sb ; // sa +=" "; } p.waitFor(); System.out.println("process "+p.exitValue()); } catch(Exception e) { System.out.println(e.getMessage()+ "sumit"); } return sa ; } }

// Client side component. Commands are to fired from here only // MainClient.java import java.io.*; import java.net.*; public class MainClient { public static void main(String[] args) throws IOException { Socket kkSocket = null; PrintWriter out = null; BufferedReader in = null; try { kkSocket = new Socket("10.37.32.74", 4445); out = new PrintWriter(kkSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Exception " + e); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String fromServer; String fromUser; // // // // while ((fromUser = stdIn.readLine()) != "hi") { System.out.println("Server: " + fromServer); if (fromServer.equals("Bye.")) break; fromUser = stdIn.readLine();

//

if (fromUser != null) { System.out.println("Client: " + fromUser); out.println(fromUser); fromServer = in.readLine(); System.out.println("Server Response: "+ fromServer); fromServer = null; if (fromUser.equals("bye")) break; } } out.close(); in.close(); stdIn.close(); kkSocket.close(); }

Vous aimerez peut-être aussi