我正在向服务器发送字节数组,服务器应该接收我发送的数据。但服务器需要很长时间才能收到数据。服务器正在Inputstream中等待。
如果我通过转换为字节发送字符串数据,那么服务器将接收。我不知道发生了什么。请帮帮我。
客户:
void Send(byte []arr)
{
//Other code
String s=new String(arr);
byte []msgByte=s.getBytes();
try
{
outStream.write(msgByte);
}
catch(Exception e)
{}
//Other Code
}
服务器:
InputStream inStream1=connection.openInputStream();
BufferedReader bReader1=new BufferedReader(new InputStreamReader(inStream1));
String lineRead=bReader1.readLine();
System.out.println(lineRead);
inStream1.close();
答案 0 :(得分:0)
您需要添加' \ n' (新行字符)在消息的末尾,因为您希望在服务器上读取一行,并且您还需要在写入消息后刷新流,因为默认情况下系统不会自动刷新它,并且刷新取决于使用的OutputStream
的类型。
void Send(byte[] arr) {
// Other code
String s = new String(arr) + "\n"; // appending '\n'
byte[] msgByte = s.getBytes();
try {
outStream.write(msgByte);
} catch (Exception e) {
}
outStream.flush(); //flushing the stream
// Other Code
}
答案 1 :(得分:0)
尝试使用outStream.close()