I'm writing a client-server pair where the server is a java TCP server running on Linux and the client is an Android app developed in Android Studio.
I've successfully made a client-server pair that sends Message objects to each other, but when I try to implement similar functionality in my Android app nothing seems to happen.
The Android app works while just sending Strings with the readLine() and println() methods from the BufferedReader and Printwriter classes, but not with the readObject() and writeObject() from ObjectOutput / InputStream classes.
Have also tried writeUnshared() / readUnshared() methods without luck.
//Message.java
package Message;
import java.io.*;
public class Message implements Serializable {
String msg;
String tag;
String username;
private static final long serialVersionUID = 4L;
// Methods.
}
// Reading MessageObjects in Server.java.
#Override
public void run() {
Message message = null;
try {
while ((message = (Message)reader.readObject()) != null) {
// Processing message.
}
}
}
// Sending Message Objects in android App.
public void onClick(View v) {
if(!msgBox.getText().toString().equals("")) {
final String msg;
try {
msg = msgBox.getText().toString();
writer.writeObject(new Message(msg, CLIENT, username));
msgBox.setText("");
writer.flush();
// Updating ui etc.
}
catch (IOException e) {
e.printStackTrace();
}
textBox.smoothScrollBy(textBox.getMaxScrollAmount(), 100);
}
}
// Connection-method in android app, initalizes streams.
private boolean connect(String username, String address, int port) {
boolean connected = false;
try {
server = new Socket(address, port);
InputStreamReader(server.getInputStream());
reader = new ObjectInputStream(server.getInputStream());
writer = new ObjectOutputStream(server.getOutputStream());
writer.writeObject(new Message("!newUser",AUTOMATED,username));
writer.flush();
connected = true;
System.out.println("Connected!");
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot Connect!");
connected = false;
// UI-things.
}
if(connected){
// Thread that listens for replies.
listenThread();
}
return connected;
}
You need to create the ObjectOutputStream before the ObjectInputStream, at both ends. Otherwise you can get a deadlock.
Your read loop is incorrect. readObject() doesn't return null at end of stream, so using null as a loop condition doesn't make sense. It can return null any time you send a null. The loop should terminate when EOFException is caught.
Related
I created 2 Java programs with sockets in it. I want the client to send continuous data to the server. But after the message sent to the server, the client keeps sending 'null' value to the server (it happens when I close the socket in client program).
Here is my codes:
import ...
public class MainClient {
private Socket serverSock;
private PrintStream clientOutput;
public static void main(String[] args) {
MainClient client = new MainClient();
client.runClient();
}
public void runClient() {
try {
serverSock = new Socket("127.0.0.1",8282);
clientOutput = new PrintStream(serverSock.getOutputStream());
clientOutput.println("Hello, I'm Connected.");
for (int i=0;i<5;i++) {
clientOutput.println(i + "");
clientOutput.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// try {
// serverSock.close(); It will keeps sending 'null' data to the server if I use this line.
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
}
The Server Side:
public class MainServer {
private ServerSocket serverSocket;
private int listenPort = 8282;
private InputStream inps;
private Socket clientSocket;
private BufferedReader clientInput;
private MainServer() {
String clientMsg = "";
try {
serverSocket = new ServerSocket(listenPort);
System.out.println("Server is Listening on " + listenPort);
clientSocket = serverSocket.accept();
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(clientSocket.isConnected()) {
clientMsg = clientInput.readLine();
System.out.println("Client : " + clientMsg);
}
}catch(IOException ex) {
ex.printStackTrace();
}finally {
try {
clientSocket.close();
} catch (IOException e) {}
}
}
public static void main(String[] args) {
new MainServer();
}
}
I tried to close the OutputStream on the Client side with clientOutput.close(); but it sends nulls to the server after it sends the 0-4 loop.
To make it stop and avoid the client sends null data, i should not insert the serverSock.close(); on the Client, but it will returns SocketException. I wanted the client to send 'Closed' message after its done.
Summary, the output on the server is:
Client: 0
Client: 1
Client: 2
Client: 3
Client: 4
Client: null
Client: null
//And so on..
I think there is something missing on the Client Program, i guess?
Thank you for the help :)
As the comment noted, the client is not sending a null value.
The isConnected() method does not do what you think it does, namely it does not tell you if the socket is currently "connected" to its peer, at least in the way you think it should. isConnected() becomes true as soon as the socket transitions into the connected state, and stays true thereafter, even after the socket is shutdown. See this discussion and others on stackoverflow.
The correct way to determine if the peer has shutdown the connection is to attempt to read from the socket and then examine the result for evidence of closure. Please read the Javadocs for the method you are using, they will tell you what the various return values mean. For the BufferedReader.readLine() method, it says:
Returns:
A String containing the contents of the line, not including
any line-termination characters, or null if the end of the stream has
been reached
Throws:
IOException - If an I/O error occurs
Thus you need to check for a null return value to detect a normal socket closure, and if you receive an IOException that indicates some kind of network anomaly.
Your MainClient() have no problem.
clientSocket.isConnected() function in MainServer() always check the status of the client and which results an infinite loop, so after the message 'client:4', clientInput.readLine() should return 'null'.
So instead of checking the client socket is connected or not you can check the client socket is closed or not using function 'clientSocket.isClosed()'.
replace the while loop in MainServer() with below code,
while(!clientSocket.isClosed()) {
clientMsg = clientInput.readLine();
System.out.println("Client : " + clientMsg);
if(clientMsg.equals("Closed")){
clientSocket.close();
// serverSocket.close();
}
}
this will help you to close the client socket at the time of receiving 'Closed' message from server and this avoid the infinite execution of while loop as well as null statement printing.
The code "serverSocket.close()" help you to close the server socket and you can use this at 'MainServer()' if you need to stop the port listening.
typically the code should be something similar
private MainServer() {
String clientMsg = "";
try {
serverSocket = new ServerSocket(listenPort);
System.out.println("Server is Listening on " + listenPort);
clientSocket = serverSocket.accept();
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while ((clientMsg = clientInput.readLine()) != null) {
if(isTerminationString(clientMsg)) {
break;
}
System.out.println("Client : " + clientMsg);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
}
}
}
boolean isTerminationString(String msg) {
return msg.equals("DONE!");
}
where in isTerminationString you check if the msg is a termination msg, the communication protocol should be shared between the client and the server . i gave the example of sending
a DONE message, but it could more complex than that .
as closing the close method on the socket does not guarantee that the socket on the other part gets closed as well, using the isClosed method might not be effective and results in the same problem you have .
I have an app that connects to a RaspberryPi via Bluetooth and loops the same data to it while it receives some data back.
I had some issues with the connection so this workaround is needed to connect my android phone to the RaspberryPi: IOException: read failed, socket might be closed - Bluetooth on Android 4.3
For some reason, the android phone is receiving its own output.
The String "Hello Raspberry. It's me, AndroidPhone" is sent to the output in a never-ending loop. The incoming data (from the RaspberryPi) is also read in a never-ending loop.
But somehow I don't only receive the data from the RaspberryPi but also the string sends via smartphone. This is my code:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
#Override
protected void onCreate(Bundle savedInstanceState) {
// (...)
// Only GUI-stuff until this point
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice raspberryPi = bluetoothAdapter.getRemoteDevice("B8:27:EB:56:DC:B2");
BluetoothSocket btSocket;
try {
btSocket = raspberryPi.createRfcommSocketToServiceRecord(SERIAL_UUID);
btSocket.connect();
} catch (IOException e) {
Log.e("BTError", e.getMessage());
// Workaround, found on: https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3
try {
Log.e("BTError", "Trying fallback...");
btSocket = (BluetoothSocket) raspberryPi.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(raspberryPi, 1);
btSocket.connect();
(new Thread(new SendingThread(btSocket))).start();
(new Thread(new ReceivingThread(btSocket))).start();
} catch (Exception e2) {
Log.e("BTError", e2.getMessage());
Log.e("BTError", "Couldn't establish Bluetooth connection!");
}
}
}
private class SendingThread extends Thread {
private OutputStream out;
public SendingThread(BluetoothSocket btSocket) {
try {
out = btSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
try {
int delay = 100000000;
while (true) {
if (delay == 0) {
Log.i("WRT", "Written to RaspberryPi");
out.write("Hello Raspberry. It's me, AndroidPhone".getBytes());
delay = 100000000;
}
delay--;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ReceivingThread extends Thread {
private InputStream in;
public ReceivingThread(BluetoothSocket btSocket) {
try {
in = btSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
int data = 0;
while (true) {
try {
data = in.read();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("RCV", String.valueOf((char) data));
}
}
}
On the RaspberryPi end, everything looks normal. A simple java program starts the Linux command rfcomm listen /dev/rfcomm0 and reads from/writes to the file /dev/rfcomm0 with FileReader and FileWriter. The only relevant lines on this end are:
run {
// Inside writer-thread
bluetoothWriter = new BufferedWriter(new FileWriter("/dev/rfcomm0"));
while(true) {
bluetoothWriter.write("This is RaspPi");
bluetoothWriter.flush();
}
}
and
run {
// Inside reader-thread
bluetoothReader = new BufferedReader(new FileReader("/dev/rfcomm0"));
while(true) {
int incData = bluetoothReader.read();
System.out.print((char) incData);
}
}
Thank you for your help!
edit: Still no solution to this problem. I suspected that the RaspberryPi is somehow sending back what it received. But when I disabled that it sends out anything, the smartphone still directly receives what it has sent out.
I scoured over the Bluetooth classes sources. The workaround seems legit from the first glances. Try this first:
if (delay == 0) {
Log.i("WRT", "Written to RaspberryPi");
out.write("Hello Raspberry. It's me, AndroidPhone".getBytes());
out.flush(); // <-- You are not flushing
delay = 100000000;
}
And the message sticks in you socket for you to read over and over again.
If that does not fix it the other option I can think of is that somehow the socket is initialized to be a socket to your Android device. The .createRfcommSocket() method seems to create a socket to your own device if the Bluetooth device is null when the socket was being created. I'm not sure how this would exactly happen, but if the Raspberry Pi's state is somehow mangled after exception I suppose it could be something to look into.
On the raspy side: If you are just starting both of those threads doesn't it mean that you are constantly sending messages to /dev/rfcomm0 and flushing. I recommend that you change it so that raspy reacts to a received message by sending back the wanted message instead of spamming all the time. I'm not sure if this is part of your problem but it would at least make debugging & development a bit easier.
I am not sure if this is the solution you need, because I don't know if you are using bluetooth classic or bluetooth 4.0>+, but I wrote a library for text based BLE and WiFi P2P 2-way communication for android (and I know the Raspberry Pi is capable of BLE communication), I don't create a socket connection for BLE communication though, but I do for WiFi P2P. Take a look, I hope it helps. It isn't published yet, so you would have to clone/fork the repo.
I think you have trouble writing
As far as I know, for buffer, should use \n and ...
bluetoothWriter.write("This is RaspPi\n");
But I prefer to use a combination of DataOutputStream and BufferedReader
For Read:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line = bufferedReader.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
for write:
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
String s = "Hi\n";
try {
dataOutputStream.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
It is better to correct the point made by a dear friend about flush() ...
I'm not sure, please test yourself ...
I want to integrate a server with multiple clients for a blackjack game I created, and thus I began practicing with servers in java. I create a thread, that when ran, forces the server to listen for input and produce an output. Then I added a feature to stop the server. However, the server randomly produces the correct output, and sometimes fails to connect. Here is the code for when the user hosts a server:
st = new ServerThread(); //this is a field of type ServerThread
st.start(); //this runs the server concurrently as a new thread
Here is the code for when they close a server:
st.stopThread();
Finally, here is the source for the serverThread:
public class ServerThread extends Thread {
private volatile boolean isRunning = true;
private Socket socket;
private static final int PORTNUM = 1342;
#Override
public void run() {
while (isRunning) { //should run only when the
try {
ServerSocket serverSocket = new ServerSocket(PORTNUM); //uses the same port number, which I made a constant
//Reading the an object of type Information from the client
socket = serverSocket.accept();
ObjectInputStream serverInputStream = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
Information i = (Information) serverInputStream.readObject();
//arbitrarily changes the data stored in the information object to verify connection with server
i.setI(100);
i.setS("new string");
i.setD(4.4);
//sends the modified object back to the client
serverOutputStream.writeObject(i);
serverInputStream.close();
serverOutputStream.close();
socket.close();
} catch (IOException e) {
//System.out.println("IOException");
//e.printStackTrace();
} catch (ClassNotFoundException e) {
//System.out.println("ClassNotFoundException");
//e.printStackTrace();
} finally {
try {
if (socket != null) { //avoid null pointer if no connections have been established
socket.close();
}
} catch (IOException ex) {
//Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void stopThread() {
isRunning = false;
}
}
Any suggestions on edits to make my code perform correctly and consistently would be welcome. Thanks.
I would move the socket definition away from being an instance variable i.e,
while (isRunning) {
Socket socket = null;
try {
...
I would like to open a TCP connection to send data to an ip address in an android application. Every socket programming article/thread I find shows both client and server side code (often the chat program). Is it possible to just have the client code running on an android device and send arbitrary data to, for example, google's IP address? Right now I am using the code from this thread (highest upvoted answer) Android Client socket , how to read data? in a class that extends AsynchTask like this:
public class InternetTask extends AsyncTask<Void, Integer, Void> {
public static final int BUFFER_SIZE = 2048;
private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;
private int port = 80;
private String host = null;
private static final String TAG="sure2015test";
public InternetTask(String host,int port) {
this.host=host;
this.port=port;
}
#Override
protected Void doInBackground(Void... args) {
connectWithServer();
Log.i(TAG, "Connected");
sendDataWithString("hello");
Log.i(TAG, "Sent data");
String response=receiveDataFromServer();
Log.i(TAG,response);
disConnectWithServer();
return null;
}
private void connectWithServer() {
try {
if (socket == null) {
socket = new Socket(this.host, this.port);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
} catch (IOException e) {
Log.i(TAG,"IO Exeception");
e.printStackTrace();
}
}
private void disConnectWithServer() {
if (socket != null) {
if (socket.isConnected()) {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
Log.i(TAG,"IO exception disconnecting");
e.printStackTrace();
}
}
}
}
public void sendDataWithString(String message) {
if (message != null) {
connectWithServer();
out.write(message);
out.flush();
}
}
public String receiveDataFromServer() {
try {
String message = "";
int charsRead = 0;
char[] buffer = new char[BUFFER_SIZE];
Log.i(TAG,"Message before: "+message);
while ((charsRead = in.read(buffer)) != -1) {
message += new String(buffer).substring(0, charsRead);
Log.i(TAG,message);
}
Log.i(TAG,"Message after: "+message);
disConnectWithServer(); // disconnect server
return message;
} catch (IOException e) {
Log.i(TAG,"IO Error in receiving message");
return "Error receiving response: " + e.getMessage();
}
}
}
And my onCreate method in MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InternetTask task=new InternetTask("74.125.226.159",80);
task.execute();
}
When I run this I get this in the logs:
05-16 00:01:04.368 622-637/? I/sure2015test﹕ Connected
05-16 00:01:04.369 622-637/? I/sure2015test﹕ Sent data
05-16 00:01:04.369 622-637/? I/sure2015test﹕ Message before:
05-16 00:03:04.405 622-637/com.example.connorstein.sockethelloworld I/sure2015test﹕ Message after:
So there were no exceptions when opening a socket on port 80 with the IP of google and no exceptions sending or receiving the data. It looks like I just have no response. Is this expected because the data I sent ("hello") is meaningless? I would think that at least I would get a response saying invalid request or something like that. I also tried sending "GET / HTTP/1.0", but also no response.
Google is responding blank because you aren't sending it a proper HTTP request. Mimic the request of a normal web browser, and you will get a response. HTTP is a protocol built on top of TCP. You need to follow the protocol to get anything useful out of servers.
Example minimal browser header that gets a response:
GET / HTTP/1.1\r\nUser-Agent: curl/7.37.1\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n
Instead of using a web server, consider using an SSH server. An SSH server will send something like...
SSH-2.0-OpenSSH_5.3\r\n
...when you connect. A server that always responds is a lot easier to troubleshoot client code. Note that you might make a sysadmin mad constantly connecting to their ssh server. You may want to set up your own to test against.
I have two apps that work in conjunction with one another. One is a "server" type app that does not have any GUI interface and handles queries to a database and processes requests from a client. The other is a "client" that is primarily a GUI and is for users to interact with database information in a structured manner.
ISSUE / TROUBLE / HELP NEEDED WITH
The problem that I am having is that I can send one Object (a String[]) to the server successfully and with no problems. Client app sends it, Server app receives it an processes it successfully.
If I try and send a second String[], the the client compiles the array and thinks it gets sent, but the server never receives is (gets only null) and produces a IOException.
This is even with Arrays that contain the exact same number of positions and the exact same text in the exact same format and positions.
The error produced by the printStackTrace() is:
Java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0 (ObjectInputStream.java:1367)
at java.io.ObjectInputStream.readObject (ObjectInputStream.java:369)
at server.ConnectionThread.processClientRequests(ConnectionThread:204)
at server.ConnectionThread.processClientRequests(ConnectionThread:50)
at javalang.Thread.run(Thread.java:722)
The code at line 204 that is the point where the ObjectStream is being read from:
String[] addArray = (String[]) ois.readObject();
ois is an ObjectInputStream and is initialized as follows:
private ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
CLIENT CODE
The client code used to send these objects TO the server app is:
ObjectToServer.writeObject(String[] var);
ObjectToServer.flush();
ObjectToServer.reset();
COMMENTS
What does NOT make sense to me is that this exact same code format is used to successfully send a number of String[] over the objectOutputStream from the SERVER to the CLIENT app without ever sending a "null"
I have Google searched this and all to absolute no avail.
Someone please help if you can!!
ADDITIONAL CODE
// CONNECTION THREAD IS ON SERVER APP, SETS UP STREAMS AND WAITS FOR MESSAGES FROM CLIENT
// HANDLES COMMUNICATION FROM CLIENT AND REST OF SERVER
public class ConnectionThread implements Runnable
{
private Socket socket;
private SystemCore core;
//Streams for connections
private InputStream is;
private OutputStream os;
//Writers and readers for communication of Strings
private PrintWriter toClient;
private BufferedReader fromClient;
// Writers and readers for sending and receiving Objects between server and client.
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
//Protocol
static final String CLIENT_QUITTING = "Exit";
public ConnectionThread(Socket s, SystemCore aSysCore)
{
socket = s;
// State of the SystemCore as taken from HelloServer
core = aSysCore;
}
public void run()
{
try
{
openStreams();
toClient.println(MESSAGE_TO_CLIENT);
processClientRequests();
closeStreams();
this.socket.close();
}
catch (OptionalDataException ode )
{
System.out.println("OptionalDataException: ");
System.out.println("length is: " + ode.length);
}
catch (IOException ioe)
{
System.out.println("IO trouble with a connection in ConnectionThread run() " + ioe.getMessage());
ioe.printStackTrace();
}
catch (ClassNotFoundException cnf)
{
System.out.println("Class trouble with a connection in ConnectionThread run() " + cnf.getMessage());
cnf.printStackTrace();
}
catch(ParseException pe)
{
System.out.println("Parse trouble with a connection in ConnectionThread run() " + pe.getMessage());
pe.printStackTrace();
}
}
/**
* Opens streams between the server and the client.
*
* #throws IOException
*/
private void openStreams() throws IOException
{
final boolean AUTO_FLUSH = true;
this.is = this.socket.getInputStream();
this.fromClient = new BufferedReader(new InputStreamReader(is));
this.os = this.socket.getOutputStream();
this.toClient = new PrintWriter(os, AUTO_FLUSH);
//Object streams.
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
System.out.println("...Streams set up");
}
/**
* Private method that accepts arguments from a client and executes the related
* commands in the systemcore as long as the command passed from the client
* is not CLIENT_QUITTING.
*
* #throws IOException
* #throws ClassNotFoundException
*/
private void processClientRequests() throws IOException, ClassNotFoundException, ParseException
{
String commandFromClient;
commandFromClient = fromClient.readLine();
while (!(commandFromClient.equals(CLIENT_QUITTING)))
{
if (commandFromClient.equals("addProjectPrepare"))
{
String[] addArray = (String[]) ois.readObject();
core.addProjectPrepare(addArray);
}
if (commandFromClient.equals("editProjectPrepareDetails"))
{
String[] editArray = (String[]) ois.readObject();
recruit.editProjectPrepareDetails(editArray);
}
}
commandFromClient = fromClient.readLine();
}
**// CLIENT SIDE (User GUI) CODE THAT SENDS STRING[] TO THE SERVER**
public void saveAction()
{
// TEST TO SEE IF THE DATE ENTERED IS CORRECT FORMAT, IF NOT NO SAVE OCCURRS
boolean parsedOk = false;
if (this.arrivalDateTextField.getText().isEmpty() == false)
{
try
{
// Check if date is correct format. Nothing will be done with
// the testDate object
MyDate testDate = new MyDate(
this.arrivalDateTextField.getText());
//Allow write to server to occur.
parsedOk = true;
//If date is okay, send form data to server.
}
catch (ParseException pe)
{
this.arrivalDateTextField.setText(""); // Set text field to blank
int messageIcon = javax.swing.JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(this, "Invalid date",
"Warning", messageIcon);
}
}
else
{
parsedOk = true; // No date entered so allow blank.
}
if (parsedOk == true)
{
// WRITE DATA TO SERVER OCCURS HERE:
try
{
**//getPersonDetails() returns a String[]**
ManageClientConnections.toServer.println("addNewData");
ManageClientConnections.objectToServer.writeObject(this.getPersonDetails());
ManageClientConnections.objectToServer.flush();
ManageClientConnections.objectToServer.reset();
}
catch (IOException ioe)
{
System.out.println(
"While writing new person to server, there was an error: " + ioe.getMessage());
}
// And dispose of the GUI, inside the parseok if clause
this.dispose();
}
}
You can't create multiple input/output streams over the same socket input/output streams. that doesn't work. you need to pick one type of stream and stick with it. Since you need to send structured data, you should use only the Object streams and ditch the Print streams. if you need to send different types of messages from client to server, then you should consider using a wrapping Serializable object type (e.g. Message) which can contain different types of messages.