开发者

Error while writing a big string on a socket ?

开发者 https://www.devze.com 2023-04-12 18:23 出处:网络
Hello all my friends, I am trying to send a long string through socket connection but I have them in two parts so I get an error while doing my processs.

Hello all my friends,

I am trying to send a long string through socket connection but I have them in two parts so I get an error while doing my processs.

In client I am sending the file,

BufferedWriter bufferedOut = null;
BufferedReader in = null;
socket = new Socket("192.168.0.15",4444);
bufferedOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

bufferedOut.write(xmlInString, 0, xmlInString.length());

/**
* wait for response
*/

byte[] buf = new byte[10000];
int actualNumberOfBytesRead = socket.getInputStream().read(buf);
String responseLine = new String(buf, 0, actualNumberO开发者_如何转开发fBytesRead);

In the server,

 BufferedReader in = null;
 PrintWriter out = null;
 in = new BufferedReader(new InputStreamReader(client.getInputStream()));
 out = new PrintWriter(client.getOutputStream(), true);

//get the input
 byte[] buf = new byte[10000];
 int actualNumberOfBytesRead = client.getInputStream().read(buf);
 line = new String(buf, 0, actualNumberOfBytesRead);
 //send back
 out.println(result);

How I can get my string as one part ? Can you please show me where is my mistake on the code ?

Thank you all


You will need a loop to repeatedly read from the input stream, concatenating the read data together each time, until you reach the end of the string.

Edit - a little more detail. If you are looking at transmitting multiple such strings/files, then see @arnaud´s answer. If all your looking to to is send 1 big string then:

  1. On the sender side, create the output stream, send the data (as you have done), and then don't forget to close the stream again (this will also perform a flush which ensure the data gets sent over the wire, and informs the other end that there is no more data to come).

  2. On the recipient site, read the data in a loop until the input stream ends (read(buf) returns -1), concatenating the data together each time in one big buffer, then close the input stream.

Also, please read my comment about sending a file as bytes rather than a string. This is particularly important for XML files, which have rather special rules for encoding detection.


When using a TCP socket, you are handling "streams". That is, there is no delimitation between messages by default. By proceeding as you do, you may read part of a message, or worse, read more than a message.

The most common way to proceed is to delimit your messages. You can use DataInputStream/DataOutputStream which encodes strings into bytes and use the first bytes to indicate it's length. That way, it knows how many bytes it should read on the receiver end.

    DataOutputStream out = null;
    DataInputStream in = null;
    Socket socket = new Socket("192.168.0.15",4444);
    out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
    in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

    out.writeUTF(xmlInString);
    out.flush(); // to ensure everything is sent and nothing is kept in the buffer.

    // wait for response

    String responseLine = in.readUTF();

Then, adjust the server code accordingly.

When using Buffered outputs with sockets, which is advised for performance reasons, it is advised to flush() after you wrote the message to ensure that everything is actually sent over the network and nothing is kept in the buffer.

Your initial problem probably occurred because your message requires several TCP/IP packets and in your server, you read only the first one(s) which just arrived.

0

精彩评论

暂无评论...
验证码 换一张
取 消