2
votes

I'm programming a socket connection. Server and Client. Server waits for connection by socket.accept(); Then all incoming messages can be processed.

So far so good, but how can I control these messages? At the moment, I only see that eg the server expects a certain message, and the client has to send this message. This could be a String message. But what if the client sends another message, eg an Integer? How can I make the server expect different messages? And based on the parameter type and parameter count, decide which method is to execute?

Or is a socket just for exchanging messages in a well known order between client and server?

Specially I'm looking for sth like to supply a eg processInteger and getString() method on server side. And if I send a message from client, based on these message I want to execute either or other method. Can I somehow control this by the client to which method the message goes?

3
You could check what the passed object is an instance of. - FThompson
Yes I use this socket. OK Instance of might be worth a try. But still I cannot really control the messages order. - membersound

3 Answers

0
votes

An socket connection is used to transfer any kind of message you want. The "raw" bytes of the message are sent through it. It's not up to the socket to define the format of the message (character data -- Strings, Integer or other type of objects, binary data, etc); it's up to you. Also the order of the messages (requests and responses) are defined by you. Together, the message format, sequence and other behaviour are referred to as the transport and/or message "protocol".

You can define and implement your own protocol, or use existing protocols, or combinations of protocols, such as HTTP, RMI, XML, json, etc, etc.

Good luck! Tom

0
votes

Unix-style sockets (as Java uses) don't present the Abstraction of passing structured ("typed") data. Instead, they represent an arbitrary stream of bytes. The only thing that gives structure to this is when the client and the server agree on how to communicate (a protocol). As you might notice from the doc, the only way to actually get data out of a Socket is with getInputStream(), which lets you read(), which gives you either a single byte or a byte[].

For example, if the client wants to send a String, perhaps it would put 'str' as the first three bytes of the message, and if it's an Integer it's 'int', or 'dbl' for Double and so on. If the server knows that the first three bytes signify the type of data, then it's able to use that to figure out how to read the rest of the message. This is the basis of essentially all networking - clients and servers speaking the same 'language'.

0
votes

The Java Tutorials Page explain it very easy: Java Tutorials: Sockets.

You can send serializable objects send it and deserialize on server. Of course, the serializable class has to be defined in both client and server.

You can use ObjectInputStream and ObjectOutputStream for read and write those objects.