如何正确读取套接字数据包

时间:2016-08-10 20:47:21

标签: java

我有一个用Java构建的服务器,它在特定端口监听传入连接。但是当我读取数据包的行时,它就会卡住。

以下是我的代码中读取数据包数据的部分:

BufferedReader BR = new BufferedReader(IR);
System.out.println("Received Request!");
String MESSAGE = null;
Stream<String> ReadPacket = null;

try {
    dOut = new DataOutputStream(socket.getOutputStream());
} catch(Exception ie) {
    System.out.println("Cound'nt create dOut");
}

try {
    IR = new InputStreamReader(socket.getInputStream());
} catch(Exception ie) {
    System.out.println("Cound'nt create IR");
}

BufferedReader BR = new BufferedReader(IR);
System.out.println("Received Request!");
String MESSAGE = null;
Stream<String> ReadPacket = null;

try {
    ReadPacket = BR.lines();
} catch(Exception ie) {
    System.out.println("Couldn't Receive Message");
}

Object[] arr = ReadPacket.toArray();
MESSAGE = arr[0].toString();
System.out.println(MESSAGE);

正如您所看到的,它会读取数据包的行,然后将其转换为String,但它会陷入循环并且永远不会打印我MESSAGE。直到我关闭我的标签,我要求它离开循环。

0 个答案:

没有答案