java - 是否可以在InputStream和OutputStream上创建1个以上的使用者?

标签 java sockets inputstream outputstream

我正在使用客户端-服务器体系结构实现Java项目。通过套接字连接时,我的客户端进入无限循环。

编辑1:
我已将客户端和服务器程序更改为最少,完整和可验证的代码。由于在相同的输入和输出流上有多个使用者(如下面的答案所指出),因此出现了问题。

这是我的服务器代码:

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();
    }
}

这是我的客户代码:
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();
    }
}

我正在实现的系统要求我的客户端发送和接收objects以及Stringint等等。我试图将DataInputStreamDataOutputStream用于StringintObjectInputStreamObjectOutputStream用于object s。我的程序陷入无限循环。因此,我应该使用ObjectStream's for passing字符串s, int s as well and completely omit DataStream还是有可用的解决方法来允许两个Streams在同一套接字上使用?

最佳答案

您消耗了两次相同的流-它无法按设计工作。每个流只应有一个使用者,例如:

TicketBooking.oos = new ObjectOutputStream(s.getOutputStream());
TicketBooking.ois = new ObjectInputStream(s.getInputStream());

为什么每个输入和输出流都需要两个使用者?

关于java - 是否可以在InputStream和OutputStream上创建1个以上的使用者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278565/

相关文章:

java - 如何使用TextViews创建JSON? Android Studio

java - 验证时,我需要 "exception"中的元素名称

java - 将数据添加到列表 - JDBC

networking - 将单个端口用于多个套接字的标准方法?

sockets - TCP套接字问题

java - BufferedInputStream.read(byte[] b, int off, int len) 可以返回 0 吗?是否有可能导致这种情况的重要的、损坏的 InputStreams?

java - 与父接口(interface)具有相同方法的子接口(interface)

sockets - UNIX套接字: Can a client ever read data meant for another client?

java - CipherInputStream 中的 skip 方法

java - 如何返回接口(interface)对象?