简体   繁体   English

ServerSocketChannel 与 ServerSocket Java

[英]ServerSocketChannel vs ServerSocket Java

I have started my work on a pre-developed Java application, where they are dealing with java sockets.我已经开始在一个预先开发的 Java 应用程序上工作,他们正在处理 Java 套接字。 The main concept of the project is to get data from the client at the java socket server and process it.该项目的主要概念是在java socket服务器端从客户端获取数据并进行处理。

The project has been developed using Java NIO ServerSocketChannel.该项目是使用 Java NIO ServerSocketChannel 开发的。

Now in this scenario, I could able to get around 70-80 records per second from the client.现在在这种情况下,我每秒可以从客户端获取大约 70-80 条记录。 While if I use simple Java ServerSocket ( Just for testing purpose ) the output zoomed to around 800 reps.而如果我使用简单的 Java ServerSocket(仅用于测试目的),输出会放大到 800 次左右。

Why is so much difference in both of this?为什么这两者差别这么大? And how can I modify ServerSocketChannel to imitate ServerSocket?以及如何修改 ServerSocketChannel 来模仿 ServerSocket?

Here is my main processing part,这是我的主要处理部分,

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);
                                }
                            }

The difference is blocking I/O vs non blocking I/O .区别在于阻塞 I/O 与非阻塞 I/O

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM