Advertisement
Guest User

Untitled

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