0

I want to write a simple client server application where they are deployed in different locations, the server needs to expose two public methods to the client and keep data exchange between them to a minimum, I was going down the path of using sockets, however, how does the client call a public method on the server? Or is there another way?

2 Answers 2

2

There are several ways to do this, but I would take a look at RMI. It makes calling methods on remote java objects pretty easy. There are still going to be low level protocols involved (sockets/tcp), but you won't need to manage that explicitly yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think so, - I feel like Khalid could have made use of servlets there to develop a web service.
If the server is running in an application server like Tomcat and can expose web services, then sure it can be done that way. But if the server is just another process running a java app, which I suspect it is based on the question, then web services are not really going to help here.
You're absolutely right, Jeff. But it feels like Khalid is not aware of possibility of using application server considering he asked the question.
Thanks guys - most of my Java code is working within a web framework, but I have been given a task of having a client server application, the client can only have a command line interface where it reads a file in and parses it and then calls two public methods on the server. I'm taking a look at RMI right now.
1

You need to encode the action and response you want in a some text or binary format.

A simple way to do this is to send the name of the method you want to call and the other end reads the name and calls the method of that name. If you want to make it shorter you can send (byte) 1 to call the first method and (byte) 2 to call the second method etc. and use a switch statement to call the appropriate method.

2 Comments

Thanks I was looking at this as an option, the requirement is there to have a the server expose two public methods that the client can call.
That's what I suggested. This is basically what RMI does (with extra functionality)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.