0
This commit is contained in:
2022-09-08 01:25:43 +03:00
commit becc7d0afd
5 changed files with 255 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.vscode/
.idea/
out/
*.iml

View File

@@ -0,0 +1,95 @@
package socket.client;
import java.io.*;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
public class ClientApp {
private static Socket client;
private static Thread inThread;
private static Thread outThread;
public static void main(String[] args) throws IOException {
client = new Socket("127.0.0.1", 8888);
inThread = new Thread(ClientApp::inputHandle);
outThread = new Thread(ClientApp::outputHandle);
inThread.start();
outThread.start();
try {
inThread.join();
outThread.join();
} catch (InterruptedException e) {
closeConnect();
}
}
private static void inputHandle() {
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
closeConnect();
return;
}
try {
while (!client.isClosed() || !Thread.currentThread().isInterrupted()) {
String message = in.readLine();
if (message != null) {
System.out.printf("INCOMMING MESSAGE: %s\n", message);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
closeConnect();
}
}
private static void outputHandle() {
BufferedWriter out;
try {
out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
closeConnect();
return;
}
try {
while (!client.isClosed() || !Thread.currentThread().isInterrupted()) {
String pingJson = String.format("{\"ping\": %d}\n", System.currentTimeMillis());
out.write(pingJson);
out.flush();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
closeConnect();
}
}
private static void closeConnect() {
inThread.interrupt();
outThread.interrupt();
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,118 @@
package socket.server;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
public class ServerApp {
private static final List<ClientHandler> clients = new CopyOnWriteArrayList<>();
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8888, 50, InetAddress.getByName("localhost"));
server.setSoTimeout(5 * 1000);
while (true) {
System.out.println("[i] wait client...");
Socket client;
try {
client = server.accept();
} catch (SocketTimeoutException e) {
continue;
}
clients.add(new ClientHandler(client));
System.out.printf("[i] client connected (%d/50).\n", clients.size());
}
}
public static class ClientHandler {
private final Socket client;
private final Thread inThread;
private final Thread outThread;
public ClientHandler(Socket client) {
this.client = client;
this.inThread = new Thread(this::inputHandle);
this.outThread = new Thread(this::outputHandle);
this.inThread.start();
this.outThread.start();
}
private void inputHandle() {
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
closeConnect();
return;
}
try {
while (!client.isClosed() || !Thread.currentThread().isInterrupted()) {
String message = in.readLine();
if (message != null) {
System.out.printf("INCOMMING MESSAGE: %s\n", message);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
closeConnect();
}
}
private void outputHandle() {
BufferedWriter out;
try {
out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e1) {
e1.printStackTrace();
closeConnect();
return;
}
try {
while (!client.isClosed() || !Thread.currentThread().isInterrupted()) {
String pingJson = String.format("{\"ping\": %d}\n", System.currentTimeMillis());
out.write(pingJson);
out.flush();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
closeConnect();
}
}
private void closeConnect() {
this.inThread.interrupt();
this.outThread.interrupt();
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
clients.remove(this);
System.out.printf("[i] client close connection (%d/50)\n", clients.size());
}
}
}

25
node/index.js Normal file
View File

@@ -0,0 +1,25 @@
import net from 'net'
const host = '127.0.0.1'
const port = 8888
const backlog = 50
const server = net.createServer()
server.listen(port, host, backlog)
server.on('connection', (client) => {
console.log('[i] client connected')
client.on('data', (data) => {
console.log('INCOMMING MESSAGE: ' + data)
})
let timerId = setInterval(() => {
client.write(JSON.stringify({"ping": Date.now()}) + "\n")
}, 1000)
client.on('close', () => {
clearTimeout(timerId)
console.log('[i] client close connection')
})
})

13
node/package.json Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "node",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"run": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}