Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. class ServerThread implements Runnable {
  7.  
  8.     Thread t;
  9.  
  10.     public ServerThread(Socket socket) throws IOException {
  11.         t = new Thread(this);
  12.         t.start();
  13.         try {
  14.             System.out.println("Przyjęto połączenie: " + socket.toString());
  15.  
  16.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  17.  
  18.             PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  19.  
  20.             while (true) {
  21.                 String str = in.readLine();
  22.  
  23.                 if (str.equals(("koniec"))) {
  24.                     break;
  25.                 }
  26.  
  27.                 System.out.println("ECHO: " + str);
  28.  
  29.                 out.println(str);
  30.             }
  31.         } catch (IOException e) {
  32.             e.printStackTrace();
  33.         } finally {
  34.             System.out.println("Zamykamy...");
  35.             socket.close();
  36.         }
  37.     }
  38.  
  39.     @Override
  40.     public void run() {
  41.     }
  42. }
  43.  
  44.  
  45. public class Main {
  46.  
  47.     public static final int PORT = 45000;
  48.  
  49.     public static void main(String[] args) throws IOException {
  50.         ServerSocket s = new ServerSocket(PORT);
  51.  
  52.         System.out.println("Serwer uruchomiony" + s.toString());
  53.  
  54.         try {
  55.  
  56.             while (true) {
  57.                 Socket socket = s.accept();
  58.  
  59.                 ServerThread st = new ServerThread(socket);
  60.                 st.run();
  61.             }
  62.  
  63.         } finally {
  64.             s.close();
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement