套接字编程:输入流似乎阻塞输出流
我正在尝试编写一个服务器-客户端通信应用程序,该应用程序使用两个单独的线程,一个用于输入,一个用于输出。不过,我遇到了一个奇怪的“死锁”问题:当一个线程读取输入,但客户端没有发送任何内容时,该线程将停止(因为它正在等待输入)。但是,由于某种原因,当输入线程被阻塞时,输出线程无法写入任何内容。
此代码示例对此进行了说明:
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class TestServer {
public static void main(String... args) throws IOException {
/* Creates a server socket that lurks about our port waiting for connections */
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(new InetSocketAddress(4114));
while(true){
SocketChannel connectionChannel = serverChannel.accept();
if(connectionChannel != null){
final Socket connection = connectionChannel.socket();
new Thread(){
public void run(){
try {
System.out.println("READING");
System.out.flush();
// If the next line is commented out, nothing blocks
connection.getInputStream().read();
System.out.println("DONE READING");
System.out.flush();
} catch (Exception e){
e.printStackTrace();
}
}
}.start();
new Thread(){
public void run(){
try {
System.out.println("WRITING");
System.out.flush();
new DataOutputStream(connection.getOutputStream()).writeBytes("AUGH!!!");
//connection.getOutputStream().write(5);
System.out.println("DONE WRITING");
System.out.flush();
} catch (Exception e){
e.printStackTrace();
}
}
}.start();
break;
}
}
}
}
客户端代码:
import java.net.*;
import java.io.*;
public class TestClient {
public static void main(String... args) throws IOException {
Socket connection = new Socket("127.0.0.1", 4114);
while(true){
System.out.println(connection.getInputStream().read());
}
}
}
上面的代码示例会阻塞,但如果服务器中的行被注释掉,则不会阻塞。这是为什么?套接字是否仅限于同时等待输入/输出?这是怎么回事?
I'm trying to code a server-client communicating application which uses two separate threads, one for input and one for output. I'm having a weird "deadlock" issue, though: when one thread reads input, but the client hasn't sent anything, the thread stops (since it's waiting for input). However, for some reason, while the input thread is blocked, the output thread can't write anything.
This is illustrated in this code sample:
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class TestServer {
public static void main(String... args) throws IOException {
/* Creates a server socket that lurks about our port waiting for connections */
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.socket().bind(new InetSocketAddress(4114));
while(true){
SocketChannel connectionChannel = serverChannel.accept();
if(connectionChannel != null){
final Socket connection = connectionChannel.socket();
new Thread(){
public void run(){
try {
System.out.println("READING");
System.out.flush();
// If the next line is commented out, nothing blocks
connection.getInputStream().read();
System.out.println("DONE READING");
System.out.flush();
} catch (Exception e){
e.printStackTrace();
}
}
}.start();
new Thread(){
public void run(){
try {
System.out.println("WRITING");
System.out.flush();
new DataOutputStream(connection.getOutputStream()).writeBytes("AUGH!!!");
//connection.getOutputStream().write(5);
System.out.println("DONE WRITING");
System.out.flush();
} catch (Exception e){
e.printStackTrace();
}
}
}.start();
break;
}
}
}
}
And the client code:
import java.net.*;
import java.io.*;
public class TestClient {
public static void main(String... args) throws IOException {
Socket connection = new Socket("127.0.0.1", 4114);
while(true){
System.out.println(connection.getInputStream().read());
}
}
}
The code sample above blocks, but if the line in the server is commented out it doesn't. Why is that? Is a socket limited to only waiting for input/output at the same time? What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你为什么会看到这个,但这与使用频道有关。
如果您替换此代码,
它将按您想要的方式工作。
I'm not sure why you are seeing this, but it has something to do with using channels.
If you replace this code with
it will work as you want.