ServerSocketChannel与ServerSocket Java

时间:2018-09-17 07:14:48

标签: java sockets serversocket socketchannel

我已经开始在预先开发的Java应用程序上工作,在那里他们正在处理Java套接字。该项目的主要概念是从Java套接字服务器上的客户端获取数据并进行处理。

该项目是使用Java NIO ServerSocketChannel开发的。

现在在这种情况下,我每秒可以从客户端获取大约70-80条记录。如果我使用简单的Java ServerSocket(仅出于测试目的),则输出会放大到大约800次。

这两者为什么有如此大的差异? 以及如何修改ServerSocketChannel以模仿ServerSocket?

这是我的主要处理部分,

SocketChannel client = (SocketChannel) key.channel();
                        if (!key.isReadable()) {
                            key.cancel();
                            client.close();
                            continue;
                        }

                        int[] k = (int[]) key.attachment();

                        if (k[2] == 0) {

                            ByteBuffer lengthBuffer = ByteBuffer.allocate(2);

                            int bytesRead = -1;
                            if (!client.socket().isClosed() && key.isValid()) {
                                bytesRead = client.read(lengthBuffer);
                            }

                            if (bytesRead == -1) {
                                key.cancel();
                                client.close();
                                continue;
                            }
                            lengthBuffer.flip();
                            byte[] bytes = new byte[2];
                            lengthBuffer.get(bytes, 0, 2);
                            short length = DRMSDecoder.getShort(bytes);


                            k[0]++;
                            k[1] = length;
                            k[2] = 1;
                            key.attach(k);
                            lengthBuffer.clear();
                        } 
                        else {
                            try {

                                int length = k[1];
                                ByteBuffer dataBuffer = ByteBuffer.allocate(length);
                                dataBuffer.clear();
                                System.gc();

                                int bytesRead = -1;
                                if (!client.socket().isClosed() && key.isValid()) {
                                    bytesRead = client.read(dataBuffer);
                                }

                                if (bytesRead == -1) {
                                    key.cancel();
                                    client.close();
                                    continue;
                                }

                                dataBuffer.flip();

                                Hashtable<String, Object> request = decoder.decode(dataBuffer);
                                short call_type = (Short) request.get("Call Type");

                                if (request.get("Call Type") != null && (((Short) request.get("Call Type")) == 78 || ((Short) request.get("Call Type")) == 80)) {


                                } else if (request.get("Call Type") != null && (((Short) request.get("Call Type")) == 79)) {
                                    key.cancel();
                                    client.close();

                                } 
                                    String message = (String) request.get("Buffer");

                                    handleMessage(message);

                                }
                                k[0]++;
                                k[2] = 0;
                                key.attach(k);
                                lastMessageProcessed++;

                                dataBuffer.clear();

                            } catch (Exception e) {

// System.out.println(e);                                 }                             }

1 个答案:

答案 0 :(得分:0)

区别在于阻塞I / O与非阻塞I / O

参考:https://medium.com/coderscorner/tale-of-client-server-and-socket-a6ef54a74763