Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.net.*;
- class ServerThread implements Runnable {
- Thread t;
- public ServerThread(Socket socket) throws IOException {
- t = new Thread(this);
- t.start();
- try {
- System.out.println("Przyjęto połączenie: " + socket.toString());
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
- while (true) {
- String str = in.readLine();
- if (str.equals(("koniec"))) {
- break;
- }
- System.out.println("ECHO: " + str);
- out.println(str);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- System.out.println("Zamykamy...");
- socket.close();
- }
- }
- @Override
- public void run() {
- }
- }
- public class Main {
- public static final int PORT = 45000;
- public static void main(String[] args) throws IOException {
- ServerSocket s = new ServerSocket(PORT);
- System.out.println("Serwer uruchomiony" + s.toString());
- try {
- while (true) {
- Socket socket = s.accept();
- ServerThread st = new ServerThread(socket);
- st.run();
- }
- } finally {
- s.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement