Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help in protocol buffer in client server using java

I am new to google protocol buffer . I am writing a client server application where client send request object to server and server return response. Currently when i send object to server neither the server respond nor throw any exception. Probably it stuck on line

Request request = Request.parseFrom(bytes);

where Request and Response are my message classes generated by protocol buffer.

My code samples are as follows

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

}

And my client look like this

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


     }
  }

I am unable to find what is wrong in the code. Please let me know if you find something missing.

like image 428
Jawad Avatar asked Mar 23 '23 17:03

Jawad


1 Answers

It works by using writeDelimtedTo(outputStream) and parseDelimitedFrom(inputStream) instead of writeTo(outputStream) and parseFrom(inputStream). So by putting the following code on server and client sides the program works.

Server side:

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

        Request request = null;

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

        }

Client side:

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

        Request request = getRequest();

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

         request.writeDelimitedTo(output);
like image 191
Jawad Avatar answered Apr 13 '23 21:04

Jawad