-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePacketLibraryTest2.java
More file actions
41 lines (31 loc) · 1.32 KB
/
SimplePacketLibraryTest2.java
File metadata and controls
41 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package test;
import java.io.IOException;
import java.net.UnknownHostException;
import com.luneruniverse.simplepacketlibrary.Client;
import com.luneruniverse.simplepacketlibrary.Server;
import com.luneruniverse.simplepacketlibrary.packets.Packet;
import com.luneruniverse.simplepacketlibrary.packets.PrimitivePacket;
public class SimplePacketLibraryTest2 {
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
// Create the server and client
Server server = new Server(31415);
Client client = new Client("localhost", 31415);
// Start the server and client
server.start();
client.start();
// Handle packets from the client
server.addPacketListener((packet, connection, wait) -> {
if (packet instanceof PrimitivePacket) {
PrimitivePacket castedPacket = (PrimitivePacket) packet;
if (castedPacket.isString() && castedPacket.getValue().equals("ping"))
connection.reply(packet, new PrimitivePacket("pong"));
}
});
// Send and receive a packet from the server
Packet packet = client.sendPacketWithResponse(new PrimitivePacket("ping"));
System.out.println(((PrimitivePacket) packet).getValue());
// Stop the server
server.close();
// The client is automatically disconnected and will close too
}
}