Java套接字服务器没有收到消息?

时间:2012-03-19 20:31:13

标签: java sockets protocols listener

好吧,所以我不确定是否确实存在错误。 我已经使用Java和NetBeans创建了一个监听服务器,并且正在尝试让协议工作。 但是,由于某种原因,它不想接受传入的消息。 它将接受传入的连接并设置用于处理客户端的对象,但不会收到任何消息。

我不确定发生了什么,我做错了什么?此外,我已经转发了端口7777端口,我正在使用“127.0.0.1”进行测试。

=============================================== =======================

我目前有3个班级:

主要课程,GMServer:

public class GMServer {
public static void main(String[] args) throws IOException {
    CreateListener createListener = new CreateListener("USA Server East",7777,500);
    }
}

=============================================== =======================

CreateListener类:

public final class CreateListener {
private ServerSocket listenSocket = null;
private String serverName = "";
private int serverPort = 7777;
private int serverMax = 500;
private boolean terminate = false;
private double connectID = 0;
private String currentUser = "-1";
private listenParser[] connections;
private int listenArray = 0;
public listenParser[] users;
public int usercount;
private boolean listenerConnected = true;

public double getCID() {
    this.connectID++;
    return this.connectID;
    }

public CreateListener(String sName, int sPort, int sMax) throws IOException {
    //Set the server name, port, and max users.

    this.serverName = sName; this.serverPort = sPort; this.serverMax = sMax;

    //Try to create a listener socket.
    try { this.listenSocket = new ServerSocket(this.serverPort); } //If not successful, output error and end program.
    catch (IOException e) { System.err.println("Could not create a listening server on port "+this.serverPort); System.exit(7777); }

    //Let the console know that the listening server was created successfully.
    System.out.println("Creating Server: \""+serverName+"\" On Port "+serverPort+"...");

    while(listenerConnected) {
        try {
            Socket accepted = this.listenSocket.accept();
            new listenParser(accepted,getCID(),sMax).start();
            } catch (IOException e) {
            //Socket Wasn't Accepted?
            }
        }

    //Close down server after anything should cause the constant loop to end.
    listenSocket.close();
    }
}

=============================================== =======================

然后是listenParser类(它应该处理来自客户端的所有流量):

public class listenParser extends Thread {
public Socket[] global_sockets;
public int connections = 0;
private PrintWriter _out = null;
private BufferedReader _in = null;
private double cid = 0;
private Socket socket = null;
private String currentUser = "";
private int timeout = 0;
private boolean connectionOpen = true;
private int socketTimeout = 0;
private int socketTimeoutMax = 1000;

private String room = "";
private long x = 0;
private long y = 0;

public listenParser(Socket sock, double acID, int maxUsers) {
    super("listenParser");
    this.socket = sock;
    System.out.println("Connected To A Client...");
    }

public String readString(BufferedReader tmpBuffer) {
    try {
        return tmpBuffer.readLine();
        } catch (IOException e) {
        return "";
        }
    }

public String protocol(String arg) {
    String RETURN = "";
    switch(arg) {
        case "login": currentUser = "1"; break;
        case "echo": this._out.println(readString(this._in)); break;
        default: /*Do Nothing*/ break;
        }

    //Only allow access to these commands if the user is logged in.
    if (currentUser!="") {
        switch(arg) {
            default: break;
            }
        }
    return RETURN;
    }

public boolean good() {
    if (this.currentUser.equals("")) {
        return false;
        }
    return true;
    }

@Override
public void run() {
    try {
        _out = new PrintWriter(socket.getOutputStream(),true);
        _in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        connections++; if (connections>500) { connectionOpen = false; }

        while(connectionOpen) {
            try {
                String arguments, reply;

                arguments = this._in.readLine();

                reply = protocol(arguments);

                socketTimeout = 0;
                } catch (IOException e) {
                socketTimeout++;
                if (socketTimeout>socketTimeoutMax) {
                    connectionOpen = false; break;
                    }
                }
            }

        _out.close();
        _in.close();
        socket.close();

        System.out.println("Client Disconnected...");
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你正在读行。你是发送行吗?很常见的错误。 readLine()将阻止,直到收到换行符或EOS。

说到EOS,你必须对readLine()的结果做的第一件事就是检查它是否为null,如果是这样的话,请关闭套接字并跳出读取循环。