Speer is a library used to perform P2P communication easier. It uses the plain Java Socket API to allow communicating between two devices.
You can consult the wiki for a detailed documentation
Take a look at Fandem a P2P file sharing app. This project uses speer to send/receive files between devices in P2P, and also to discover Fandem peers with UDP.
Server-side
import com.tambapps.p2p.speer.PeerServer
try (PeerServer server = peerServer();
PeerConnection connection = server.accept()) {
connection.writeUTF("Hello! do you want to talk to me?");
if ("yes".equals(connection.readUTF())) {
talk(connection);
}
}
Client-side
try (PeerConnection connection = PeerConnection.from(peer)) {
connection.readUTF()
connection.writeUTF("yes")
talk(connection)
}
This library is hosted on Jitpack
You can import it to your project by adding the Jitpack repository
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then add the dependency
<dependency>
<groupId>com.github.tambapps</groupId>
<artifactId>speer</artifactId>
<version>1.0</version>
</dependency>