Android client with remote server - java

Im making an app that has to send a class to a server written in c++ using Sockets. The class consists of to variables both are Ints. I want to convert the java class into bytes then send it over the socket as a packet. The server is expecting 8 bytes for the packet size. When I try to convert my object I get more than 8 bytes. How else can I send my object to the server? Also the my code below sends 4 bytes of data in two 2 bytes chucks. Why is it doing that?
public void connect2() {
String serverHostname = new String("My IP");
ObjectOutputStream out2 = null;
ObjectInputStream in2 = null;
try {
echoSocket = new Socket(serverHostname, MYPORT);
StatusPacket p = new StatusPacket();
byte[] data = new byte[8];
data = serializeObject(p);
int j = data.length;
out2 = new ObjectOutputStream(echoSocket.getOutputStream());
out2.flush();
in2 = new ObjectInputStream(echoSocket.getInputStream());
DataOutputStream dOut = new DataOutputStream(echoSocket.getOutputStream());
out2.write(data);
out2.close();
in2.close();
echoSocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
}

ObjectOutputStream and ObjectInputStream use Java serialization mechanisms, which includes a lot more info than just the class property values. You don't want to deal with those in C++ code, so I recommend you remove all serialization code and those two streams from your code.
Since you already know what you want on byte level, you should really be using that DataOutputStream instead. It allows you to transfer scalar data types like byte, int, long etc. without any overhead. Just get those two 32-bit integers from your object and pass them to DataOutputStream.writeInt(..) and you're set.

Related

Socket communicating between java and c# applications

I have two applications, one written in Java and the other in C#. I am trying to send a string from the Java app to C# app.
My java code for sending the string is as follows:
String response;
try {
DataOutputStream outToServer =
new DataOutputStream(outGoingSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(outGoingSocket.getInputStream()));
outToServer.writeBytes(message + '\n');
outToServer.flush();
response = inFromServer.readLine();
System.out.println("Received: " + response);
} catch (Exception ex) {
System.err.println("Exception in incoming socket: " + ex.getMessage());
}
My C# code for receiving the data is as follows:
Byte[] bytes = new Byte[1000];
String data = null;
try {
Console.Write("Waiting for a connection... ");
TcpClient client = incomingSocket.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
while (true) {
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) {
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received:", data);
processReceivedMessage(data);
ackSocket(stream, "OK");
}
}
} catch (Exception ex) {
Console.WriteLine("Exception: ", ex);
}
I have a problem with receiving the data in the C# application. When I send the string "Data" in the Java app, and try to print the data received by the C# application using Console.WriteLine("Received: {0}", data), the output is:
Received: D
Received: ata
If I use Console.WriteLine("Received: ", data), the output is:
Received:
Received:
I want my C# application to receive the full string that is sent by the Java application. I tried to increase the buffer byte array size to 1000 but it doesn't help. I don't have experience using sockets, can someone show me what I did wrong?
So, as you can see, the receiving end picks up a response in chunks that might be considerably smaller than the total message.
You shouldn't be seeking to change this behaviour... it's a fact of network programming. It's your job to glue them back together again.
"I want my c# application receive the full string"
So, how is your receiving app meant to know that it has received the full string? Did you send a length field to indicate how much data is coming? Perhaps you expect \n to indicate the end of message? A zero byte?
If your terminator is indeed a newline, you might want to consider passing your NetworkStream to a StreamReader and calling ReadLine on it. Now, the StreamReader will keep reading from the stream until it hits a newline, then hand you the line.

java.io.StreamCorruptedException: invalid stream header: 00000001

I keep getting this get this Exception:
java.io.StreamCorruptedException: invalid stream header: 00000001
Server side I used this to send and receive int, works fine.
Server:
new DataOutputStream(player1.getOutputStream()).writeInt(P1);
Client:
dataFromServer = new DataInputStream(socket.getInputStream());
dataFromServer.readInt();
But when I try to send an object, like this, it gives the error.
Server:
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
Client:
objectFromServer = new ObjectInputStream(socket.getInputStream());
crossword = (Crossword)objectFromServer.readObject();
Any help would be good. Here is me sending the crossword initially prior to game session
I changed the code to use only object streams rather than data streams, upon the advice of jtahlborn
server
player1 = serverSocket.accept();
serverLog.append(new Date() + ": Player 1 joined session " + sessionNo + '\n');
serverLog.append("Player 1's IP address" + player1.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player1.getOutputStream()).writeInt(P1);
new ObjectOutputStream(player1.getOutputStream()).writeObject(gameCrossword);
player2 = serverSocket.accept();
serverLog.append(new Date() + ": Player 2 joined session " + sessionNo + '\n');
serverLog.append("Player 2's IP address" + player2.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player2.getOutputStream()).writeInt(P2);
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
client
private void connectToServer() {
try {
Socket socket = new Socket(host, 8000);
objectFromServer = new ObjectInputStream(socket.getInputStream());
objectToServer = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.err.println(ex);
}
Thread thread = new Thread(this);
thread.start();
}
#Override
public void run() {
try {
player = objectFromServer.readInt();
crossword = (Crossword)objectFromServer.readObject();
System.out.println(crossword);
regards,
C.
don't wrap the socket streams with more than one input/output streams. this will break in all kinds of bad ways. in this specific case, the ObjectInputStream reads a header from the stream on construction, which is happening before you have read the int from the stream. regardless, just use a single ObjectOutputStream and ObjectInputStream and ditch the Data streams (note that ObjectOutputStream has a writeInt method).

Android reading from Socket hangs on second read loop

I got to implement a chat in my application. Connection to a server is made using sockets. I should register to that server and the server will aknowledge that with a reply.
I have implemented this in a single method where I send the command using a BufferedWriter, and then start reading from the input stream until it tells me there is no more data.
I read properly the server reply. However, I never get the negative value from the second in.read call and thus my method stays blocked in the while loop (in the conditionnal statement where I make that call).
How should this be done with sockets? I usually do that with files or other input streams without problem.
If I should read only the bytes I am supposed to read, does that mean that I either have to:
Know in advance the length of the server response?
or make the server send a code to notify it has finished to send its response?
Currently I am doing the following:
private String sendSocketRequest(String request, boolean skipResponse) throws ChatException {
if (!isConnected()) openConnection();
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()), 2048);
out.append(request);
out.flush();
out = null;
} catch (IOException e) {
LogHelper.error("Unable to send socket request: " + request, e);
throw new ChatException("Unable to send socket request: " + request, e);
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()), 2048);
StringBuffer response = new StringBuffer();
char[] buffer = new char[2048];
int charsRead = -1;
// >>>>>>>> This is where it gets blocked <<<<<<<<<
while ((charsRead = in.read(buffer)) >= 0) {
if (charsRead > 0) response.append(new String(buffer, 0, charsRead));
}
return response.toString();
} catch (IOException e) {
LogHelper.error("Unable to read socket response: " + request, e);
throw new ChatException("Unable to read socket response: " + request, e);
}
}
Connection to the server is made with the following method:
public synchronized void openConnection() throws ChatException {
try {
socket = new Socket(Constants.API_CHAT_SERVER_ADDRESS, Constants.API_CHAT_SERVER_PORT);
socket.setKeepAlive(true);
LogHelper.debug("CHAT >> Connected to the chat server: " + Constants.API_CHAT_SERVER_ADDRESS);
} catch (UnknownHostException e) {
LogHelper.error("Unable to open chat connection", e);
throw new ChatException("Unable to open chat connection", e);
} catch (IOException e) {
LogHelper.error("Unable to open chat connection", e);
throw new ChatException("Unable to open chat connection", e);
}
}
The amount of data to be sent/received over a socket based connection is protocol dependend and not known to the TCP/IP stack, but only to the application layer.
The protocol used is developer dependend ... ;-) so coming to your questions:
If I should read only the bytes I am supposed to read, does that mean that I either have to:
Know in advance the length of the server response?
Yes, this is one possibility.
or make the server send a code to notify it has finished to send its response?
Also yes, as this is another possibility. Common markers are \n or \r\n. The NUL/'\0' character also might make sense.
A third option is to prefix each data chunk with a constant number of bytes describing the amount of bytes to come.
Instead of dealing with bytes, maybe it's simpler handling instances of ad-hoc classes, like - for instance - a Message class:
The server:
// Streams
protected ObjectInputStream fromBuffer = null;
protected ObjectOutputStream toBuffer = null;
// Listening for a new connection
ServerSocket serverConn = new ServerSocket(TCP_PORT);
socket = serverConn.accept();
toBuffer = new ObjectOutputStream(socket.getOutputStream());
fromBuffer = new ObjectInputStream(socket.getInputStream());
// Receiving a new Message object
Message data = (Message)fromBuffer.readObject();
The client then sends a message by simply:
// Sending a message
Message data = new Message("Hello");
toBuffer.writeObject(data);
Message can be as complex as needed as long as its members implement Serializable interface.

sending multiple byte array over the socket

I want to send multiple byte array from the client and server ?
I was able to send/Receive one byte array from client and send / Receive one byte array from server :
My code is like this :
server :
Socket sock=null;
ByteArrayOutputStream input=null;
OutputStream out=null;
InputStream in=null;
try{
ServerSocket server_sock=new ServerSocket(2972);
sock=server_sock.accept();
in =
sock.getInputStream();
out=sock.getOutputStream();
}catch(IOException e){
System.out.println(e.getMessage());
}
String word="";
//1-Receive
try{
ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
int len=0;
byte[] buf=new byte[1000];
while ((len = in.read(buf))>=0) {
serverinput.write(buf, 0, len);
}
sock.shutdownInput();
word=new String(serverinput.toByteArray());
System.out.println("Client send 1"+word);
}catch(Exception e){
System.out.println(e.getMessage());
}
String st="Server is a king";
try{
out.write(st.getBytes());
out.flush();
}catch(Exception e){
System.out.println(e.getMessage());
}
client :
Socket sock=null;
OutputStream out=null;
InputStream in=null;
try{
sock=new Socket("127.0.0.1",2972);
}catch(IOException e){
System.out.println(e.getMessage());
}
String word="Hellow World" ;
try{
in =
sock.getInputStream();
out=sock.getOutputStream();
}catch(IOException e){
System.out.println(e.getMessage());
}
//1- send
try{
System.out.println("Your string is"+word+"converted to byte"+word.getBytes());
out.write(word.getBytes());
out.flush();
sock.shutdownOutput();
}catch(Exception e){
System.out.println(e.getMessage());
}
try{ ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
int len=0;
byte[] buf=new byte[1000];
while ((len = in.read(buf))>=0) {
serverinput.write(buf, 0, len);
}
System.out.println("server send 1 "+new String(serverinput.toByteArray()));
System.out.println("Your string is"+word+"converted to byte"+word.getBytes());
}catch(Exception e){
System.out.println(e.getMessage());
}
This code is working fine for one submitting from client and server but it does not work when I want to send / receive more byte array ?
It is working only when I use shutdown because both client and server reading and writing to the data.
Therefore, I can not use the socket channel again ... is there is alternative solution? ...that does not lead to deadlock.
The problem you have is that you don't currently have any way to say when one byte array ends and the next one starts. (In your "one array" solution, the end of the byte array corresponds to the end of stream. And of course, once the stream has been ended / closed, it cannot be reopened without creating a new Socket, etcetera.)
The simple way to solve this is as follows, using DataOutputStream and DataInputStream pairs wrapped around the respective socket streams:
To send a byte array:
Convert data to bytes.
Send the byte array size using the DataOutputStream.writeInt(int) method.
Send the byte array using DataOutputStream.write(byte[]) method.
To receive a byte array:
Receive the byte array size using the DataInputStream.readInt() method.
Allocate a byte array of the required size.
Receive the bytes into the byte array using the DataInputStream.read(byte[], int, int) method ... repeatedly until you've gotten all of the bytes.
By sending the size of the byte array at the front, you tell the receiver how many bytes to read. You can repeat this process as many times as you need. The sender can indicate to the receiver that there are no more byte arrays to send by simply closing the socket stream.
Note - this is pseudo-code. I assume that you are capable of turning it into working Java.
Don't forget to insert BufferedInputStreams and BufferedOutputStreams into the respective stream chains ... to reduce system call overheads.
Try wrapping your socket streams in DataInputStream and DataOutputStream. That should allow you to do what you want.
You should really have a look at this tutorial : Reading from and Writing to a Socket
It seems to outline how to read and write to a socket. The reading should be as easy as creating a server socket and listening on the port you expect and then waiting for data.
You could also create an object that would hold your byte array and send it out with ObjectOutputStream, then use the writeObject(Object) method to send the info.

Java/Android: Reading/writing a byte array over a socket

I have an Android application where I'm trying to send a picture to a server. I did this using Base64 encoding and it worked quite well, but it took too much memory (and time) to encode the picture before sending it.
I'm trying to strip the Android application down to where it just simply sends the byte array and doesn't fiddle around with any kind of encoding scheme so it'll save as much memory and CPU cycles as possible.
This is what I would like the Android code to look like:
public String sendPicture(byte[] picture, String address) {
try {
Socket clientSocket = new Socket(address, 8000);
OutputStream out = clientSocket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.write(picture);
return in.readLine();
}
catch(IOException ioe) {
Log.v("test", ioe.getMessage());
}
return " ";
}
The server is written in Java. How do I write the server code so I can properly retrieve the exact same byte array? My goal is to save as many CPU cycles on the Android as possible.
So far, all the methods I've tried resulted in corrupt data or a thrown exception.
Any help will be appreciated.
Try something like this:
public byte[] getPicture(InputStream in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int data;
while ((data = in.read())>=0) {
out.write(data);
}
return out.toByteArray();
} catch(IOException ioe) {
//handle it
}
return new byte[]{};
}
Based on Robert's and Zaki's comment, here is the modified code that should perform better.
public byte[] getPicture(InputStream in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int length = 0;
while ((length = in.read(data))!=-1) {
out.write(data,0,length);
}
return out.toByteArray();
} catch(IOException ioe) {
//handle it
}
return null;
}
If you want bi-directional communication, the server must know when you're ready - you should prepend a 4 byte length field to your sender side indicating the number of bytes to come.
On the server side you read the length and then stay listening until everything has arrived. Then you can reply your acknowledge string.
If it is enough to send only the picture, you can simply send the data and then close the connection. The server side is implemented as shown by #thejh.

Categories