简体   繁体   English

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

[英]Java Socket Server Not Receiving Messages?

Alright, so I'm not sure if there really is an error or not. 好吧,所以我不确定是否确实存在错误。 I've created a listening server using Java and NetBeans, and am trying to get the protocol to work. 我已经使用Java和NetBeans创建了一个监听服务器,并且正在尝试让协议工作。 However, for some reason it doesn't want to accept incoming messages. 但是,由于某种原因,它不想接受传入的消息。 It will accept incoming connections and set up the object used for handling a client, but just wont receive any messages. 它将接受传入的连接并设置用于处理客户端的对象,但不会收到任何消息。

I'm not sure what's going on, am I doing something wrong? 我不确定发生了什么,我做错了什么? Also, I already have port 7777 port forwarded, and I'm doing testing using "127.0.0.1". 此外,我已经转发了端口7777端口,我正在使用“127.0.0.1”进行测试。

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

I currently have 3 classes: 我目前有3个班级:

The main class, GMServer: 主要类GMServer:

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

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

The CreateListener class: 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();
    }
}

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

and then the listenParser class (where it should be handling all traffic from clients): 然后是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();
        }
    }
}

You are reading lines. 你在读线。 Are you sending lines? 你在发送线路吗? Very common mistake. 很常见的错误。 readLine() will block until it receives a newline, or EOS. readLine()将阻塞,直到收到换行符或EOS。

Speaking of EOS, the first thing you must do with the result of readLine() is check it for null, and if so close the socket and break out of the reading loop. 说到EOS,你必须对readLine()的结果做的第一件事是检查它是否为null,如果是这样,则关闭套接字并打破读取循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM