-2

我在玩多人游戏时遇到问题。现在,我只是想让服务器将当前级别发送到客户端。服务器肯定会发送数据,但它永远不会到达客户端应用程序。

客户端代码:

    public void run() {
    while(true)
    {
        try {
            sel.select();
            Set readyKeys = sel.selectedKeys();
            Iterator itr = readyKeys.iterator();
            while(itr.hasNext())
            {
                SelectionKey key = (SelectionKey) itr.next();
                itr.remove();
                SocketChannel ch = (SocketChannel) key.channel();
                if(key.isReadable())
                {
                    inputbuf.clear();
                    long bytesRead = ch.read(inputbuf);
                    inputbuf.flip();
                    byte[] test = inputbuf.array();
                    {
                        for(int i=0; i < inputbuf.remaining(); i++)
                        {
                           System.out.print(test[i]+" ");
                        }
                    }
                    byte cmd = 0;
                    cmd = inputbuf.get();
                    switch(cmd)
                    {
                        case 1:
                           //do something
                        case 2:
                           //do something else
                        break;
                    }
                }
                else if(key.isConnectable())
                {
                    sc.finishConnect();
                    sc.register(sel, SelectionKey.OP_READ);
                }
            }
        } catch (IOException ex) {
            Buildsim.error("Error handling input from server", ex);
        }
    }
}

服务器代码:

public RemotePlayer(SocketChannel s)
{
    sc = s;
    try {
        sel = Selector.open();
        sc.configureBlocking(false);
        sc.register(sel, SelectionKey.OP_WRITE);
        socket = sc.socket();
        ip = socket.getInetAddress().toString();
        System.out.println("New connection from "+ip);
        inputbuf = ByteBuffer.allocate(1048576);
        outputbuf = ByteBuffer.allocate(1048576);
        inputbuf.clear();
        outputbuf.clear();
    }
    catch(Exception e)
    {
        Buildsim.error("Connection error: ", e);
    }
    sendLevel();
}
public void sendObject(GameObject obj)
{
     //Sends a packet of the object, then does the same for the object's children
}
public void sendLevel()
{
    try {
        outputbuf.put((byte) 0x01);
        outputbuf.flip();
        sc.write(outputbuf);
        sendObject(Buildsim.w.parts.get(0)); //root object
    }
    catch(IOException e)
    {
        Buildsim.error("Error sending level to "+ip, e);
    }
}

唯一到达客户端的是字节 0x01(表示电平变化)。

提前致谢。

4

1 回答 1

0

以您提供给我们的信息量,我们只能提供全局提示,例如:您是否确保您的套接字流没有被缓冲并且flush()在信息写入之后您是否确保?

数据可能正在等待填充以发送的缓冲区中等待。

编辑:

您将 SocketChannel 设置为非阻塞模式,所以您假设在发送端负责刷新是正确的,我假设客户端不缓冲他们的输入?

问题可能是SocketChannel.write()不能保证写入整个缓冲区,但可能会提前返回。您需要在 while 循环中包装写入以跟踪写入的字节,以确保您的数据实际写入通道。引用 Javadoc:

除非另有说明,否则写入操作仅在写入所有 r 个请求字节后才会返回。 某些类型的通道,取决于它们的状态,可能只写入一些字节,或者根本不写入。例如,处于非阻塞模式的套接字通道不能写入比套接字输出缓冲区中空闲的字节多的字节。

于 2010-01-31T21:58:55.447 回答