Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字断开时出错_Java_Sockets - Fatal编程技术网

Java 套接字断开时出错

Java 套接字断开时出错,java,sockets,Java,Sockets,我制作了一个程序来保持与服务器的连接。它有很多数据包,总是从recv和send发送,从不断开连接(就像一个游戏)。它始终工作正常,但当服务器与客户端失去连接时,会发生以下情况之一: channel.read返回-1。它可以毫无错误地断开连接 channel.write引发异常。它调用closeServer(),channel.readreturn-1并毫无问题地断开连接 channel.read永不返回channel.write的次数太多,它总是抛出异常,但是channel.read永远不会返回

我制作了一个程序来保持与服务器的连接。它有很多数据包,总是从recv和send发送,从不断开连接(就像一个游戏)。它始终工作正常,但当服务器与客户端失去连接时,会发生以下情况之一:

  • channel.read
    返回-1。它可以毫无错误地断开连接

  • channel.write
    引发异常。它调用
    closeServer()
    channel.read
    return-1并毫无问题地断开连接

  • channel.read
    永不返回<调用code>channel.write的次数太多,它总是抛出异常,但是
    channel.read
    永远不会返回

  • channel.read
    永不返回<代码>频道。写入从不引发异常。过了一段时间,
    channel.write
    再也不会返回。它得到堆叠没有错误。有时它永远不会堆叠,它总是写入字节,但连接已经结束

  • channel.read
    从不返回,
    channel.write
    从不引发异常。我手动调用
    closeServer()
    ,但它被堆叠在
    channel.close()上永不返回

  • 我都快疯了。这是源代码:

    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    
    public class Client {
        Boolean close = false;
        SocketChannel channel = null;
        JFrame window;
    
        public Client(JFrame window){
            this.window = window;
        }
        public void send (String s){
            try {
                channel.write(StringToByteBuffer(s));
            } catch (IOException ex) {
                window.error("Error writing data\n");
                closeServer();
            }
        }
    
        public void Connect (){
    
            ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
            InetSocketAddress socketAddress = new InetSocketAddress("xx.xx.xx.xx", 800);
            try {
                channel = SocketChannel.open();
                channel.connect(socketAddress);
            } catch (IOException ex) {
                window.error("Unable to connect to server\n");
            }
    
            send("Login String");
    
            try {
                while ((channel.read(buffer)) != -1) {
                    buffer.flip();
                    proccessBuffer(buffer);
                    buffer.clear();
                    if (close){
                        channel.close();
                        break;
                    }
                }
            } catch (IOException ex) {
                window.error("Error reading data\n");
                closeServer();
            }
            window.setServerOffline();
        }
    
        public void closeServer(){
            try {
                channel.close();
            } catch (IOException ex) {
                window.error("Error closeing socket");
            }
            close = true;
        }
    }
    
    我从主JFrame创建了一个此类线程,如下所示:

    new Thread() {
                    @Override
                    public void run() {
    
                    client = new Client(window);
                    client.Connect();
    
    
                }}.start();
    

    window
    是一个JFrame,
    client
    是client类。

    如何检测并关闭3)、4)和5)上的套接字不确定这里是否是这种情况,但是否可能得到空值?尝试并检查从服务器读取时是否存在空值,看看会发生什么。您没有打印实际的异常消息,这是一个重大错误。因此,您不知道错误实际上是什么,因此我们也不知道。修复您的代码以打印实际错误,而不是您编造的错误,然后在此处重新运行并报告实际错误。除非您这样做,否则您根本不可能通过自己或这里的帮助找到任何结果。主要问题是read永远不会返回null或-1。没有错误抛出。有时,write函数从不抛出异常,只是似乎写入了所有字节(有时,函数冻结,永远不会返回)