开发者

How to use BufferedWriter in socket communication in java?

开发者 https://www.devze.com 2023-04-10 22:39 出处:网络
I am trying to send a string through sockets but I just have some problems. the string that I am trying to send is ; (ATTENTION : It is a string NOT XML )

I am trying to send a string through sockets but I just have some problems. the string that I am trying to send is ; (ATTENTION : It is a string NOT XML )

<message>
<header>
<messageType>snmp</messageType>
<sendFrom>192.168.0.16</sendFrom>
<hostName>oghmasysMehmet</hostName>
<sendTo>192.168.0.12</sendTo>开发者_运维问答
<receiverName>Mehmet</receiverName>
<date>03/10/2011</date>
</header>
<body>
<snmpType>getbulk</snmpType>
<ip>127.0.0.1</ip>
<port>161</port>
<oids>
  <oid>1.3.6.1.2.1.1.3.0</oid>
</oids>
<community>community</community>
<nR>0</nR>
<mR>5</mR>
</body>
</message>

But when I look what I get from server it is just ;

<?xml version="1.0" encoding="UTF-8"?>

I dont know what is the problem:

I am using ,

socket = new Socket(localIP, Integer.parseInt(localPort));
out = new PrintWriter(socket.getOutputStream(), true);

to send the string from client and use,

in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = in.readLine();

to read the string on server.

Can you please help me how I can solve it ?

Thank you all


Lucas has the answer, I believe. Personally, I don't trust readLine() as in doing so is making the assumption that you are recv-ing valid String data on the socket.... which is a very poor assumption!

Try reading into a byte buffer first, then try to make it into a string:

byte[] buf = new byte[4096];

int actualNumberOfBytesRead = socket.getInputStream().read(buf);

String dataString = new String(buf, 0, actualNumberOfBytesRead);

...Now, my example has a few issues to it, but the point (should) be made =D


Could be a missing newline character as BufferedReader.readLine will not return until a new line character is available on the stream.


Well you've written out some XML which contains more than a single line. The first line is:

<?xml version="1.0" encoding="UTF-8"?>

On the server you've then read the first line - you've got one readLine() call. Unsurprisingly, that's giving you a single line of text.

If you want to send a string including line breaks as a single line, you'll need to escape those line breaks.

A nicer alternative is not to rely on lines at all: length prefix each string you send, so you send:

  • Length (in bytes) as 4 bytes
  • String data (as a byte array)

On the reading side, you'd read 4 bytes to find out how long the message is, then read the message into a byte array, then construct the string from that byte array.

DataInputStream and DataOutputStream can help you do that reasonably simply. Note that you should definitely specify the encoding you use for all strings; UTF-8 is a good starting point in most cases.


I think you should be looping over in.readline until no further input available.

Check example on the http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html


First of all: You should really be using a buffer to parse whatever the server sent to you. See http://www.kodejava.org/examples/266.html for a good example.

If you hate adding this to your code, you can use the Apache Commons IO library to do the following:

ByteArrayInputStream out = new ByteArrayInputStream();
IOUtils.readFully( in, out );
String output = new String( out.getBytes(), "UTF-8");

Please note you should take care to flush() the stream server-side. Let the client close the socket as soon as all data is received. This way, you cannot close() the socket too early.

0

精彩评论

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

关注公众号