套接字输出响应未到达客户端

时间:2013-10-22 11:02:26

标签: java sockets

您有一个成功打印到输出流的套接字客户端。服务器检索它并尝试在输出流上返回响应字符串。但是,尽管我可以在客户端的readLine()方法上看到调试触发器,但响应永远不会到达客户端。

客户端代码段

       protected static BufferedReader getClientInputChannel() {
            BufferedReader inFromServer = null;
            try {
                if (clientSocket != null)
                    clientSocket = new Socket(TPS_HOST, TPS_PORT);
                inFromServer = new BufferedReader(new InputStreamReader(
                        clientSocket.getInputStream()));
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return inFromServer;
        }


        if (query != null && !query.isEmpty()) {                
                    System.out.println("RetrievalSystem: send query: " + query);
                    outToServer.println(query);
                    String response = getClientInputChannel().readLine();
                    System.out.println("RetrievalSystem: response from TPS: " + response);
                    // Close the output stream.
                    outToServer.close();
                }

服务器端。

package com.ds;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

public class TextProcessingServer extends Thread {
    protected final static int DEFAULT_TPS_PORT = 8081;

    protected int port;

    public TextProcessingServer(int defaultTpsPort) {
        setPort(defaultTpsPort);
    }

    public TextProcessingServer() {
        setPort(DEFAULT_TPS_PORT);
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public void run() {
        init();
    }

    /**
     * single threaded query consumer.
     */
    protected void init() {
        System.out.println("Initializing TPS Server.");
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        try {
            serverSocket = new ServerSocket(getPort());
            clientSocket = serverSocket.accept();

            // set up the read and write end of the communication socket
            BufferedReader inFromClient = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter outToClient =
                    new PrintWriter(clientSocket.getOutputStream(), true); 

            // retrieve first line of request and set up for parsing
            String requestMessageLine = inFromClient.readLine();

            while (requestMessageLine.length() > 0) {
                System.out.println("TPS SERVER: Incoming request: " + requestMessageLine);
                // Send reply       
                    // THIS REPLY NEVER REACHES THE CLIENT.
                outToClient.println(queryInvertedIndex(requestMessageLine));
                // Now, listen again for the next incoming request.
                requestMessageLine = inFromClient.readLine();
            }

            System.out.println("left client listening mode.");

        } catch (IOException e) {
            System.out
                    .println("Exception caught when trying to listen on port "
                            + getPort() + " or listening for a connection");
            System.out.println(e.getMessage());
        } finally {
            try {
                clientSocket.close();
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public String queryInvertedIndex(String queryString) {
        InvertedIndex invIdx = InvertedIndex.getInstance();
        // There could be multiple query strings, thus split.
        // Also handle html encoded strings: pigs%20fly
        String queryStrings[] = queryString.split("%20|\\s");
        StringBuilder resultList = new StringBuilder();
        for (String key : queryStrings) {
            List<Integer> partialResultList = invIdx.get(key);
            for (Integer lineNumber : partialResultList)
                resultList.append(lineNumber.toString() + " ");
            resultList.replace(resultList.length() - 1, resultList.length(),
                    ", ");
        }
        // Remove the last comma.
        if (resultList.length() > 0)
            resultList = new StringBuilder(resultList.substring(0,
                    resultList.length() - 2));
        else
            resultList = new StringBuilder("[NO_RESULT]");
        return resultList.toString();
    }

}

2 个答案:

答案 0 :(得分:0)

我认为您忘记刷新输出流。

outToClient.flush();

试试..

答案 1 :(得分:0)

解决。这是你看不到的东西。我从静态类调用了服务器。在没有静态方法的情况下将类更改为非静态类有帮助。