|
|
@ -10,8 +10,10 @@ 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 { |
|
|
|
public class ExchangeServer{ |
|
|
|
private static int port = 8081; |
|
|
|
private static int port = 8081; |
|
|
|
|
|
|
|
private static String link = "localhost"; |
|
|
|
|
|
|
|
private static int num = 0; |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) { |
|
|
|
try (ServerSocket serverSocket = new ServerSocket(port)) { |
|
|
|
try (ServerSocket serverSocket = new ServerSocket(port)) { |
|
|
@ -21,6 +23,7 @@ public class ExchangeServer { |
|
|
|
// ожидаем подключения
|
|
|
|
// ожидаем подключения
|
|
|
|
Socket socket = serverSocket.accept(); |
|
|
|
Socket socket = serverSocket.accept(); |
|
|
|
System.out.println("Client connected ..."); |
|
|
|
System.out.println("Client connected ..."); |
|
|
|
|
|
|
|
num++; |
|
|
|
|
|
|
|
|
|
|
|
// для подключившегося клиента открываем потоки
|
|
|
|
// для подключившегося клиента открываем потоки
|
|
|
|
// чтения и записи
|
|
|
|
// чтения и записи
|
|
|
@ -32,8 +35,19 @@ public class ExchangeServer { |
|
|
|
|
|
|
|
|
|
|
|
// считываем и печатаем все что было отправлено клиентом
|
|
|
|
// считываем и печатаем все что было отправлено клиентом
|
|
|
|
System.out.println(); |
|
|
|
System.out.println(); |
|
|
|
|
|
|
|
String firstLine = null; |
|
|
|
|
|
|
|
String querry = null; |
|
|
|
while (input.ready()) { |
|
|
|
while (input.ready()) { |
|
|
|
System.out.println(input.readLine()); |
|
|
|
String line = input.readLine(); |
|
|
|
|
|
|
|
if (firstLine == null){ |
|
|
|
|
|
|
|
firstLine = line; |
|
|
|
|
|
|
|
if(firstLine.startsWith("GET")){ |
|
|
|
|
|
|
|
querry = "GET"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("firstLine: " + firstLine); |
|
|
|
|
|
|
|
System.out.println("querry: " + querry); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
System.out.println(line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// отправляем ответ
|
|
|
|
// отправляем ответ
|
|
|
@ -42,14 +56,17 @@ public class ExchangeServer { |
|
|
|
String fileName = "upload.html"; |
|
|
|
String fileName = "upload.html"; |
|
|
|
path = path + "/src/main/webapp/"; |
|
|
|
path = path + "/src/main/webapp/"; |
|
|
|
File file = new File(path + fileName); |
|
|
|
File file = new File(path + fileName); |
|
|
|
|
|
|
|
//String text = "";
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader(file))){ |
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader(file))){ |
|
|
|
String line; |
|
|
|
String line; |
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
System.out.println(line); |
|
|
|
System.out.println(line); |
|
|
|
output.println(line); |
|
|
|
output.println(line); |
|
|
|
|
|
|
|
//text = text + " " + line;
|
|
|
|
} |
|
|
|
} |
|
|
|
output.flush(); |
|
|
|
output.flush(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
catch (IOException e) { |
|
|
|
catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
@ -57,6 +74,7 @@ public class ExchangeServer { |
|
|
|
// по окончанию выполнения блока try-with-resources потоки,
|
|
|
|
// по окончанию выполнения блока try-with-resources потоки,
|
|
|
|
// а вместе с ними и соединение будут закрыты
|
|
|
|
// а вместе с ними и соединение будут закрыты
|
|
|
|
System.out.println("Client disconnected!"); |
|
|
|
System.out.println("Client disconnected!"); |
|
|
|
|
|
|
|
System.out.println("num: " + num); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|