1 changed files with 123 additions and 0 deletions
@ -0,0 +1,123 @@ |
|||
package server; |
|||
|
|||
import proto.ProtoHandler; |
|||
import proto.ProtoIn; |
|||
import proto.ProtoOut; |
|||
|
|||
import java.io.DataInputStream; |
|||
import java.io.DataOutputStream; |
|||
import java.io.IOException; |
|||
import java.net.ServerSocket; |
|||
import java.net.Socket; |
|||
import java.sql.PseudoColumnUsage; |
|||
import java.util.ArrayList; |
|||
|
|||
public class EchoServer |
|||
{ |
|||
public EchoServer(int port) |
|||
{ |
|||
ServerSocket ss; |
|||
try |
|||
{ |
|||
ss = new ServerSocket(port); |
|||
} catch (IOException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
return; |
|||
} |
|||
while (true) |
|||
{ |
|||
try |
|||
{ |
|||
Socket sock = ss.accept(); |
|||
new ProtoIn(new DataInputStream(sock.getInputStream()), new EchoHandler(new ProtoOut(new DataOutputStream(sock.getOutputStream())))); |
|||
} catch (IOException e) |
|||
{ e.printStackTrace(); } |
|||
} |
|||
} |
|||
} |
|||
|
|||
class EchoHandler implements ProtoHandler |
|||
{ |
|||
private ProtoOut dispatch; |
|||
|
|||
public EchoHandler(ProtoOut dispatch) |
|||
{ |
|||
this.dispatch = dispatch; |
|||
} |
|||
|
|||
@Override |
|||
public void handleError(String username, byte[] msg) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.sendError("echo_" + username, msg); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleJoin(String username) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.sendJoin("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handlePart(String username) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.sendPart("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleListing(String username, ArrayList<String> names) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.giveUserListing("echo_" + username, names); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleListingRequest(String username) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.requestUserList("echo_" + username); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleMessage(String username, String message) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.sendMessage("echo_" + username, message); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleFile(String username, String filename, byte[] data) |
|||
{ |
|||
try |
|||
{ |
|||
dispatch.sendFile("echo_" + username, filename, data); |
|||
} catch (Exception e) {} |
|||
} |
|||
|
|||
@Override |
|||
public void handleUnknownCommand(int length, byte id, String username) |
|||
{ |
|||
System.out.println("Unknown command " + id + " from " + username); |
|||
} |
|||
|
|||
@Override |
|||
public void handleClose() |
|||
{ |
|||
System.out.println("closed"); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue