简体   繁体   English

从套接字输入流读取

[英]Reading from socket input stream

I'm trying to determine the best way to transfer data through a socket between a client and server. 我正在尝试确定通过客户端和服务器之间的套接字传输数据的最佳方法。 Currently I have a BufferedReader that reads one character at a time (or however many characters have arrived since the last iteration). 目前,我有一个BufferedReader,它一次读取一个字符(或者,自上次迭代以来,已经到达了许多字符)。 Through each iteration, it pulls the data received so far and puts it into an array. 在每次迭代中,它会将到目前为止收到的数据提取到一个数组中。 When the '|' 当“ |” character is read, it knows that the current instruction is done. 字符被读取,它知道当前指令已完成。

I know what I have so far is grossly inefficient and burns the CPU, but I'm a little unclear as to the differences between all the ways to read from a socket input stream. 我知道到目前为止,我的工作效率非常低下,并且烧毁了CPU,但是对于从套接字输入流读取数据的所有方式之间的差异,我还不清楚。 What would I use to not have to read each character at a time, but rather to wait until the input stream is finished receiving the current instruction (which would be terminated by "\\n")? 我将不必一次读取每个字符,而是等到输入流完成接收当前指令(该指令将以“ \\ n”终止)后使用什么呢?

I find the best way is to create a new thread for each socket/client that listens for input. 我发现最好的方法是为每个侦听输入的套接字/客户端创建一个新线程。 readLine() always works for me fine. readLine()总是对我有用。 Perhaps this might help you out a bit. 也许可能会对您有所帮助。

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

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