Reputation: 127
I made a TCP sever with Qt on windows and I want to make my android app discuss with the server.
I was able to connect my app with the server now i want to send a packet to the server I found a way but I will have to change almost all the server code source.
The solution is to use a outputStream
or a DataStream
with android, I know exactly what to do with Qt but i don't know and I don't find the equivalence on Java, can somebody can help me please ? thank you :)
the code of the server:
QDataStream in(idClient);
if (tailleMessage == 0)
{
if (idClient->bytesAvailable() < (int) sizeof(quint16))
return;
in >> tailleMessage;
}
if (idClient->bytesAvailable() < tailleMessage)
return;
in >> type;
tailleMessage = 0;
the code of my windows client:
QByteArray typeToServer;
QDataStream typeOut(&typeToServer, QIODevice::WriteOnly);
QString type = typeDonnees;
typeOut << (quint16) 0;
typeOut << type;
typeOut.device()->seek(0);
typeOut << (quint16) (typeToServer.size() - sizeof(quint16));
socket->write(typeToServer);
I want make something like this with my androis apps instead of (if i use this code i will force to change the code of my server and my window client in order to readLine
):
String str = text01.getText().toString();
int a= str.length();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(a);
out.println(str);
Upvotes: 0
Views: 381
Reputation: 8259
Is this what you are after?
private void createClient() throws IOException {
Socket client = new Socket("server",22222);
String text = "hello"
// you can specify encoding of text to bytes by default java is UTF-8
byte[] message = text.getBytes("ASCII");
// encode length as 2 bytes (short integer) using big endian
byte[] length = new byte[2];
length[0] = (byte)(message.length >> 8);
length[1] = (byte)(message.length & 0x0ff);
// write message length to socket
client.getOutputStream().write(length);
// write message to socket
client.getOutputStream().write(message);
client.getOutputStream().close();
client.close();
System.out.println("client closed");
}
this is the same as above but only does one output write.
private void createSingleWriteClient() throws IOException {
Socket client = new Socket("localhost",22222);
String text = "hello";
// you can specify encoding of text to bytes by default java is UTF-8
// encode length as 2 bytes (short integer) using big endian
byte[] bytes = text.getBytes("UTF-16");
byte[] message = new byte[2+bytes.length];
message[0] = (byte)(bytes.length >> 8);
message[1] = (byte)(bytes.length & 0x0ff);
System.arraycopy(bytes, 0, message, 2, bytes.length);
// write message to socket
client.getOutputStream().write(message);
client.getOutputStream().close();
client.close();
System.out.println("client closed");
}
Upvotes: 1
Reputation: 5711
Assuming you have a C++ server and a Java client (being Android app), your safest choice is to stick to a text (potentially compressed/encrypted) protocol that is compatible with both: XML or JSON. Both C++ and Java have support for either.
Upvotes: 2