java中如何使用BufferedWriter进行socket通信?
我正在尝试通过套接字发送字符串,但遇到一些问题。我试图发送的字符串是; (注意:它是一个字符串而不是 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>
但是当我查看从服务器获取的内容时,它只是;
<?xml version="1.0" encoding="UTF-8"?>
我不知道问题是什么:
我正在使用 ,
socket = new Socket(localIP, Integer.parseInt(localPort));
out = new PrintWriter(socket.getOutputStream(), true);
从客户端发送字符串并使用,
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = in.readLine();
读取服务器上的字符串。
你能帮我解决这个问题吗?
谢谢大家
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信卢卡斯有答案。就我个人而言,我不相信 readLine() ,因为这样做是假设您正在套接字上接收有效的字符串数据......这是一个非常糟糕的假设!
首先尝试读入字节缓冲区,然后尝试将其转换为字符串:
...现在,我的示例有一些问题,但(应该)指出=D
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:
...Now, my example has a few issues to it, but the point (should) be made =D
可能是缺少换行符,因为 BufferedReader.readLine 在流中出现新行字符之前不会返回。
Could be a missing newline character as BufferedReader.readLine will not return until a new line character is available on the stream.
好吧,您已经编写了一些包含多行的 XML。第一行是:
在服务器上,您已读取第一行 - 您有一个
readLine()
调用。毫不奇怪,这会为您提供一行文本。如果您想将包含换行符的字符串作为单行发送,则需要转义这些换行符。
更好的替代方案是根本不依赖于行: length 为您发送的每个字符串添加前缀,因此您发送:
在读取端,您将读取 4 个字节找出消息的长度,然后将消息读入字节数组,然后从该字节数组构造字符串。
DataInputStream
和DataOutputStream
可以帮助您相当简单地做到这一点。请注意,您绝对应该指定所有字符串使用的编码;在大多数情况下,UTF-8 是一个很好的起点。Well you've written out some XML which contains more than a single line. The first line is:
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:
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
andDataOutputStream
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.我认为你应该循环 in.readline 直到没有进一步的输入可用。
检查 http://download.oracle.com/javase/tutorial 上的示例/networking/sockets/readingWriting.html
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
首先:您确实应该使用缓冲区来解析服务器发送给您的任何内容。请参阅 http://www.kodejava.org/examples/266.html 了解更多信息例子。
如果您讨厌将其添加到代码中,您可以使用 Apache Commons IO 库执行以下操作:
请注意,您应该注意在服务器端刷新()流。让客户端在收到所有数据后立即关闭套接字。这样,您就不能过早 close() 套接字。
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:
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.