|
|
@ -9,35 +9,48 @@ 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();
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
System.out.println("Остановить сервер не удалось. Ошибка: " + e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return the port |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
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 ..."); |
|
|
@ -87,7 +100,6 @@ public class ExchangeServer{ |
|
|
|
} |
|
|
|
} |
|
|
|
output.flush(); |
|
|
|
output.flush(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e) { |
|
|
|
catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
@ -98,6 +110,7 @@ public class ExchangeServer{ |
|
|
|
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(); |
|
|
|
} |
|
|
|
} |
|
|
|