简体   繁体   中英

Java server handle different requests from client

I am now design a train ticket booking system using socket. The client can search ticket, book ticket and cancel ticket. Clients will sent different objects to server,like SearchRequest, BookRequest and CancelRequest. On the server side,I want to have three threadpool to handle each request. Here is how I handle search request.

searchserverSocket = new ServerSocket(search_portNum);
search_pool = Executors.newFixedThreadPool
              (Runtime.getRuntime().availableProcessors()*POOL_SIZE); 
public void service(){
        while(true){
            Socket search_socket = null;
            try{
                search_socket = searchserverSocket.accept();
                SearchHandler search_han = new SearchHandler(search_socket,search_request);

                search_pool.submit(search_han);

            }catch(IOException e){
                e.printStackTrace();
            }
        }   
    }

I am now very confused how to handle other two request. Should I create a new ServerSocket on different port ? How can server identify different type of request ?

From the socket you will get a stream of bytes, you need to use that stream to determine what action is being requested - you need to design a protocol.

You could just use Java classes to define your protocol, serializing instances and writing the bytes to the socket, though if I was given this task I'd use Google Protocol Buffer . For example you could define messages for each request type like this (I'm guessing that the fields you'd need):

message Search 
{
  required string destination = 1;
  required string origin = 2;
  ...
}

message Book
{
  required int32 trainId = 1;
  required string destination = 2;
  required string origin = 3;   
  ...
}

message Cancel
{
  required int32 bookingId = 1;
  ...
}

When you have the classes that define your protocol you need to think about how they will appear as an array of bytes, which is what you'll get from the socket. The important thing here is to think about framing given your server will hopefully be receiving a stream of requests. There are two common approaches - you use a delimiter to mark the end of each message or you write the length of the message before each in a fixed number of bytes. Both have their pros and cons.

Once you are reading messages from the socket, you can switch on the request type and invoke the appropriate handler class.

I'll refer you to this link http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html . Read the whole tutorial

I think the little thing you are missing is you can write data in the Sockets, by retrieving the InputStream on it, also you can read information back using the OutputStream in it.

All you need is a way to pass the type of request you want, and read it from the stream on the server side. You won't need anything fancy, probably just pass down a string: "request=xxx", and parse it on the other side. Those implementation details are for you to figure out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM