[英]Is it possible to create more than 1 consumers on InputStream and OutputStream?
I am implementing a Java project using client-server architecture.我正在使用客户端 - 服务器架构实现一个 Java 项目。 My client goes into an infinite loop when connected via socket.
通过套接字连接时,我的客户端进入无限循环。
EDIT1: I have changed my client and server programs to minimal, complete and verifiable codes. EDIT1:我已将客户端和服务器程序更改为最少、完整且可验证的代码。 The issue arises due to having multiple consumers on the same input and output streams (as pointed by the answer below).
由于在相同的输入和输出流上有多个使用者(如下面的答案所指出的),因此出现了这个问题。
Here is my server code:这是我的服务器代码:
import java.net.*;
import java.io.*;
class Demo {
int a;
String b;
Demo() {
a = 10;
b = "Sample";
}
}
class Streamsample {
private ServerSocket serverSocket = null;
DataInputStream dis = null;
DataOutputStream dos = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
Streamsample() {
try{
serverSocket = new ServerSocket(3000);
Socket s = serverSocket.accept();
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
oos = new ObjectOutputStream(s.getOutputStream());
System.out.println(dis.readUTF());
Demo d = (Demo)ois.readObject();
System.out.print(d.a + " " + d.b);
dis.close();
dos.close();
ois.close();
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Streamsample ss = new Streamsample();
}
}
Here is my client code:这是我的客户端代码:
import java.net.*;
import java.io.*;
class Demo {
int a;
String b;
Demo() {
a = 10;
b = "Sample";
}
}
class Streamclient {
private Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
Streamclient() {
try{
s = new Socket("localhost",3000);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
oos = new ObjectOutputStream(s.getOutputStream());
dos.writeUTF("Hello");
Demo d = new Demo();
oos.writeObject(d);
dis.close();
dos.close();
ois.close();
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Streamclient ss = new Streamclient();
}
}
The system I am implementing requires my client to send and receive objects
as well as String
s, int
s etc. I am attempting to use DataInputStream
and DataOutputStream
for String
s, int
s and ObjectInputStream
, ObjectOutputStream
for object
s.我正在实施的系统要求我的客户端发送和接收
objects
以及String
s、 int
s 等。我试图将DataInputStream
和DataOutputStream
用于String
s、 int
s 和ObjectInputStream
、 ObjectOutputStream
用于object
s。 My program goes into an infinite loop.我的程序进入无限循环。 So should I use
ObjectStream's for passing
String s,
int s as well and completely omit
DataStream`s or is there a workaround available which will allow both Streams to be used on the same socket?那么我应该使用
ObjectStream's for passing
String s,
int s as well and completely omit
DataStream`s 还是有可用的解决方法来允许在同一个套接字上使用两个流?
You are consuming the same streams twice - it cannot work by design.您两次使用相同的流 - 它不能按设计工作。 You should only have one consumer for each of your streams, eg:
每个流应该只有一个消费者,例如:
TicketBooking.oos = new ObjectOutputStream(s.getOutputStream());
TicketBooking.ois = new ObjectInputStream(s.getInputStream());
Why do you need two consumers for each of your input and output streams?为什么每个输入和输出流都需要两个消费者?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.