Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 1.01 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How create servers and clients with socket?
  2. public class Server {
  3.  
  4.     public Event receive(int port) {
  5.  
  6.         Evento event = null;
  7.         try {
  8.             ServerSocket ss = new ServerSocket(port);
  9.  
  10.             Socket s = ss.accept();
  11.             InputStream is = s.getInputStream();
  12.             ObjectInputStream ois = new ObjectInputStream(is);
  13.  
  14.              evento = (Evento) ois.readObject();
  15.  
  16.             is.close();
  17.             s.close();
  18.             ss.close();
  19.  
  20.         }catch(Exception e){
  21.             System.out.println(e);
  22.         }
  23.  
  24.         return event;
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. public class Client {
  31.     public void send(Event event, int port) {
  32.         try {
  33.             Socket s = new Socket("localhost", 2002);
  34.             OutputStream os = s.getOutputStream();
  35.             ObjectOutputStream oos = new ObjectOutputStream(os);
  36.  
  37.             oos.writeObject(event);
  38.             oos.close();
  39.             os.close();
  40.             s.close();
  41.  
  42.         } catch (Exception e) {
  43.             System.out.println(e);
  44.         }
  45.     }
  46. }