This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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:
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....
If you simply use serialization, you don't have to worry about it at all - it will just work automagically.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Dec 01, 2003
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
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?
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.