esoe 2 years ago
parent
commit
161096273a
  1. 137
      src/main/java/ru/molokoin/ExchangeServer.java
  2. 1
      src/main/java/ru/molokoin/MainFrameController.java
  3. 138
      src/main/java/ru/molokoin/example/ExchangeServer.java
  4. 19
      src/main/java/ru/molokoin/example/TestServerNIO.java
  5. 13
      src/main/java/ru/molokoin/example/servernio/IServerAdminFace.java
  6. 5
      src/main/java/ru/molokoin/example/servernio/IServerUserFace.java
  7. 28
      src/main/java/ru/molokoin/example/servernio/Server.java
  8. 50
      src/main/java/ru/molokoin/example/testFileSystem.java
  9. BIN
      target/classes/ru/molokoin/ExchangeServer.class
  10. BIN
      target/classes/ru/molokoin/MainFrameController.class
  11. BIN
      target/classes/ru/molokoin/example/ExchangeServer.class
  12. BIN
      target/classes/ru/molokoin/example/TestServerNIO.class
  13. BIN
      target/classes/ru/molokoin/example/servernio/IServerAdminFace.class
  14. BIN
      target/classes/ru/molokoin/example/servernio/IServerUserFace.class
  15. BIN
      target/classes/ru/molokoin/example/servernio/Server.class
  16. BIN
      target/classes/ru/molokoin/example/testFileSystem.class

137
src/main/java/ru/molokoin/ExchangeServer.java

@ -1,138 +1,5 @@
package ru.molokoin; package ru.molokoin;
import java.io.BufferedReader; public class ExchangeServer {
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);
}
}
} }

1
src/main/java/ru/molokoin/MainFrameController.java

@ -8,6 +8,7 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import ru.molokoin.example.ExchangeServer;
public class MainFrameController implements Initializable{ public class MainFrameController implements Initializable{
@FXML @FXML

138
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);
}
}
}

19
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...
}
}
}

13
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();
}

5
src/main/java/ru/molokoin/example/servernio/IServerUserFace.java

@ -0,0 +1,5 @@
package ru.molokoin.example.servernio;
public interface IServerUserFace {
}

28
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
}
}

50
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();
}
}
}

BIN
target/classes/ru/molokoin/ExchangeServer.class

Binary file not shown.

BIN
target/classes/ru/molokoin/MainFrameController.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/ExchangeServer.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/TestServerNIO.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/servernio/IServerAdminFace.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/servernio/IServerUserFace.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/servernio/Server.class

Binary file not shown.

BIN
target/classes/ru/molokoin/example/testFileSystem.class

Binary file not shown.
Loading…
Cancel
Save