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?
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