在Java中重复接受同一套接字连接请求

时间:2019-06-14 10:52:11

标签: java android http

我正在Android 6.1平台上使用Java ServerSocket实现一个简单的http服务器。通过调用ServerSocket.accept(),线程将阻塞直到请求进入。

问题是,我总是看到相同的http请求被重复接受,最终只有其中一个可以获取请求的内容。任何想法?

在Android 6.1平台上使用Java ServerSocket的简单http服务器

while (mServerSocket != null && !mServerSocket.isClosed()) {
    try {
        Log.i(TAG, "{\"msgid\":\"RunningServer\",\"msg\":\"LogHttpServer starts to listen at " + mServerSocket.getInetAddress().getHostAddress() + ":" + mServerSocket.getLocalPort() + "\"}");
        Socket clientSocket = mServerSocket.accept();
        Thread clientRequest = new WorkThread(clientSocket, appContext);
        clientRequest.start();

    } catch (Exception e) {
        ...
    }
}

class WorkThread extends Thread {
    @Override
    public void run() {
    Log.i(TAG, "{\"msgid\":\"ReceivingData\",\"msg\":\"LogHttpServer accepted connection from "+socket.getInetAddress().getHostAddress()+"\"}");
    processHttpRequest(this.socket);

    private void processHttpRequest(Socket socket) {
        BufferedReader in = null;
        OutputStream out = null;

        if (this.socket.isConnected()) {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            } catch (IOException e) {
                    Log.e(TAG, "{\"msgid\":\"IOException\",\"msg\":\"fail to get Input Stream, " + e.toString() + "\"}");
            }

            try {
                out = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "{\"msgid\":\"IOException\",\"msg\":\"fail to get Input Stream, " + e.toString() + "\"}");
            }

            int lineNum = 0;
            String method = null;
            String request = null;

            while (true) {
                String line;
                try {
                    if ( in == null || (line = in.readLine()) == null || line.length() == 0) break;
                    lineNum++;

                    Log.i(TAG, "{\"msgid\":\"HttpRequest\",\"msg\":\"" + line + "\"}");
                    line = line.trim();
                    if (lineNum == 1) {
                        StringTokenizer tokenized = new StringTokenizer(line);
                        method = tokenized.hasMoreTokens() ? tokenized.nextToken().toUpperCase() : null;
                        request = tokenized.hasMoreTokens() ? tokenized.nextToken().toLowerCase() : null;
                    }

               } catch (IOException e) {
                   Log.e(TAG, "{\"msgid\":\"IOException\",\"msg\":\"fail to read in Stream\"}");
               } catch (Exception e) {
                    Log.e(TAG, "{\"msgid\":\"Exception\",\"msg\":\"" + e.toString() + "\"}");
                    return;
               }

           }
           ...
     }
}

结果日志如下:

2019-06-14T10:34:52.635Z  3149  3209 I LogHttpServer: {"msgid":"RunningServer","msg":"LogHttpServer starts to listen at :::33284"}
2019-06-14T10:34:52.636Z  3149  3209 I LogHttpServer: {"msgid":"RunningServer","msg":"LogHttpServer starts to listen at :::33284"}
2019-06-14T10:34:52.636Z  3149  4409 I LogHttpServer: {"msgid":"ReceivingData","msg":"LogHttpServer accepted connection from 192.168.43.254"}

--> this is where I don't expect: duplicate acceptance with separate thread created.
2019-06-14T10:34:52.636Z  3149  4410 I LogHttpServer: {"msgid":"ReceivingData","msg":"LogHttpServer accepted connection from 192.168.43.254"} 

2019-06-14T10:34:52.642Z  3149  4409 I LogHttpServer: {"msgid":"HttpRequest","Method":"GET", "Request":"/cgi-bin/logs.sh"}

--> later, it actually not able to parse the contents
2019-06-14T10:35:03.216Z  3149  4410 E LogHttpServer: {"msgid":"InvalidHttpRequest","msg":"null Method or Request. Return."}

0 个答案:

没有答案