JAVA:从类中获取对象的实例

JAVA:从类中获取对象的实例,java,object,return,Java,Object,Return,我在返回类内定义的对象实例时遇到问题,在下面的代码中,我试图在扩展线程的类内建立套接字连接。socket\u connect类: public class socket_connect extends Thread { private Socket socket; private OutputStream out; private String host; private int port; socket_connect(String host,int p

我在返回类内定义的对象实例时遇到问题,在下面的代码中,我试图在扩展
线程的类内建立
套接字
连接。
socket\u connect
类:

public class socket_connect extends Thread {
    private Socket socket;
    private OutputStream out;
    private String host;
    private int port;
    socket_connect(String host,int port){
        this.host=host;
        this.port=port;
        this.start();
    }
    @Override
    public void run() {
        try {
            this.socket = new Socket(this.host,this.port);
            this.out=new BufferedOutputStream(this.socket.getOutputStream());
            this.out.write("i am here \n\r".getBytes());
            this.out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getPort() {
        return port;
    }
    public String getHost() {
        return host;
    }
    public Socket getSocket() {
              return socket;
    }
    public  OutputStream getOut() {
        return out;
    }
}
主要类别:

public class mymain {
public static void main(String[] args) {
    Socket backsock;
    String backhost;
    int backport;
    String msg ="Second Hi!!!!!";
    socket_connect sc = new socket_connect("127.0.0.1",8080);
    backsock = sc.getSocket();
    backhost = sc.getHost();
    backport = sc.getPort();
    System.out.println(backhost + " " + backport);
    try {
        OutputStream buffer= new BufferedOutputStream(backsock.getOutputStream());
        buffer.write(msg.getBytes());
        buffer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    while (true) { }
}
}

连接已建立,
Outputstream
Socket\u connect
类中,将向服务器写入“我在这里!!!”。但是当我试图用main中定义的New
Outputstream
执行另一个写过程时,我在行中得到一个错误
OutputStream buffer=new BufferedOutputStream(backsock.getOutputStream())
我在主文件中定义的backsocket似乎是空的。
即使我使用了
backsock=sc.getSocket()使其与我在实例sc中定义的套接字相同。
但是
gethost()
getPort()
工作得非常好:

backhost = sc.getHost();
backport = sc.getPort();
System.out.println(backhost + " " + backport);
这将打印
127.0.0.1 8080
我可以将它们取回并存储在main中定义的int和string中,并使用
System.out
打印它们,但我无法从类中取回
Socket
或outputstream。它们返回
null

我以前使用C++,有一个指针指向程序中的精确对象,但是我不能。

尝试运行这个程序几次,你可能发现它工作了,在某些情况下。这似乎是一个与线程相关的问题。执行时,main方法可能会在socket\u connect线程开始执行
run()
方法之前继续执行。主机
和端口
的get方法之所以有效,是因为 值已在
run()
方法之外分配。您可以通过使用
Thread.sleep()
在main方法中等待片刻来简单地检查它。 例如:


注意:在(true)
时也无限期地等待
,这不是一个好做法。您所能做的就是尝试从sockets输入流中读取一些内容。

尝试多次运行此程序,在某些情况下,您可能会发现它工作正常。这似乎是一个与线程相关的问题。执行时,main方法可能会在socket\u connect线程开始执行
run()
方法之前继续执行。主机
和端口的get方法之所以有效,是因为 值已在
run()
方法之外分配。您可以通过使用
Thread.sleep()
在main方法中等待片刻来简单地检查它。 例如:


注意:在(true)时也无限期地等待
,这不是一个好做法。您所能做的就是尝试从套接字输入流中读取一些内容。

问题是您试图在创建套接字之前获取它。您应该像这样将线程同步技术集成到代码中

public class socket_connect extends Thread {
    private Socket socket;
    private OutputStream out;
    private String host;
    private int port;
    private Semaphore sema = new Semaphore(1);

    socket_connect(String host, int port) {
        try {
            this.host = host;
            this.port = port;
            this.sema.acquire();
            this.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            this.socket = new Socket(this.host, this.port);
            this.out = new BufferedOutputStream(this.socket.getOutputStream());
            this.out.write("i am here \n\r".getBytes());
            this.out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sema.release();
        }
    }

    public int getPort() throws InterruptedException {
            return port;
    }

    public String getHost() {
        return host;
    }

    public Socket getSocket() throws InterruptedException {
        try {
            sema.acquire();
            return socket;
        } finally {
            sema.release();
        }
    }

    public OutputStream getOut() throws InterruptedException {
        try {
            sema.acquire();
            return out;
        }finally {
            sema.release();
        }
    }

public static void main(String[] args) throws InterruptedException {
            Socket backsock;
            String backhost;
            int backport;
            String msg = "Second Hi!!!!!";
            socket_connect sc = new socket_connect("google.com", 80);
            backsock = sc.getSocket();
            backhost = sc.getHost();
            backport = sc.getPort();
            System.out.println(backhost + " " + backport);
            try {
                OutputStream buffer = new BufferedOutputStream(backsock.getOutputStream());
                buffer.write(msg.getBytes());
                buffer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

问题是您试图在创建套接字之前获取它。您应该像这样将线程同步技术集成到代码中

public class socket_connect extends Thread {
    private Socket socket;
    private OutputStream out;
    private String host;
    private int port;
    private Semaphore sema = new Semaphore(1);

    socket_connect(String host, int port) {
        try {
            this.host = host;
            this.port = port;
            this.sema.acquire();
            this.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            this.socket = new Socket(this.host, this.port);
            this.out = new BufferedOutputStream(this.socket.getOutputStream());
            this.out.write("i am here \n\r".getBytes());
            this.out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sema.release();
        }
    }

    public int getPort() throws InterruptedException {
            return port;
    }

    public String getHost() {
        return host;
    }

    public Socket getSocket() throws InterruptedException {
        try {
            sema.acquire();
            return socket;
        } finally {
            sema.release();
        }
    }

    public OutputStream getOut() throws InterruptedException {
        try {
            sema.acquire();
            return out;
        }finally {
            sema.release();
        }
    }

public static void main(String[] args) throws InterruptedException {
            Socket backsock;
            String backhost;
            int backport;
            String msg = "Second Hi!!!!!";
            socket_connect sc = new socket_connect("google.com", 80);
            backsock = sc.getSocket();
            backhost = sc.getHost();
            backport = sc.getPort();
            System.out.println(backhost + " " + backport);
            try {
                OutputStream buffer = new BufferedOutputStream(backsock.getOutputStream());
                buffer.write(msg.getBytes());
                buffer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

无关:阅读java命名约定。在java中,类名总是大写的。你在名字中不使用“u”,除非是为了某个常数,而且只是为了记录:你在那里做的几乎是没有意义的。除了添加不工作的方法之外,在这里使用线程。。。真的没有做任何有意义的事。如果你想学习多线程:读一本好书或教程,并遵循它。不要试图发明你自己的用例,当你的知识还没有准备好的时候。@ghostcat我正在尝试在android中使用java,让我使用写在主线程中,因此,我必须制作3个线程,一个用于连接,两个用于写入,三个用于读取,但我需要所有三个线程的套接字都相同。在java中,类名总是大写的。你在名字中不使用“u”,除非是为了某个常数,而且只是为了记录:你在那里做的几乎是没有意义的。除了添加不工作的方法之外,在这里使用线程。。。真的没有做任何有意义的事。如果你想学习多线程:读一本好书或教程,并遵循它。不要试图发明你自己的用例,当你的知识还没有准备好的时候。@ghostcat我正在尝试在android中使用java,让我使用写入主线程,所以我必须制作3个线程,一个用于连接,两个用于写,三个用于读,但我需要所有三个线程的套接字相同是的,是的,谢谢,我需要使主线程睡眠,似乎我需要一个延迟来确定连接的时间,然后在主线程中获得输出是的,是的,谢谢,我需要使主线程睡眠,似乎我需要一个延迟来确定连接的时间,然后在主线程中获得输出。谢天谢地,我们推迟了在chizi ke shoma neveshtiym bayad的主要城市moshkelo hal kardam的测试。谢天谢地,你可以推迟在chizi ke shoma neveshtiym bayad测试科南的主要城市moshkelo hal kardam的测试