Skip to content

Commit feba990

Browse files
committed
22
1 parent a081cc7 commit feba990

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<groupId>com.andmal</groupId>
88
<artifactId>java-examples</artifactId>
99

10-
<version>1.0.2</version>
10+
<version>1.0.3</version>
1111

1212
<properties>
13-
<java.version>21</java.version>
13+
<java.version>22</java.version>
1414
<junit.version>5.8.1</junit.version>
1515
</properties>
1616

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.anma.java.core.net.socket;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
8+
public class MultithreadedTCPServer extends TCPServerEx {
9+
public void run(Socket data) {
10+
try {
11+
DataOutputStream dos =
12+
new DataOutputStream(data.getOutputStream());
13+
14+
dos.writeByte(TypeServerConstants.WELCOME);
15+
16+
DataInputStream dis =
17+
new DataInputStream(data.getInputStream());
18+
19+
while (true) {
20+
21+
byte b = dis.readByte();
22+
23+
if (b != TypeServerConstants.GET_STRING_REQUEST) {
24+
System.out.println("Client sent unknown request " + b);
25+
continue;
26+
}
27+
28+
dos.writeByte(TypeServerConstants.GET_STRING_RESPONSE);
29+
dos.writeUTF("Thisisateststring");
30+
dos.flush();
31+
}
32+
} catch (Exception e) {
33+
System.out.println("Client terminating: " + e);
34+
return;
35+
}
36+
}
37+
38+
public static void main(String[] args) throws IOException {
39+
TCPServerEx ts = new TCPServerEx();
40+
ts.startServer(Integer.parseInt(args[0]));
41+
System.out.println("Server ready and waiting...");
42+
}
43+
}
44+
45+
class TypeServerConstants {
46+
public final static byte WELCOME = 0;
47+
public final static byte GET_STRING_REQUEST = 1;
48+
public final static byte GET_STRING_RESPONSE = 2;
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.anma.java.core.net.socket;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
public class TCPServerEx implements Cloneable, Runnable {
8+
9+
Thread runner = null;
10+
ServerSocket server = null;
11+
Socket data = null;
12+
private boolean done = false;
13+
14+
public synchronized void startServer(int port) throws IOException {
15+
if (runner == null) {
16+
server = new ServerSocket(port);
17+
runner = new Thread(this);
18+
runner.start();
19+
}
20+
}
21+
22+
public synchronized void stopServer() {
23+
done = true;
24+
runner.interrupt();
25+
}
26+
27+
protected synchronized boolean getDone() {
28+
return done;
29+
}
30+
31+
public void run() {
32+
if (server != null) {
33+
while (!getDone()) {
34+
try {
35+
Socket datasocket = server.accept();
36+
TCPServerEx newSocket = (TCPServerEx) clone();
37+
newSocket.server = null;
38+
newSocket.data = datasocket;
39+
newSocket.runner =
40+
new Thread(newSocket);
41+
newSocket.runner.start();
42+
} catch (Exception e) {
43+
}
44+
}
45+
} else {
46+
run(data);
47+
}
48+
}
49+
50+
public void run(Socket data) {
51+
}
52+
}

0 commit comments

Comments
 (0)