【发布时间】:2014-04-05 04:37:10
【问题描述】:
我写的代码可以向服务器发送消息。问题是如果我在 Wireshark 中捕获通信,我的应用程序发送的字符串消息如下所示:
hello - 1 packet
如果我检查从 Telnet cmd 发送的相同消息,终端消息如下所示:
h - 1 packet
e - 1 packet
l - 1 packet
l - 1 packet
o - 1 packet
所以最后它按字符发送了整个字符串。服务器可以从 cmd Telnet 终端读取消息并回复,但无法读取从我的应用程序发送的消息。有什么方法可以发送这样的字符串吗?我不是在编程服务器。我只在客户端编程,所以重要的是服务器必须能够正确读取消息。非常感谢!
PrintWriter out;
BufferedReader in;
public void run() {
// TODO Auto-generated method stub
try {
InetAddress serverAddr = InetAddress.getByName(hostname);
// create a socket to make the connection with the server
socket = new Socket(serverAddr, port);
Log.i("Terminal", "Socket connecting");
try {
// send the message to the server
out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
Log.i("Terminal", "Connected.");
// receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
}catch...
}catch...
}
//send code
public void sendMessage(String message) {
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
Log.i("Terminal", "Message sent.");
}
}
【问题讨论】:
-
请定义“无法阅读从我的应用程序发送的消息”。服务器无法区分这两种情况。您一定是做错了什么,例如不正确或缺少线路终止。
标签: java networking network-programming telnet wireshark