java对象流

发布于 2024-10-05 06:32:03 字数 1539 浏览 10 评论 0原文

我正在尝试用 java 创建一个服务器,它将同时维护最多 4 个连接。我认为将相关信息保存在数组中可以满足我的目的,但是我遇到了一些麻烦。

这是我创建的类:


import java.net.*;
import java.io.*;
public class tcpConnects{
private ObjectInputStream input;
private ObjectOutputStream output;
private int player;
public tcpConnects(int playerNumber, Socket connect) {
    // TODO Auto-generated method stub
        try{
            System.out.println("create InputStream");
            input= new ObjectInputStream(connect.getInputStream());
            System.out.println("create OutputStream");
            output= new ObjectOutputStream(connect.getOutputStream());
            System.out.println("streams created");
            //sendData("Welcome!");
            player=playerNumber;
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }
    public ObjectInputStream getInput(){
        return input;
    }
    public void setInput(ObjectInputStream in){
        input=in;
    }
    public ObjectOutputStream getOutput(){
        return output;
    }
    public void setOutput(ObjectOutputStream out){
        output=out;
    }
    public int getPlayer(){
        return player;
    }
    public void sendData(String data){
        try{
            output.writeObject(data);
            output.flush();
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }
}

无论如何,当我将套接字发送到类中时,它会进入创建输入流的阶段,但实际上从未创建该流。语法似乎是正确的,所以我只能假设存在某种我不知道的逻辑错误。任何有助于解释为什么此类不会创建输入或输出流的帮助将不胜感激。

谢谢,

I'm trying to create a server in java which will maintain up to 4 connections simultaneously. I thought that holding the relevant information in an array would serve my purpose, well, but I'm having some trouble.

Here is the class I've created:


import java.net.*;
import java.io.*;
public class tcpConnects{
private ObjectInputStream input;
private ObjectOutputStream output;
private int player;
public tcpConnects(int playerNumber, Socket connect) {
    // TODO Auto-generated method stub
        try{
            System.out.println("create InputStream");
            input= new ObjectInputStream(connect.getInputStream());
            System.out.println("create OutputStream");
            output= new ObjectOutputStream(connect.getOutputStream());
            System.out.println("streams created");
            //sendData("Welcome!");
            player=playerNumber;
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }
    public ObjectInputStream getInput(){
        return input;
    }
    public void setInput(ObjectInputStream in){
        input=in;
    }
    public ObjectOutputStream getOutput(){
        return output;
    }
    public void setOutput(ObjectOutputStream out){
        output=out;
    }
    public int getPlayer(){
        return player;
    }
    public void sendData(String data){
        try{
            output.writeObject(data);
            output.flush();
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }
}

anyway, when I send a socket into the class, it gets to the stage of creating the input stream, but then the stream is never actually created. The syntax appears to be correct, so I can only assume that there is some form of logic error which I am not aware of. Any help in deciphering why this class will not create an input or output stream would be greatly appreciated.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-10-12 06:32:03

ObjectInputStream之前在两端创建ObjectOutputStreamObjectOutputStream 的构造函数将标头写入到 ObjectInputStream 的构造函数读取的流中,因此,如果您先创建 ObjectInputStreams,则会出现死锁。

Create the ObjectOutputStream before the ObjectInputStream, at both ends. The constructor of ObjectOutputStream writes a header to the stream that the constructor of ObjectInputStream reads, so if you create the ObjectInputStreams first you get a deadlock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文