tcp server raw implementation
This commit is contained in:
@@ -1,17 +1,28 @@
|
|||||||
package org.lumijiez;
|
package org.lumijiez;
|
||||||
|
|
||||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
import org.lumijiez.core.TcpServer;
|
||||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
TcpServer server = new TcpServer(8080, (message, clientSocket) -> {
|
||||||
// to see how IntelliJ IDEA suggests fixing it.
|
System.out.println("Processing message from " + clientSocket.getInetAddress() + ": " + message);
|
||||||
System.out.printf("Hello and welcome!");
|
|
||||||
|
|
||||||
for (int i = 1; i <= 5; i++) {
|
if (message.equalsIgnoreCase("hello")) {
|
||||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
return "Hello, client!";
|
||||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
} else if (message.equalsIgnoreCase("bye")) {
|
||||||
System.out.println("i = " + i);
|
return "Goodbye!";
|
||||||
|
} else {
|
||||||
|
return "Unknown command.";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
new Thread(server::start).start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(60000);
|
||||||
|
server.stop();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
45
src/main/java/org/lumijiez/core/TcpClientHandler.java
Normal file
45
src/main/java/org/lumijiez/core/TcpClientHandler.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package org.lumijiez.core;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class TcpClientHandler implements Runnable {
|
||||||
|
private final Socket clientSocket;
|
||||||
|
private final TcpServerCallback callback;
|
||||||
|
|
||||||
|
public TcpClientHandler(Socket clientSocket, TcpServerCallback callback) {
|
||||||
|
this.clientSocket = clientSocket;
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
OutputStream out = clientSocket.getOutputStream()) {
|
||||||
|
|
||||||
|
String receivedMessage;
|
||||||
|
while ((receivedMessage = in.readLine()) != null) {
|
||||||
|
System.out.println("Received from client: " + receivedMessage);
|
||||||
|
|
||||||
|
String response = callback.onClientMessage(receivedMessage, clientSocket);
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
out.write((response + "\n").getBytes());
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error handling client: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
clientSocket.close();
|
||||||
|
System.out.println("Client disconnected: " + clientSocket.getInetAddress());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error closing client socket: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
65
src/main/java/org/lumijiez/core/TcpServer.java
Normal file
65
src/main/java/org/lumijiez/core/TcpServer.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package org.lumijiez.core;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class TcpServer {
|
||||||
|
private final int port;
|
||||||
|
private boolean running;
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
private final ExecutorService threadPool;
|
||||||
|
private final TcpServerCallback callback;
|
||||||
|
|
||||||
|
public TcpServer(int port, TcpServerCallback callback) {
|
||||||
|
this.port = port;
|
||||||
|
this.callback = callback;
|
||||||
|
this.running = false;
|
||||||
|
this.threadPool = Executors.newCachedThreadPool();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
try {
|
||||||
|
serverSocket = new ServerSocket(port);
|
||||||
|
running = true;
|
||||||
|
System.out.println("Server started on port " + port);
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
System.out.println("New client connected: " + clientSocket.getInetAddress());
|
||||||
|
|
||||||
|
TcpClientHandler clientHandler = new TcpClientHandler(clientSocket, callback);
|
||||||
|
threadPool.submit(clientHandler);
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (running) {
|
||||||
|
System.out.println("Error accepting client connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error starting server: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
running = false;
|
||||||
|
if (serverSocket != null) {
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
System.out.println("Server stopped.");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error stopping server: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
threadPool.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
7
src/main/java/org/lumijiez/core/TcpServerCallback.java
Normal file
7
src/main/java/org/lumijiez/core/TcpServerCallback.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package org.lumijiez.core;
|
||||||
|
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public interface TcpServerCallback {
|
||||||
|
String onClientMessage(String message, Socket clientSocket);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user