@ -9,35 +9,48 @@ import java.io.PrintWriter;
@@ -9,35 +9,48 @@ import java.io.PrintWriter;
import java.net.ServerSocket ;
import java.net.Socket ;
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 {
private static int port = 8081 ;
private static String link = "localhost" ;
private static int num = 0 ;
ServerSocket serverSocket ;
Socket socket ;
//подготовка компонентов
public void init ( ) {
ExchangeServer ( ) {
isActive = true ;
}
//остановка серера
public void stop ( ) {
try {
serverSocket . close ( ) ;
//socket.close();
} catch ( Exception e ) {
System . out . println ( "Остановить сервер не удалось. Ошибка: " + e ) ;
}
ExchangeServer ( int port ) {
isActive = true ;
setPort ( port ) ;
}
/ * *
* @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 ) ) {
System . out . println ( "Server started at port: " + port + " ..." ) ;
while ( true ) {
while ( isActiv e) {
// ожидаем подключения
Socket socket = serverSocket . accept ( ) ;
System . out . println ( "Client connected ..." ) ;
@ -87,7 +100,6 @@ public class ExchangeServer{
@@ -87,7 +100,6 @@ public class ExchangeServer{
}
output . flush ( ) ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
}
@ -98,6 +110,7 @@ public class ExchangeServer{
@@ -98,6 +110,7 @@ public class ExchangeServer{
System . out . println ( "num: " + num ) ;
}
}
System . out . printf ( "%s finished... \n" , Thread . currentThread ( ) . getName ( ) ) ;
} catch ( IOException ex ) {
ex . printStackTrace ( ) ;
}