一个BufferedReader如何与Socket进行交互?关于Socket的工作原理。

huangapple go评论81阅读模式
英文:

How does a BufferedReader interact with a Socket? About how Sockets work

问题

我一直在按照教程学习如何在Java中构建简单的聊天应用程序。我已经基本了解了服务器和客户端如何通信的原理。到目前为止,我使用了PrintWriter来发送信息,并使用BufferedReader来接收双方的信息。但是我不明白的是为什么这不会导致错误?

Socket soc = new Socket(ipAddress, port);
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream));
while (true) {
    String str = in.readLine();
}

我的问题是,in.readLine()语句是否在“等待”输入?如果不是,如果使用str而它没有值,程序不应该抛出错误吗?或者一般来说,像Socket soc = serverSocket.accept();这样的Socket特定函数,或者像上面例子中读取InputStream一样,是否会“等待”任何信号?我是否漏掉了关于Socket的一些基本概念?如果有人能帮助我进一步理解这个主题,我会非常感谢。

提前感谢您的帮助,

Appa

英文:

I have been following a tutorial on how to build a simple chat application in Java. I get the basic principle of how sever and client communicate. So far I used a PrintWriter to send, and a BufferedReader to recieve information for both sides. But what I don´t get is how this does not lead to errors?

Socket soc = new Socket(ipAddress, port);
BufferedReader in = new BufferedReader( new InputStreamReader(soc.getInputStream));
while(true) {
			
	String str = in.readLine();

}

My question would be the following. Is the in.readLine() statement "waiting" for input? If it does not, shouldn´t the program throw Errors if using the str since it has no value? Or in general terms, do Socket specific functions like Socket soc = serverSocket.accept(); or as in the example given above the reading of an InputStream "wait" for a signal whatsoever? Did I miss some essential concept about Sockets? It would be great if someone could help me understand this topic further.

Thank you in advance,

Appa

答案1

得分: 1

我的问题是这样的。in.readLine() 语句是否在等待输入?

是的,它在等待输入,因为.readLine()是一个阻塞方法。

如果不等待,如果使用字符串(str)而它又没有值,程序不应该抛出错误吗?

我无法解析这个问题 —— 这个陈述没有意义。你期望会有什么错误或异常?

或者用更通俗的话说,像 Socket soc = serverSocket.accept(); 这样的套接字特定函数,或者如上面给出的从输入流读取,在等待信号吗?

再次地,我担心这个问题不太清楚(至少对我来说是这样)。ServerSocket#accept() 一个阻塞方法,会等待直到可以建立套接字连接,如果这就是你的问题。


注意,这段代码:

while (true) {
    String str = in.readLine();
}

虽然有效,但可能不是特别有用,因为你会读取一个字符串然后丢弃它,没有对它进行任何操作。最好要么打印获取到的字符串,要么将它传递到一个方法中,在方法中可以以某种方式使用它。另一方面,我想如果你只想“吞掉”流以防止操作系统的缓冲区溢出,这可能是有用的。

英文:

> My question would be the following. Is the in.readLine() statement "waiting" for input?

Yes, it is "waiting for input" since .readLine() is a blocking method

> If it does not, shouldn´t the program throw Errors if using the str since it has no value?

I cannot parse this -- the statement doesn't make sense. What error / exception would you be expecting?

> Or in general terms, do Socket specific functions like Socket soc = serverSocket.accept(); or as in the example given above the reading of an InputStream "wait" for a signal whatsoever?

Again, I'm afraid that this is not too clear (at least to me). ServerSocket#accept() is a blocking method and will wait until a socket connection can be made, if that is what you are asking.


Note that thiscode:

while(true) {
    String str = in.readLine();
}

while valid, would not be particularly useful, since you would be reading a String and then discarding it, doing nothing with it. Better to either print the String obtained, or pass it into a method where it can be used in some way. On the other hand, I suppose this could be useful if all you wanted to do was to "gobble" the stream to prevent the OS's buffers from overflowing.

答案2

得分: 0

是的,readLine实际上会阻塞。它会等待直到读取到回车符/换行符或套接字关闭。

英文:

Yes readLine does in fact block. It waits till you read a carriage return/line feed, or the socket closes.

huangapple
  • 本文由 发表于 2020年4月5日 22:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61044121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定