diff --git a/src/main/java/ru/molokoin/ExchangeServer.java b/src/main/java/ru/molokoin/ExchangeServer.java index fdaa8ec..24badd6 100644 --- a/src/main/java/ru/molokoin/ExchangeServer.java +++ b/src/main/java/ru/molokoin/ExchangeServer.java @@ -1,138 +1,5 @@ package ru.molokoin; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -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 static ServerSocket serverSocket; - public static Socket socket; - - ExchangeServer() throws Exception{ - isActive = true; - serverSocket = new ServerSocket(port); - } - ExchangeServer (int port) throws Exception{ - isActive = true; - setPort(port); - serverSocket = new ServerSocket(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() throws Exception{ - isActive = false; - } - - public void connect() throws Exception{} - - //запуск сервера - @Override - public void run(){ - System.out.printf("%s started... \n", Thread.currentThread().getName()); - try { - System.out.println("Server started at port: " + port + " ..."); - do { - if (isActive){ - // ожидаем подключения - Socket socket = serverSocket.accept(); - System.out.println("Client connected ..."); - num++; - - // для подключившегося клиента открываем потоки - // чтения и записи - try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); - PrintWriter output = new PrintWriter(socket.getOutputStream())) { - - // ждем первой строки запроса - while (!input.ready()); - - // считываем и печатаем все что было отправлено клиентом - System.out.println(); - String firstLine = null; - String querry = null; - while (input.ready()) { - String line = input.readLine(); - if (firstLine == null){ - firstLine = line; - if(firstLine.startsWith("GET")){ - querry = "GET"; - } - if(firstLine.startsWith("POST")){ - querry = "POST"; - } - System.out.println("firstLine: " + firstLine); - System.out.println("querry: " + querry); - } - //System.out.println(line); - } - - // отправляем ответ - System.out.println("отправка ответа от сервера:"); - String path = new File("").getAbsolutePath(); - String fileName = "upload.http"; - path = path + "/src/main/webapp/"; - File file = new File(path + fileName); - //String text = ""; - try (BufferedReader br = new BufferedReader(new FileReader(file))){ - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); - output.println(line); - //text = text + " " + line; - } - output.flush(); - } - catch (IOException e) { - e.printStackTrace(); - } - - // по окончанию выполнения блока try-with-resources потоки, - // а вместе с ними и соединение будут закрыты - System.out.println("Client disconnected!"); - System.out.println("num: " + num); - } - } - else { - System.out.printf("%s finished... \n", Thread.currentThread().getName()); - serverSocket.close(); - socket.close(); - return; //завершение потока - } - } while (true); - //serverSocket.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - - public static void main(String[] args) { - try { - new ExchangeServer().start(); - } catch (Exception e) { - System.out.println(e); - } - } - +public class ExchangeServer { + } diff --git a/src/main/java/ru/molokoin/MainFrameController.java b/src/main/java/ru/molokoin/MainFrameController.java index fc5573d..77a8d7a 100644 --- a/src/main/java/ru/molokoin/MainFrameController.java +++ b/src/main/java/ru/molokoin/MainFrameController.java @@ -8,6 +8,7 @@ import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField; +import ru.molokoin.example.ExchangeServer; public class MainFrameController implements Initializable{ @FXML diff --git a/src/main/java/ru/molokoin/example/ExchangeServer.java b/src/main/java/ru/molokoin/example/ExchangeServer.java new file mode 100644 index 0000000..b1b895a --- /dev/null +++ b/src/main/java/ru/molokoin/example/ExchangeServer.java @@ -0,0 +1,138 @@ +package ru.molokoin.example; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +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 static ServerSocket serverSocket; + public static Socket socket; + + public ExchangeServer() throws Exception{ + isActive = true; + serverSocket = new ServerSocket(port); + } + ExchangeServer (int port) throws Exception{ + isActive = true; + setPort(port); + serverSocket = new ServerSocket(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() throws Exception{ + isActive = false; + } + + public void connect() throws Exception{} + + //запуск сервера + @Override + public void run(){ + System.out.printf("%s started... \n", Thread.currentThread().getName()); + try { + System.out.println("Server started at port: " + port + " ..."); + do { + if (isActive){ + // ожидаем подключения + Socket socket = serverSocket.accept(); + System.out.println("Client connected ..."); + num++; + + // для подключившегося клиента открываем потоки + // чтения и записи + try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); + PrintWriter output = new PrintWriter(socket.getOutputStream())) { + + // ждем первой строки запроса + while (!input.ready()); + + // считываем и печатаем все что было отправлено клиентом + System.out.println(); + String firstLine = null; + String querry = null; + while (input.ready()) { + String line = input.readLine(); + if (firstLine == null){ + firstLine = line; + if(firstLine.startsWith("GET")){ + querry = "GET"; + } + if(firstLine.startsWith("POST")){ + querry = "POST"; + } + System.out.println("firstLine: " + firstLine); + System.out.println("querry: " + querry); + } + //System.out.println(line); + } + + // отправляем ответ + System.out.println("отправка ответа от сервера:"); + String path = new File("").getAbsolutePath(); + String fileName = "upload.http"; + path = path + "/src/main/webapp/"; + File file = new File(path + fileName); + //String text = ""; + try (BufferedReader br = new BufferedReader(new FileReader(file))){ + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + output.println(line); + //text = text + " " + line; + } + output.flush(); + } + catch (IOException e) { + e.printStackTrace(); + } + + // по окончанию выполнения блока try-with-resources потоки, + // а вместе с ними и соединение будут закрыты + System.out.println("Client disconnected!"); + System.out.println("num: " + num); + } + } + else { + System.out.printf("%s finished... \n", Thread.currentThread().getName()); + serverSocket.close(); + socket.close(); + return; //завершение потока + } + } while (true); + //serverSocket.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + public static void main(String[] args) { + try { + new ExchangeServer().start(); + } catch (Exception e) { + System.out.println(e); + } + } + +} diff --git a/src/main/java/ru/molokoin/example/TestServerNIO.java b/src/main/java/ru/molokoin/example/TestServerNIO.java new file mode 100644 index 0000000..faaeffc --- /dev/null +++ b/src/main/java/ru/molokoin/example/TestServerNIO.java @@ -0,0 +1,19 @@ +package ru.molokoin.example; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +public class TestServerNIO { + public static void main(String[] args) throws IOException { + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.socket().bind(new InetSocketAddress(9999)); + serverSocketChannel.configureBlocking(false); + while(true){ + SocketChannel socketChannel = serverSocketChannel.accept(); + + //do something with socketChannel... +} + } +} diff --git a/src/main/java/ru/molokoin/example/servernio/IServerAdminFace.java b/src/main/java/ru/molokoin/example/servernio/IServerAdminFace.java new file mode 100644 index 0000000..5a087ae --- /dev/null +++ b/src/main/java/ru/molokoin/example/servernio/IServerAdminFace.java @@ -0,0 +1,13 @@ +package ru.molokoin.example.servernio; + +public interface IServerAdminFace { + //передаем адрес хоста + public abstract void initHost(String host); + //передаем номер порта для запуска сервера + public abstract void initPort(int port); + //запуск сервера + public abstract void start(); + //остановка сервера + public abstract void stop(); + +} diff --git a/src/main/java/ru/molokoin/example/servernio/IServerUserFace.java b/src/main/java/ru/molokoin/example/servernio/IServerUserFace.java new file mode 100644 index 0000000..0dbb4be --- /dev/null +++ b/src/main/java/ru/molokoin/example/servernio/IServerUserFace.java @@ -0,0 +1,5 @@ +package ru.molokoin.example.servernio; + +public interface IServerUserFace { + +} diff --git a/src/main/java/ru/molokoin/example/servernio/Server.java b/src/main/java/ru/molokoin/example/servernio/Server.java new file mode 100644 index 0000000..9bdacdc --- /dev/null +++ b/src/main/java/ru/molokoin/example/servernio/Server.java @@ -0,0 +1,28 @@ +package ru.molokoin.example.servernio; + +public class Server implements IServerUserFace, IServerAdminFace{ + Server(){} + public static void main(String[] args) { + + } + @Override + public void start() { + // TODO Auto-generated method stub + + } + @Override + public void stop() { + // TODO Auto-generated method stub + + } + @Override + public void initHost(String host) { + // TODO Auto-generated method stub + + } + @Override + public void initPort(int port) { + // TODO Auto-generated method stub + + } +} diff --git a/src/main/java/ru/molokoin/example/testFileSystem.java b/src/main/java/ru/molokoin/example/testFileSystem.java new file mode 100644 index 0000000..dd60970 --- /dev/null +++ b/src/main/java/ru/molokoin/example/testFileSystem.java @@ -0,0 +1,50 @@ +package ru.molokoin.example; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class testFileSystem { + public static void main(String[] args) { + try { + FileSystem filesystem = FileSystems.getDefault(); + for (Path rootdir : filesystem.getRootDirectories()) { + System.out.println(rootdir.toString()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + //читаем файл + RandomAccessFile aFile; + try { + String stringPath = "src/main/webapp/upload.http"; + Path path = Paths.get(stringPath); + aFile = new RandomAccessFile(path.toAbsolutePath().toString(), "rw"); + FileChannel inChannel = aFile.getChannel(); + ByteBuffer buf = ByteBuffer.allocate(24); + int bytesRead; + bytesRead = inChannel.read(buf); + while (bytesRead != -1){ + System.out.println("Read " + bytesRead); + buf.flip(); + while(buf.hasRemaining()){ + System.out.print((char) buf.get()); + } + buf.clear(); + bytesRead = inChannel.read(buf); + } + aFile.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } +} + diff --git a/target/classes/ru/molokoin/ExchangeServer.class b/target/classes/ru/molokoin/ExchangeServer.class index 5a1271e..2308d07 100644 Binary files a/target/classes/ru/molokoin/ExchangeServer.class and b/target/classes/ru/molokoin/ExchangeServer.class differ diff --git a/target/classes/ru/molokoin/MainFrameController.class b/target/classes/ru/molokoin/MainFrameController.class index 5f19190..3494a70 100644 Binary files a/target/classes/ru/molokoin/MainFrameController.class and b/target/classes/ru/molokoin/MainFrameController.class differ diff --git a/target/classes/ru/molokoin/example/ExchangeServer.class b/target/classes/ru/molokoin/example/ExchangeServer.class new file mode 100644 index 0000000..d5bf04d Binary files /dev/null and b/target/classes/ru/molokoin/example/ExchangeServer.class differ diff --git a/target/classes/ru/molokoin/example/TestServerNIO.class b/target/classes/ru/molokoin/example/TestServerNIO.class new file mode 100644 index 0000000..4142abc Binary files /dev/null and b/target/classes/ru/molokoin/example/TestServerNIO.class differ diff --git a/target/classes/ru/molokoin/example/servernio/IServerAdminFace.class b/target/classes/ru/molokoin/example/servernio/IServerAdminFace.class new file mode 100644 index 0000000..5051c14 Binary files /dev/null and b/target/classes/ru/molokoin/example/servernio/IServerAdminFace.class differ diff --git a/target/classes/ru/molokoin/example/servernio/IServerUserFace.class b/target/classes/ru/molokoin/example/servernio/IServerUserFace.class new file mode 100644 index 0000000..747c4df Binary files /dev/null and b/target/classes/ru/molokoin/example/servernio/IServerUserFace.class differ diff --git a/target/classes/ru/molokoin/example/servernio/Server.class b/target/classes/ru/molokoin/example/servernio/Server.class new file mode 100644 index 0000000..933830d Binary files /dev/null and b/target/classes/ru/molokoin/example/servernio/Server.class differ diff --git a/target/classes/ru/molokoin/example/testFileSystem.class b/target/classes/ru/molokoin/example/testFileSystem.class new file mode 100644 index 0000000..36ebb49 Binary files /dev/null and b/target/classes/ru/molokoin/example/testFileSystem.class differ