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);and here i'm sending the dataCode://Setting up channel to send data to outputserv PrintWriter out = new PrintWriter(new OutputStreamWriter(outputServ .getOutputStream()));
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.




Reply With Quote