4

我是谷歌协议缓冲区的新手。我正在编写一个客户端服务器应用程序,其中客户端将请求对象发送到服务器,服务器返回响应。目前,当我将对象发送到服务器时,服务器既不响应也不抛出任何异常。应该是卡在了线上

请求请求 = Request.parseFrom(bytes);

其中 Request 和 Response 是我由协议缓冲区生成的消息类。

我的代码示例如下

public class TCPServer {

   final static Logger logger = Logger.getLogger(TCPServer.class.getName());
   static int PORT = 6789;

public static void main(String argv[]) throws Exception
   {

      ServerSocket socket = new ServerSocket(PORT);
      Socket connectionSocket = null;
      while(true)
        {
           try{ 
                connectionSocket = socket.accept();
           }catch (IOException e) {
                System.out.println("Could not listen on port:" + PORT);
                System.exit(-1);
        }

           Thread thread = new Thread(new ServerConnection(connectionSocket));
           thread.start();

       }
  } 

}


public class ServerConnection implements Runnable{

   static final Logger logger =  Logger.getLogger(ServerConnection.class.getName());
   String clientInput;
   String serverOutput = null;

  Socket connectionSocket = null;

   ServerConnection(Socket connectionSocket){
    this.connectionSocket = connectionSocket;   
  }

  public void run() {


    try {
       InputStream input = connectionSocket.getInputStream();


        ObjectInputStream inFromClient =  new ObjectInputStream(input);

       ObjectOutputStream outToClient = new     ObjectOutputStream(connectionSocket.getOutputStream());


       serveRequest(inFromClient , outToClient);



        outToClient.flush(); 

     } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
            System.out.println("Exception occured in ServerConnection run() method");
        }
    }


public void serveRequest(InputStream inFromClient, OutputStream outToClient){


    try {
        System.out.println("Recieving data from client");
        ResponseReciever response = new ResponseReciever();

        ObjectInputStream input = (ObjectInputStream) inFromClient;

        byte size = input.readByte();
        byte []bytes = new byte[size];
        input.readFully(bytes);


        Request request = Request.parseFrom(bytes);


         System.out.println("Request recieved");
            response.createResponse(request.getId(),request.getMessage(),true).writeTo(outToClient);
        System.out.println("Response send");
    } catch (Exception ex) {
        logger.log(Level.SEVERE, null, ex);
        System.out.println("Exception occured in ServerConnection serverRequest() method");
    }

}

我的客户看起来像这样

public class TCPClient {

  final static Logger logger = Logger.getLogger(TCPClient.class.getName());

 private static int PORT = 6789;
 private static String HOST_NAME = "localhost";
 private static boolean isOpen = true;




 private Socket openConnection(final String hostName,final int port){

    Socket clientSocket = null;
    try {
        clientSocket = new Socket(HOST_NAME, PORT);


    } catch (IOException e) {
        logger.log(Level.SEVERE, "Exception occured while connecting to server", e);
    }
    return clientSocket;
 }

 private void closeConnection(Socket clientSocket){
    try {
        logger.log(Level.INFO, "Closing the connection");
        clientSocket.close();
        isOpen = false;
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Exception occured while closing the connection", e);
    }
}

  public void sendToServer(OutputStream output){
     try {
         System.out.println("Sending data to server");
         RequestSender requestSender = new RequestSender();
         Request request = requestSender.getRequest(1,"param1","param2",23L,"Its message",true);

        ObjectOutputStream outputStream = (ObjectOutputStream)output;

        request.writeTo(outputStream);


    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

}

  public void recieveFromServer(InputStream input){
     try {
       System.out.println("Recieving data from server");
        Response response = Response.parseFrom(input);

        System.out.println(response.getId());
        System.out.println(response.getResponse());
        System.out.println(response.getError());

    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
  }

}


 public static void main(String argv[]) throws Exception

 {


   ObjectOutputStream outToServer = null;

   InputStream inFromServer = null;

   TCPClient  client = new TCPClient();
   try {
    while(isOpen)
       {


        Socket clientSocket = client.openConnection(HOST_NAME, PORT);

        outToServer = new ObjectOutputStream(clientSocket.getOutputStream());

        inFromServer = new ObjectInputStream(clientSocket.getInputStream());


         client.sendToServer(outToServer);
         client.recieveFromServer(inFromServer);

        }
     }catch (Exception e) {
         logger.log(Level.SEVERE, "Exception occured ", e);
         System.out.println("Exception occured in TCPClient main() method");
         System.exit(1);
       }


     }
  }

我无法找到代码中的错误。如果您发现缺少某些东西,请告诉我。

4

2 回答 2

7

它通过使用 writeDelimtedTo(outputStream) 和 parseDelimitedFrom(inputStream) 而不是 writeTo(outputStream) 和 parseFrom(inputStream) 来工作。因此,通过将以下代码放在服务器端和客户端,程序可以工作。

服务器端:

        InputStream input = connectionSocket.getInputStream();
        OutputStream output = connectionSocket.getOutputStream();

        Request request = null;

        while ((request = Request.parseDelimitedFrom(input)) != null) {
            System.out.println(request.toString());

        }

客户端:

        Socket clientSocket = client.openConnection(HOST_NAME, PORT);

        Request request = getRequest();

        OutputStream output = clientSocket.getOutputStream();
         InputStream input = clientSocket.getInputStream();

         request.writeDelimitedTo(output);
于 2013-08-21T07:52:03.003 回答
0

如果您开始通过线路发送协议缓冲区 - 那么您将需要“框架”它们。问题报告并解决了这个问题:protobuf需要网络包头吗?

您可以查看https://code.google.com/p/protobuf-rpc-pro/并查看它是否满足您对 java 服务器和 java 客户端之间的 RPC 的要求,而不是编写所有这些代码。

于 2013-08-20T18:12:42.480 回答