Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. class Serwer extends Thread {
  2.     protected int port;
  3.     static Vector<ServerThread> klienci;
  4.     public Serwer(int port) {
  5.         this.port = port;
  6.     }
  7.     public void rozeslij(String wiadomosc, Socket omin){
  8.         for(int i = 0; i<klienci.size();i++){
  9.             if(klienci.get(i).channel==omin)
  10.                 continue;
  11.        
  12.         klienci.get(i).wyslij(wiadomosc);
  13.         }
  14.     }
  15.     @Override public void run() {
  16.         ServerSocket serwer = null;
  17.         try {
  18.             serwer = new ServerSocket(port);
  19.         } catch (IOException e) {}
  20.        
  21.         ServerThread t;
  22.         while(true) {
  23.             try {
  24.                 Socket channel = serwer.accept();
  25.                 t = new ServerThread(channel);
  26.                 klienci.add(t);
  27.                 t.start();
  28.             } catch (IOException e) {}
  29.         }
  30.     }
  31.    
  32.    
  33. }
  34. class ServerThread extends Thread {
  35.     protected Socket channel;
  36.     protected PrintWriter out;
  37.     public BufferedReader in = null;
  38.    
  39.  
  40.     public ServerThread(Socket s) {
  41.         channel = s;
  42.         try {
  43.             in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  44.             out = new PrintWriter(s.getOutputStream(), true);
  45.         } catch (Exception e) {}
  46.     }
  47.  
  48.     public void wyslij(String wiadomosc) {
  49.         try {
  50.             wiadomosc=wiadomosc+"\n";
  51.             out.print(wiadomosc);
  52.             out.flush();
  53.            
  54.             } catch (Exception e) {}
  55.     }
  56.  
  57.     public String odbierz() {
  58.         String wiadomosc="";
  59.         try {
  60.                 wiadomosc = in.readLine();
  61.                
  62.         } catch (Exception e) {}
  63.        
  64.         return wiadomosc;
  65.     }
  66.     protected void rozlacz() {
  67.         try {
  68.             channel.close();
  69.         } catch(Exception ex) {}
  70.     }
  71.    
  72.  
  73.    
  74.  
  75.     @Override public void run() {
  76.         String wiadomosc;
  77.         while (channel.isConnected()) {
  78.             wiadomosc = odbierz();
  79.             System.out.print(wiadomosc);
  80.                 if (wiadomosc!="") {
  81.                     wyslij(wiadomosc);
  82.                         wiadomosc="";
  83.                 }
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89. public class SerwerTest{
  90. public static void main(String[] args){
  91.     Serwer serwer = new Serwer(8000);
  92.     serwer.start();
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement