• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

data lenght mismatch in socket code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sending byte[] data through a socket from an MDB in Websphere to a stand alone socket server. When I do a display of the size of the data on the MDB "client" end it shows that the size of the field is 2807 bytes. On the server side it display 12 bytes. When I display the contents of the field on either side they are identical. I have included my code snippets.

client side (MDB):
PrintWriter out = null;
BufferedOutputStream out1 = null;
BufferedReader in = null;
String lineRead = null;
String input = " ";
byte[] compressedData = null;
try{
out1 = new BufferedOutputStream(soc.getOutputStream());
out = new PrintWriter(out1,true);
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
out.println(rawData + "\n");
out.flush();
while ((lineRead = in.readLine()) != null){
if (lineRead.length() == 0) break;
input = input + lineRead.getBytes();
logger.debug("this is what's in input " + input);
}
if (input == null){
logger.debug("input is null");
String input1 = "stuff";
compressedData = input1.getBytes();
}else{
logger.debug("Here is the stuff" + input);
compressedData = input.getBytes();
}

}catch(UnknownHostException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally {
try{
logger.debug("closing sockets and streams");
out.close();
out1.close();
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
return compressedData;
}

server side (stand alone socket server)
PrintWriter out = null;
BufferedReader in = null;
byte[] compressedData = null;
try{
out = new PrintWriter(new BufferedOutputStream(server.getOutputStream()));
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
while ((inputLine = in.readLine()) != null ){
if (inputLine.length() == 0) break;
outputLine = outputLine + inputLine;
System.out.println("Here's the old data array: " + outputLine.toString() + " " + outputLine.length());
newDataArray = outputLine.getBytes();
}
out.println("I am out of here. \n");
out.flush();

}catch(UnknownHostException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally {
try{
System.out.println("closing sockets and streams");
out.close();
in.close();
//outData.close();
server.close();
}catch(Exception e){
e.printStackTrace();
}
}
return compressedData;
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when posting code. Makes it much easier to read.
You haven't shown us what your actual input and output are and we can't execute your code, so I can only guess what the problem is.
It is a Bad Idea to use Strings for binary data. Java String objects are Unicode encoded, not plain binary data, so you are likely to lose data as the conversion is performed.
Also, socket streams support reading/writing byte arrays, so you are adding overhead to a simple data transmission.
It's also a Bad Idea to use BufferedReader.readLine() to read data from a socket for the reasons stated here.
 
Ralph Philistine
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry about that. The data is an xml file that is converted to a byte array.

I have changed my approach a little and am now using the following code for the client:



and the following for the server:



at this point I am not real interested in what gets back to the client, but I will be, I just want to see the same thing on both sides.

Am I on the right track with this approach? The lengths are still different on both sides and so is the data.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ralph Philistine:
Am I on the right track with this approach?



As near as I can tell, you are sending an int length then an array of data to the server. You read everything into a single buffer on the server, then sending back the size of the data that is read (remember, read() does not necessarily read all the data). On the client side, you read the reply but print out the length of the reply, not the value of the reply.
So there are a couple of places in your code things can go wrong.
 
Ralph Philistine
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to resolve my problem by using the following code:

This code read in the byte array based on the size of the array. The compression piece is superfulous with regards to the socket implementation. Thanks for leading me in the right direction.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic