场景,我有一个服务端,客户端是超级网口USR-K3的TCPClient。K3发送消息给我服务端时,已经跟我建立连接,服务端已经看到了它的IP和端口,但是在取出K3发过来的值时,显示为“null”。求大神告知是不是我取值的方法和K3放值得方法不一样才取不出值。
public void run()
{
try
{
// 采用循环不断从Socket中读取客户端发送过来的数据
while (true) {
content = readFromClient();
OutputStream os = socket.getOutputStream();
os.write(("123" + "\n").getBytes("utf-8"));
System.out.println(content);
System.out.println("success");
os.flush();
if (content == null) {
System.out.println(content);
System.out.println("success");
os.write(("456" + "\n").getBytes("utf-8"));
os.flush();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
//定义读取客户端数据的方法
private String readFromClient()
{
try
{
str = br.readLine();
return str;
}
//如果捕捉到异常,表明该Socket对应的客户端已经关闭
catch (IOException e)
{
try {
br.close();
reader.close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}