Java NIO - 使用SocketChannels接收数据的问题

时间:2011-04-18 23:35:57

标签: java networking nio


我正在使用Java NIO编写简单的即时消息。它的工作正常,客户端不会收到已登录的联系人的消息 这是主要的服务器进程方法:

public void process() throws IOException, ClassNotFoundException{
    while (true){
        selector.select();
        Set keys = selector.selectedKeys();
        Iterator it = keys.iterator();
        while (it.hasNext()){
            SelectionKey key = (SelectionKey)it.next();
            it.remove();
            if (key.isAcceptable()){
                /*configuring incoming channel to be non-blocking 
                and adding it to the clients set*/
            }
            if (key.isReadable()) {
                SocketChannel client = (SocketChannel)key.channel();
                /*Message class contains its type, source user name and data.
                getMessage() method reads message from SocketChannel*/
                Message m = getMessage(client);    
                switch (m.getType()) {
                    case LOGIN_REQUESTED:
                        /*Accept or reject the client log in name
                        in case of accepting add user with its status
                        to Map users*/
                        break;
                    case CONTACTS_REQUESTED:
                        /*Here is the problem, client gets only one message
                        sendMessage() writes the buffer with serialized 
                        message to clients channels*/
                        for (String name : users.keySet()) {
                            sendMessage(client, MessageType.CONTACTS_REQUESTED, 
                                name, users.get(name).toString());
                        }
                        break;
                   //Other messages are processing
                }
            }
        }
    }
}

处理传入消息的客户端方法:

private void processIncomingMessages() 
    throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais;
    ObjectInputStream ois;
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    while (true){
        selector.select();
        Set keys = selector.selectedKeys();
        Iterator it = keys.iterator();
        while (it.hasNext()){
            SelectionKey key = (SelectionKey)it.next();
            it.remove();
            if (key.isReadable()){
                SocketChannel sc = (SocketChannel)key.channel();
                buffer.clear();
                if (sc.read(buffer) != -1){
                    buffer.flip();
                    bais = new ByteArrayInputStream(buffer.array());
                    ois = new ObjectInputStream(bais);
                    Message m = (Message)ois.readObject();
                    /*My castom event serves to notify GUI to update 
                    its contact list. In case of sending CONTACTS_REQUESTED 
                    messages, it gets only one (first?) of them*/
                    fireNetworkEvent(m);
                }
            }
        }
    }
}

其他消息,单独发送给客户端没有任何问题。 如果有任何问题,请询问。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

我的猜测是在客户端......

 if (sc.read(buffer) != -1){

不保证会读取您的整个邮件,它可能不会读取任何内容,某些内容或多条消息。

套接字是字节流,如果你想读取数据包(就像你一样),你需要打包流。

一种方法是在数据包的长度前加上,并确保在尝试处理之前读取整个数据包。

另一种方法是以一种方式编写数据包,使其具有您读取的唯一终结符,直到获得终结符。

相关问题