import java.net.*; import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; public class Client { BufferedReader in; PrintWriter out; InetAddress addr; Socket socket; String name; Gui g; Client(Frame f){ g = new Gui(f); } class Gui{ TextArea serv; TextField cl; Gui (Frame f){ f.setLayout(new BorderLayout()); serv = new TextArea(20,10); serv.setEditable(false); cl = new TextField(30); f.add("Center",serv); f.add("South",cl); cl.addActionListener(new SrvL()); } class SrvL implements ActionListener{ public void actionPerformed(ActionEvent e){ try{ String st=cl.getText(); send(st); cl.setText(""); serv.append(receive()+"\n"); } catch (Exception ex){ System.out.println("exception: "+ex); System.out.println("closing..."); try{ socket.close(); } catch (Exception expt){ System.out.println(expt); } } } } } public void init()throws IOException{ try{ String server = null; InetAddress addr = InetAddress.getByName(server); System.out.println("addr = " + addr); socket = new Socket(addr, MultiClientServerName.PORT); System.out.println("socket = " + socket); BufferedReader sin = new BufferedReader( new InputStreamReader(System.in)); in = new BufferedReader( new InputStreamReader(socket.getInputStream())); // Output is automatically flushed // by PrintWriter: out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); System.out.print("Your name: "); name = sin.readLine(); System.out.println("sending name to server"); out.println(name); }catch (Exception e){ System.out.println("exception: "+e); System.out.println("closing..."); socket.close(); } } void send(String s){ if(s.length()==0){ out.println("END"); System.out.println("closing..."); try{ socket.close(); } catch (Exception expt){ System.out.println(expt); } System.exit(0); } else out.println(name+": "+s); } String receive()throws IOException{ return in.readLine(); } public static void main(String[] args )throws IOException{ JFrame frame =new JFrame(); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); Client cl = new Client(frame); cl.init(); frame.setTitle(cl.name); frame.setSize(300,500); frame.setVisible(true); } } |
//:
MultiClientServerName.java // A server that uses multithreading to handle // any number of clients with names. import java.io.*; import java.net.*; class ServeOneClient extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; private String nameCl; public ServeOneClient(Socket s) throws IOException { socket = s; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Enable auto-flush: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) ), true); // If any of the above calls throw an // exception, the caller is responsible for // closing the socket. Otherwise the thread // will close it. nameCl=""; //initialisation start(); // Calls run() } public void run() { try { nameCl=in.readLine(); } catch (IOException e) { } System.out.println("new client "+nameCl ); try { while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " + str); out.println(str); } System.out.println("closing client "+ nameCl ); } catch (IOException e) { } finally { try { socket.close(); } catch(IOException e) {} } } } public class MultiClientServerName { static final int PORT = 8081; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Server Started"); try { while(true) { // Blocks until a connection occurs: Socket socket = s.accept(); try { new ServeOneClient(socket); } catch(IOException e) { // If it fails, close the socket, // otherwise the thread will close it: socket.close(); } } } finally { s.close(); } } } |