当前位置:  开发笔记 > 编程语言 > 正文

Java中可靠的UDP协议实现 - 为什么会这样?

如何解决《Java中可靠的UDP协议实现-为什么会这样?》经验,为你挑选了0个好方法。

我目前正在使用可靠UDP协议的Java实现,可在此处找到.该项目绝对没有教程,所以我发现很难找出问题.

我已经设置了客户端和服务器.服务器在localhost:1234上运行,客户端在localhost:1235上运行.服务器首先建立,并循环侦听连接 -

try {
            ReliableSocket clientSocket = server.socket.accept();
            InetSocketAddress clientAddress = (InetSocketAddress) clientSocket.getRemoteSocketAddress();

            Logger.getLogger("ServerConnectionListener").info("New Connection from "+
                    clientAddress.getHostName()+":"+clientAddress.getPort()+" Processing...");
            LessurConnectedClient client = new LessurConnectedClient(clientSocket);
            ClientCommunicationSocketListener listener = new ClientCommunicationSocketListener(this, client);
            clientSocket.addListener(listener);
        } catch (Exception e) {
            e.printStackTrace();
        }

建立连接后,它会为该套接字上的事件创建一个侦听器 -

class ClientCommunicationSocketListener implements ReliableSocketListener {
    ServerConnectionListener connectionListener;
    LessurConnectedClient client;

    public ClientCommunicationSocketListener(ServerConnectionListener connectionListener, LessurConnectedClient client){
        this.connectionListener = connectionListener;
        this.client = client;
    }

    @Override
    public void packetReceivedInOrder() {
        connectionListener.server.handlePacket(client);
    }

    @Override
    public void packetReceivedOutOfOrder() {
        connectionListener.server.handlePacket(client);
    }
}

收到数据包后,它会将其传递给server.handlePacket,后者执行打印"Packet Received!"的调试程序.

我的客户端如此连接到服务器 -

LessurClient client = new LessurClient();
        InetSocketAddress a = (InetSocketAddress) server.getSocket().getLocalSocketAddress();
        Logger.getLogger("client-connector").info("Trying to connect to server "+
                a.getAddress().toString()+":"+
                a.getPort());
        client.connect(a.getAddress(), a.getPort());
// LessurClient.connect
    public void connect(InetAddress address, int port){
            try {
                socket = new ReliableSocket(address, port, InetAddress.getLocalHost(), 1235);
                isConnected = true;
                Logger.getLogger("LessurClient").info("Connected to server "+address.getHostAddress()+":"+port);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

我已经链接了我的代码所以当我按下'Z'键时,它会向服务器发送一个数据包,因为 -

public void sendPacket(GamePacket packet){
        if(!isConnected){
            Logger.getLogger("LessurClient").severe("Can't send packet. Client is not connected to any server.");
            return;
        }
        try {
            OutputStream o = socket.getOutputStream();
            o.write(packet.getData());
            o.flush();
            Logger.getLogger("LessurClient").info("Sending Packet with data \""+packet.getData()+"\" to server "+socket.getInetAddress().toString()+":"+socket.getPort());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

我的问题是,在发送32个数据包后,服务器不再接收数据包,并且在发送64个数据包后,它会崩溃.我已经调查了代码,看起来它与数据包没有从接收队列中删除相关联,就像我将ReliableSocket.java:1815中的_recvQueueSize变量从32更改为40时,我现在可以发送40个数据包没有东西出错了.

有人可以帮我识别这个问题吗?我整天都在看代码.

推荐阅读
放ch养奶牛
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有