java inputstream in_如何在服务器套接字JAVA中读取所有Inputstream

小编典典

例如,你从服务器发送了100字节的事实并不意味着你在第一次读取时将在客户端中读取100字节。从服务器发送的字节可能在几个TCP段中到达客户端。

你需要实现一个循环,在该循环中你将阅读直到收到整个消息为止。让我提供一个示例,以DataInputStream代替BufferedinputStream。举一个简单的例子。

假设你事先知道服务器要发送100字节的数据。

在客户端中,你需要编写:

byte[] messageByte = new byte[1000];

boolean end = false;

String dataString = "";

try

{

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

while(!end)

{

int bytesRead = in.read(messageByte);

dataString += new String(messageByte, 0, bytesRead);

if (dataString.length == 100)

{

end = true;

}

}

System.out.println("MESSAGE: " + dataString);

}

catch (Exception e)

{

e.printStackTrace();

}

现在,通常一个节点(这里的服务器)发送的数据大小通常是未知的。然后,你需要定义自己的小型协议,用于与TCP进行通信的服务器和客户端(或任何两个节点)之间的通信。

最常见和最简单的方法是定义TLV:类型,长度,值。因此,你定义了从服务器发送到客户端的每条消息都附带:

1个字节,指示类型(例如,它也可以是2或任何数字)。

1字节(或任何长度)的消息长度

N个字节的值(N表示长度)。

因此,你知道你必须至少接收2个字节,而第二个字节则知道需要读取多少个后续字节。

这只是可能协议的建议。你也可以摆脱“类型”。

因此,它将类似于:

byte[] messageByte = new byte[1000];

boolean end = false;

String dataString = "";

try

{

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

int bytesRead = 0;

messageByte[0] = in.readByte();

messageByte[1] = in.readByte();

int bytesToRead = messageByte[1];

while(!end)

{

bytesRead = in.read(messageByte);

dataString += new String(messageByte, 0, bytesRead);

if (dataString.length == bytesToRead )

{

end = true;

}

}

System.out.println("MESSAGE: " + dataString);

}

catch (Exception e)

{

e.printStackTrace();

}

以下代码可以编译并看起来更好。假定提供长度的前两个字节以网络格式(大字节序)以二进制格式到达。对于邮件的其余部分,不关注不同的编码类型。

import java.nio.ByteBuffer;

import java.io.DataInputStream;

import java.net.ServerSocket;

import java.net.Socket;

class Test

{

public static void main(String[] args)

{

byte[] messageByte = new byte[1000];

boolean end = false;

String dataString = "";

try

{

Socket clientSocket;

ServerSocket server;

server = new ServerSocket(30501, 100);

clientSocket = server.accept();

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

int bytesRead = 0;

messageByte[0] = in.readByte();

messageByte[1] = in.readByte();

ByteBuffer byteBuffer = ByteBuffer.wrap(messageByte, 0, 2);

int bytesToRead = byteBuffer.getShort();

System.out.println("About to read " + bytesToRead + " octets");

//The following code shows in detail how to read from a TCP socket

while(!end)

{

bytesRead = in.read(messageByte);

dataString += new String(messageByte, 0, bytesRead);

if (dataString.length() == bytesToRead )

{

end = true;

}

}

//All the code in the loop can be replaced by these two lines

//in.readFully(messageByte, 0, bytesToRead);

//dataString = new String(messageByte, 0, bytesToRead);

System.out.println("MESSAGE: " + dataString);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

2020-03-25

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值