|
-
July 10th, 2007, 02:27 PM
#1
Bytes sent and recieved from sockets?
Hello everyone i'm using java and I'm having issues figuring out how many bytes are sent/recieved from a socket.
I'm sending a string from a client socket to a server.
I can't find any built in functions from the PrintWriter stream nor the
DataInputStream input;
DataOutputStream output; that returns how many bytes are sent, nor does it show how many are recieved.
I'm using the following to send the string via streams to the Server through a client socket (outputServ) the Server is running on port 1234, and its hostname is "localhost";
Code:
Socket outputServ = new Socket("localhost", 1234);
Code:
//Setting up channel to send data to outputserv
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputServ
.getOutputStream()));
and here i'm sending the data
Code:
//sending the string version of the message object to the
//output server
out.println(msg.toString());
here's my code
Code:
public class MultiThreadServer implements Runnable {
Socket csocket;
MultiThreadServer(Socket csocket) {
this.csocket = csocket;
}
public void run() {
//setting up sockets
Socket outputServ = null;
//create a message database to store events
MessageDB testDB = new MessageDB();
try {
//setting up channel to recieve events from the omnibus server
BufferedReader in = new BufferedReader(new InputStreamReader(
csocket.getInputStream()));
//This socket will be used to communicate with the z/OS reciever
//we will need a new socket each time because this is a multi-threaded
//server thus, the z/OS reciever (outputServ) will need to be
//multi threaded to handle all the output.
outputServ = new Socket("localhost", 1234);
//Setting up channel to send data to outputserv
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputServ
.getOutputStream()));
String input;
//accepting events from omnibus server and storing them
//in a string for later processing.
while ((input = in.readLine()) != null) {
//accepting and printing out events from omnibus server
//also printing out connected client information
System.out.println("Event from: "
+ csocket.getInetAddress().getHostName() + "-> "
+ input + "\n");
System.out.println("Waiting for data...");
//sending the string version of the message object to the
//output server
out.println(msg.toString());
System.out.println("Waiting for data...");
out.flush();
}
//cleaning up
System.out.println("Connection closed by client.");
in.close();
out.close();
outputServ.close();
csocket.close();
}
catch (SocketException e) {
System.err.println("Socket error: " + e);
}
catch (UnknownHostException e) {
System.out.println("Unknown host: " + e);
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}
}
My solution which isn't a good one is too count the string before its being sent and tag on the size of the string being sent, then the other socket will read that string and parse out the "size" of the string, and compare it to the actual size of the string the server got.
-
July 15th, 2007, 01:46 PM
#2
Re: Bytes sent and recieved from sockets?
I don't program sockets in Java, but your solution (pre-pending the length of the string to the string's transmission) is actually a common one.
It's a good idea, since TCP is simply a streaming protocol that sends a stream of bytes, which it is permitted to group together however it wants. Thus, in TCP, its' possible that two separate sends of two different strings are grouped into one transmission, and received together in one single recv. Without the length being pre-pended to the transmission, the application would have no way of knowing where one string ends and the second begins.
Incidentally, it's also possible for TCP to break a single send into multiple parts. Again, the pre-pended length is needed by the application to know when a completed string has been received.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|