简体   繁体   English

如何在套接字中读取大量数据并写入套接字通道

[英]How to read huge data in socket and also write into socketchannel

How to read a very big data using DataInputStream of socket If the data is in String format and having a length of more than 1,00,000 characters. 如何使用套接字的DataInputStream读取非常大的数据如果数据为String格式,并且长度超过1,00,000个字符。

Also how to write that big data using SocketChannel in java? 另外如何在Java中使用SocketChannel写入大数据?

The problem is that your data is arriving in chunks. 问题在于您的数据正在分块到达。 Either the packet size is limiting that or maybe DataInputStream has an internal buffer of only 40k. 数据包大小限制了数据输入流,或者DataInputStream的内部缓冲区只有40k。 I don't know, but it doesn't matter. 我不知道,但这没关系。 Either way, all 1000000 bytes will not arrive at once. 无论哪种方式,所有1000000字节都不会一次到达。 So you have to rewrite your program to expect that. 因此,您必须重写程序才能实现这一目标。 You need to read the smaller chunks that you receive and store them in another byte[1000000] variable (keeping track of where your last byte index). 您需要读取收到的较小的块并将它们存储在另一个byte [1000000]变量中(跟踪最后一个字节索引的位置)。 Keep looping until you are done reading the socket. 继续循环播放,直到完成阅读套接字为止。 Then you can work with your internal variable. 然后,您可以使用内部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM