Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/telamon/libmetasrv
A small general purpose TCP/IP networking framework
https://github.com/telamon/libmetasrv
Last synced: about 1 month ago
JSON representation
A small general purpose TCP/IP networking framework
- Host: GitHub
- URL: https://github.com/telamon/libmetasrv
- Owner: telamon
- Created: 2012-03-19T14:29:15.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2012-03-21T21:17:11.000Z (almost 13 years ago)
- Last Synced: 2024-04-29T22:21:53.373Z (8 months ago)
- Language: Java
- Homepage:
- Size: 188 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# libmetasrv
This is a small framework for setting up a tcp/ip
server/client communication that I wrote back in 2007.
It's pretty easy to useSimpleTest.java:
public class SimpleTest {
public static void main(String[] argv) throws MetaSrvException {
if (argv.length < 2) { // No address provided, act as server.
SimpleServer myServer = new SimpleServer();
myServer.start();
} else { // Address and port provided as first argument in format "example.com:1234"
String[] addrPort = argv[1].split(":");
SimpleServer myClient = new SimpleServer();
myClient.clientMode(addrPort[0], Integer.parseInt(addrPort[1]));
}
}static class SimpleClient extends MetaClient {
BufferedReader input;
public SimpleClient(MetaServer ms, Socket sock) {
super(ms, sock);
input = new BufferedReader(new java.io.InputStreamReader(iStream));
}@Override
public void process() {
try {
if (!mServer.clientMode) {
// I'm a server, I Broadcast incoming messages to all connected clients.
mServer.broadcast(input.readLine().getBytes());
} else {
// I'm a client, I print all incoming messages
System.out.println(input.readLine());
}
} catch (IOException ex) {
Logger.getLogger(SimpleTest.class.getName()).log(Level.SEVERE, null, ex);
}
}@Override
public boolean handshake() {
try {
if (!mServer.clientMode) {
// This instance is the server
String greeting = input.readLine();
if (greeting.equalsIgnoreCase("hello")) {
return true;
} else {
return false;
}
} else {
// This is instance the client
oStream.write("hello\n".getBytes());
return true;
}
} catch (IOException ex) {
Logger.getLogger(SimpleTest.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}static class SimpleServer extends MetaServer {
@Override
protected void resetInstance() {
}@Override
public MetaClient newClient(Socket sock) {
return new SimpleClient(this, sock);
}
}
}