Browse Source

Finished first version of echo server

server
Thomas Johnson 3 years ago
parent
commit
036005c12b
  1. 31
      server/EchoServer.java

31
server/EchoServer.java

@ -9,7 +9,6 @@ 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
@ -30,11 +29,28 @@ public class EchoServer
try
{
Socket sock = ss.accept();
new ProtoIn(new DataInputStream(sock.getInputStream()), new EchoHandler(new ProtoOut(new DataOutputStream(sock.getOutputStream()))));
System.out.println("accepted");
DataInputStream dis = new DataInputStream(sock.getInputStream());
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
ProtoOut pos = new ProtoOut(dos);
new ProtoIn(dis, new EchoHandler(pos)).init();
} catch (IOException e)
{ e.printStackTrace(); }
}
}
public static void main(String args[])
{
int port = 25519;
if (args.length > 0)
{
try
{
port = Integer.parseInt(args[0]);
} catch (Exception e) {}
}
new EchoServer(port);
}
}
class EchoHandler implements ProtoHandler
@ -49,6 +65,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handleError(String username, byte[] msg)
{
System.out.println("error " + username);
try
{
dispatch.sendError("echo_" + username, msg);
@ -58,6 +75,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handleJoin(String username)
{
System.out.println("join " + username);
try
{
dispatch.sendJoin("echo_" + username);
@ -67,6 +85,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handlePart(String username)
{
System.out.println("part " + username);
try
{
dispatch.sendPart("echo_" + username);
@ -76,6 +95,11 @@ class EchoHandler implements ProtoHandler
@Override
public void handleListing(String username, ArrayList<String> names)
{
System.out.println("listing " + username);
for (String n: names)
{
System.out.println(" " + n);
}
try
{
dispatch.giveUserListing("echo_" + username, names);
@ -85,6 +109,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handleListingRequest(String username)
{
System.out.println("listing request " + username);
try
{
dispatch.requestUserList("echo_" + username);
@ -94,6 +119,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handleMessage(String username, String message)
{
System.out.println("message " + username + ": " + message);
try
{
dispatch.sendMessage("echo_" + username, message);
@ -103,6 +129,7 @@ class EchoHandler implements ProtoHandler
@Override
public void handleFile(String username, String filename, byte[] data)
{
System.out.println("file " + username + " " + filename);
try
{
dispatch.sendFile("echo_" + username, filename, data);

Loading…
Cancel
Save