使用java在客户端服务器的协议缓冲区中需要帮助

时间:2013-08-20 05:21:36

标签: java client-server protocol-buffers

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

请求请求= 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);
       }


     }
  }

我无法找到代码中的错误。如果您发现遗失的内容,请告诉我。

2 个答案:

答案 0 :(得分:4)

它的工作原理是使用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);

答案 1 :(得分:0)

如果您开始通过线路发送协议缓冲区 - 那么您将需要“构建”它们。使用此问题报告并解决了该问题:does protobuf need a network packet header?

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