Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nathsou/socketify
Lightweight Java network library unifying TCP and UDP.
https://github.com/nathsou/socketify
Last synced: about 2 months ago
JSON representation
Lightweight Java network library unifying TCP and UDP.
- Host: GitHub
- URL: https://github.com/nathsou/socketify
- Owner: nathsou
- Created: 2015-08-14T20:52:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-08-19T01:31:31.000Z (over 9 years ago)
- Last Synced: 2024-10-11T20:42:54.472Z (3 months ago)
- Language: Java
- Homepage:
- Size: 195 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Usage
Socketify enables you to create client/server couples using TCP and/or UDP protocols.
The library provides the following classes:
* *TcpClient* and *TcpServer* for the TCP protocol.
* *UdpClient* and *UdpServer* for the UDP protocol.
* *SocketifyClient* and *SocketifyServer* for both protocols.All of those Client/Server couples share the same methods, so it's as easy to create a TCP and UDP couple as just a TCP one !
```java
//The SocketifyServer class accepts an optional 3rd argument representing the UDP port.
SocketifyServer server = new SocketifyServer(1621, 1789); int tcpPort, int udpPort (optional)
server.listen(); //Starts the serverSocketifyClient client = new SocketifyClient("localhost", 1621, 1789); ////String address, int tcpPort, int udpPort (optional)
client.connect();
client.send("Hoy !", ProtocolType.TCP);
server.sendToAll("How are you ?", ProtocolType.UDP);
client.disconnect();
server.close();
```
##Event handling
Server classes fire the following events:
* PacketReceivedEvent
* PacketSentEventClient classes fire the following events:
* ClientConnectedEvent
* ClientDisconnectedEvent
* PacketReceivedEvent
* PacketSentEvent```java
server.addPacketReceivedListener(new PacketReceivedListener() {
@Override
public void PacketReceived(PacketReceivedEvent e) {
System.out.println("Server received (" + e.getProtocol() + "): " + e.getPacket() + " from " + e.getSenderId());
}
});//Using lambda expressions:
server.addPacketReceivedListener(e -> System.out.println("Server received (" + e.getProtocol() + "): " + e.getPacket() + " from " + e.getSenderId()));
```