|
|
@ -9,41 +9,54 @@ import java.io.PrintWriter; |
|
|
|
import java.net.ServerSocket; |
|
|
|
import java.net.ServerSocket; |
|
|
|
import java.net.Socket; |
|
|
|
import java.net.Socket; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public class ExchangeServer extends Thread{ |
|
|
|
|
|
|
|
private int port = 8081; |
|
|
|
|
|
|
|
private int num = 0; |
|
|
|
|
|
|
|
public static boolean isActive; |
|
|
|
|
|
|
|
public ServerSocket serverSocket; |
|
|
|
|
|
|
|
public Socket socket; |
|
|
|
|
|
|
|
|
|
|
|
public class ExchangeServer{ |
|
|
|
ExchangeServer(){ |
|
|
|
private static int port = 8081; |
|
|
|
isActive = true; |
|
|
|
private static String link = "localhost"; |
|
|
|
|
|
|
|
private static int num = 0; |
|
|
|
|
|
|
|
ServerSocket serverSocket; |
|
|
|
|
|
|
|
Socket socket; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//подготовка компонентов
|
|
|
|
|
|
|
|
public void init(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ExchangeServer (int port){ |
|
|
|
//остановка серера
|
|
|
|
isActive = true; |
|
|
|
public void stop(){ |
|
|
|
setPort(port); |
|
|
|
try { |
|
|
|
} |
|
|
|
serverSocket.close(); |
|
|
|
/** |
|
|
|
//socket.close();
|
|
|
|
* @return the port |
|
|
|
} catch (Exception e) { |
|
|
|
*/ |
|
|
|
System.out.println("Остановить сервер не удалось. Ошибка: " + e); |
|
|
|
public int getPort() { |
|
|
|
} |
|
|
|
return port; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param port the port to set |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setPort(int port) { |
|
|
|
|
|
|
|
this.port = port; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//осановка сервера
|
|
|
|
|
|
|
|
public void finish(){ |
|
|
|
|
|
|
|
isActive = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//запуск сервера
|
|
|
|
//запуск сервера
|
|
|
|
public void start(){ |
|
|
|
@Override |
|
|
|
|
|
|
|
public void run(){ |
|
|
|
|
|
|
|
System.out.printf("%s started... \n", Thread.currentThread().getName()); |
|
|
|
try (ServerSocket serverSocket = new ServerSocket(port)) { |
|
|
|
try (ServerSocket serverSocket = new ServerSocket(port)) { |
|
|
|
System.out.println("Server started at port: " + port + " ..."); |
|
|
|
System.out.println("Server started at port: " + port + " ..."); |
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
while (isActive) { |
|
|
|
// ожидаем подключения
|
|
|
|
// ожидаем подключения
|
|
|
|
Socket socket = serverSocket.accept(); |
|
|
|
Socket socket = serverSocket.accept(); |
|
|
|
System.out.println("Client connected ..."); |
|
|
|
System.out.println("Client connected ..."); |
|
|
|
num++; |
|
|
|
num++; |
|
|
|
|
|
|
|
|
|
|
|
// для подключившегося клиента открываем потоки
|
|
|
|
// для подключившегося клиента открываем потоки
|
|
|
|
// чтения и записи
|
|
|
|
// чтения и записи
|
|
|
|
try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); |
|
|
|
try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); |
|
|
|
PrintWriter output = new PrintWriter(socket.getOutputStream())) { |
|
|
|
PrintWriter output = new PrintWriter(socket.getOutputStream())) { |
|
|
@ -87,17 +100,17 @@ public class ExchangeServer{ |
|
|
|
} |
|
|
|
} |
|
|
|
output.flush(); |
|
|
|
output.flush(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e) { |
|
|
|
catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// по окончанию выполнения блока try-with-resources потоки,
|
|
|
|
// по окончанию выполнения блока try-with-resources потоки,
|
|
|
|
// а вместе с ними и соединение будут закрыты
|
|
|
|
// а вместе с ними и соединение будут закрыты
|
|
|
|
System.out.println("Client disconnected!"); |
|
|
|
System.out.println("Client disconnected!"); |
|
|
|
System.out.println("num: " + num); |
|
|
|
System.out.println("num: " + num); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
System.out.printf("%s finished... \n", Thread.currentThread().getName()); |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|
ex.printStackTrace(); |
|
|
|
ex.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
@ -106,5 +119,5 @@ public class ExchangeServer{ |
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) { |
|
|
|
new ExchangeServer().start(); |
|
|
|
new ExchangeServer().start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|