Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I specify size of a String?

 
Sean Hetfield
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to send different Strings through a network with different Sizes, starting from 0 byte to up. How can I define that?
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a String array?

String[] array = new String[size];
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you want to send them ? if you are sending just the bytes over the wire (e.g. i you do not use java serialization / rmi stuff) and you want to send several strings at once (if you just send one string, then just use all the bytes you receive), then you probably need to send the string length or use a stop sequence:

or

if you send the length, then you need to know how many bytes you use for the length information (e.g 4 bytes length, then as many bytes an in the length information, then 4 bytes length infor.... and so on and on and on). if you use a stop sequence (e.g. a line break) then you need to make sure, that this sequence can never occur in the string itself.

did that help ?
if not: perhaps you can tell us what you want to achieve and we can give more tips....


pascal
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you simply use serialization, you don't have to worry about it at all - it will just work automagically.
 
Sean Hetfield
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using this code in both Client and Server to send a message through a socket:

Socket socket = new Socket(address,port);//initiate a socket object

OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);//create a PrintWriter object for output

InputStream sIn = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(sIn);
BufferedReader br = new BufferedReader(isr);//create a BufferReader object for input


pw.println("Client sent a message");
pw.flush(); //Send the message to Server

long sendTime = System.currentTimeMillis();

String inString = br.readLine();

long returnTime = System.currentTimeMillis();


So I need send different String Sizes such as 50, 150, 9000, 128k one at a time and calculate the latency time and draw the chart.
So What I don't know is, because I use BufferedReader.readLine(), how to define String with these sizes in Client at the time of send?

Thanks
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're trying to measure latency, I would avoid using Strings (and Readers) and stick with byte arrays and Input/OutputStream. By using Strings and Readers, you are also measuring the time it takes to convert the String to a byte array in your calculations, giving you incorrect results.

I would recommend creating a byte array of the maximum size you need (again to avoid including looping time in your calculations) and then use OutputStream.write(buffer, 0, length). You can very length and reuse the same buffer. You might also want to use java.util.Random to fill the byte array just to be super cool.

If both client and server know the lengths to expect, You can avoid sending the length before the "string" of bytes. Otherwise, DataOutputStream will help you as you can send an int and then the byte array. DataInputStream has the readFully(byte[], offset, length) method that could prove useful.

Note also that sending 128k bytes will not be a single packet, so you'll definitely be including a lot of time for writing/reading to/from the socket, but I suppose that's what the assignment asks so it's fine.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm. Sockets, RMI, clients and servers, measuring latency ...

This sounds way, way too advanced for beginners. I'm moving this to the Java in General(intermediate) forum.
[ January 31, 2005: Message edited by: Marilyn de Queiroz ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic