Advertisement
orzecho1994

Serwer_wątki

Mar 31st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package serwer;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. /**
  7.  *
  8.  * @author student
  9.  */
  10. class Odbieranie extends Thread
  11. {
  12.     InputStream in;
  13.    
  14.     Odbieranie(InputStream in)
  15.     {
  16.         this.in=in;
  17.     }
  18.    
  19.    
  20.     public void run()
  21.     {
  22.         try
  23.         {
  24.             while(true)
  25.             {
  26.                 int k = 0;
  27.  
  28.                 StringBuffer sb = new StringBuffer();
  29.  
  30.  
  31.  
  32.                 while((k=in.read())!=-1  && k != '\n' )
  33.                 {
  34.                     sb.append((char)k);
  35.                 }
  36.                 String wiadomosc = sb.toString().trim();
  37.  
  38.                 System.out.println(wiadomosc);
  39.             }
  40.         }catch (Exception e){}
  41.     }
  42. }
  43.  
  44.  
  45.  
  46. public class Serwer {
  47.  
  48.     /**
  49.      * @param args the command line arguments
  50.      */
  51.     public static void main(String[] args) {
  52.         // TODO code application logic here
  53.         try
  54.         {
  55.            
  56.         ServerSocket serwer = new ServerSocket(4444);
  57.         Socket gniazdo = serwer.accept();
  58.        
  59.         InputStream in = gniazdo.getInputStream();
  60.         OutputStream out = gniazdo.getOutputStream();
  61.        
  62.        
  63.         Odbieranie o1 = new Odbieranie(in);
  64.         o1.start();
  65.        
  66.         BufferedReader klawiatura = new BufferedReader(new InputStreamReader(System.in));
  67.        
  68.         while (true)
  69.         {
  70.           String linia = klawiatura.readLine();
  71.           out.write(linia.getBytes());
  72.           out.write("\r\n".getBytes());
  73.         }
  74.        
  75.        
  76.         }catch(Exception e){}
  77.        
  78.        
  79.     }
  80.    
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement