Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectInputStream(socket.getInputStream());不起作用_Java_Sockets_Io - Fatal编程技术网

Java ObjectInputStream(socket.getInputStream());不起作用

Java ObjectInputStream(socket.getInputStream());不起作用,java,sockets,io,Java,Sockets,Io,我正在编写一个与服务器通信的类,但是当它试图在inputstream的帮助下构造ObjectInputStream时,程序被冻结了。没有例外,程序仍在运行,但挂起在尝试构造ObjectInputstream的行中 以下是我的问题所在方法的代码: @Override public void connect(String ip, int port) throws UnknownHostException, IOException { Socket socket = new Socket(ip


我正在编写一个与服务器通信的类,但是当它试图在inputstream的帮助下构造ObjectInputStream时,程序被冻结了。没有例外,程序仍在运行,但挂起在尝试构造ObjectInputstream的行中

以下是我的问题所在方法的代码:

@Override
public void connect(String ip, int port) throws UnknownHostException, IOException {
    Socket socket = new Socket(ip, port);
    out = new ObjectOutputStream(socket.getOutputStream());
    InputStream is = socket.getInputStream();
    in = new ObjectInputStream(is);
}
这是整个班级的代码:

package Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class MessageStreamerImpl implements MessageStreamer {
    ObjectOutputStream out;
    ObjectInputStream in;

    public MessageStreamerImpl(String ip, int port) throws UnknownHostException, IOException{
        connect(ip, port);
    }

    public MessageStreamerImpl(){
    }

    @Override
    public void send(Object message) throws IOException {
        if(out == null) throw new IOException();
        out.writeObject(message);
        out.flush();
    }

    @Override
    public Object receive() throws IOException{
        try {
            return in.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException();
        }
    }

    @Override
    public void connect(String ip, int port) throws UnknownHostException, IOException {
        Socket socket = new Socket(ip, port);
        out = new ObjectOutputStream(socket.getOutputStream());
        InputStream is = socket.getInputStream();
        in = new ObjectInputStream(is);
    }

}
在浏览谷歌时,我发现了这个:。但是我仍然不知道如何解决这个问题,因为我的ObjectOutputStream构造函数在ObjectInputStream的构造函数之前

这是我的服务器代码,也许会有帮助;)

包服务器;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.net.SocketException;
导入java.util.ArrayList;
公共类服务器{
ArrayList clients=新的ArrayList();
公共服务器(int端口){
试一试{
ServerSocket mySocket=新的ServerSocket(端口);
waitForClients(mySocket);
}捕获(IOE异常){
System.out.println(“无法启动”);
e、 printStackTrace();
}
}
私有void waitForClients(ServerSocket mySocket){
while(true){
试一试{
System.out.println(“准备接收”);
Socket client=mySocket.accept();
客户。添加(客户);
System.out.println(client.getInetAddress().getHostAddress()+“连接到服务器”);
线程t=新线程(新ClientHandler(客户端));
t、 start();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
公共无效shareToAll(对象ObjectToChare){
用于(套接字客户端:客户端){
对象输出流oos;
试一试{
oos=newObjectOutputStream(client.getOutputStream());
oos.writeObject(objectToSchare);
oos.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
私有类ClientHandler实现Runnable{
插座客户端插座;
公共客户端句柄(套接字客户端套接字){
this.clientSocket=clientSocket;
}
@凌驾
公开募捐{
试一试{
ObjectInputStream ois=新的ObjectInputStream(clientSocket.getInputStream());
while(true){
试一试{
ois.readObject();
}捕获(ClassNotFoundException | IOException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}捕获(SocketException e){
System.out.println(clientSocket.getInetAddress().getHostAddress()+“已断开与服务器的连接”);
客户端。移除(clientSocket);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
谢谢你的帮助,我找到了毛病。这是在服务器类上,它必须如下所示:

package Server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

public class Server {
    ArrayList<ObjectOutputStream> clientstreams = new ArrayList<ObjectOutputStream>();

    public Server(int port){
        try {
            ServerSocket mySocket = new ServerSocket(port);
            waitForClients(mySocket);
        } catch (IOException e) {
            System.out.println("Unable to start.");
            e.printStackTrace();
        }
    }

    private void waitForClients(ServerSocket mySocket) {
        while(true){
            try {
                System.out.println("Ready to receive");
                Socket client = mySocket.accept();
                clientstreams.add(new ObjectOutputStream(client.getOutputStream()));
                System.out.println(client.getInetAddress().getHostAddress()+" connected to the Server");
                Thread t = new Thread(new ClientHandler(client));
                t.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void shareToAll(Object objectToSchare){
        for(ObjectOutputStream stream:clientstreams){
            try {
                stream.writeObject(objectToSchare);
                stream.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private class ClientHandler implements Runnable{
        Socket clientSocket;

        public ClientHandler(Socket clientSocket){
            this.clientSocket = clientSocket;
        }
        @Override
        public void run() {
            try {
                ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
                while(true){
                    try {
                        ois.readObject();

                    } catch (ClassNotFoundException | IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }catch(SocketException e){
                System.out.println(clientSocket.getInetAddress().getHostAddress()+" disconnected from the Server");
                clientstreams.remove(clientSocket);
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }   
}
包服务器;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.net.SocketException;
导入java.util.ArrayList;
公共类服务器{
ArrayList clientstreams=新的ArrayList();
公共服务器(int端口){
试一试{
ServerSocket mySocket=新的ServerSocket(端口);
waitForClients(mySocket);
}捕获(IOE异常){
System.out.println(“无法启动”);
e、 printStackTrace();
}
}
私有void waitForClients(ServerSocket mySocket){
while(true){
试一试{
System.out.println(“准备接收”);
Socket client=mySocket.accept();
添加(新的ObjectOutputStream(client.getOutputStream());
System.out.println(client.getInetAddress().getHostAddress()+“连接到服务器”);
线程t=新线程(新ClientHandler(客户端));
t、 start();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
公共无效shareToAll(对象ObjectToChare){
for(ObjectOutputStream:clientstreams){
试一试{
stream.writeObject(objectToSchare);
stream.flush();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
私有类ClientHandler实现Runnable{
插座客户端插座;
公共客户端句柄(套接字客户端套接字){
this.clientSocket=clientSocket;
}
@凌驾
公开募捐{
试一试{
ObjectInputStream ois=新的ObjectInputStream(clientSocket.getInputStream());
while(true){
试一试{
ois.readObject();
}捕获(ClassNotFoundException | IOException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}捕获(SocketException e){
System.out.println(clientSocket.getInetAddress().getHostAddress()+“已断开与服务器的连接”);
clientstreams.remove(clientSocket);
}捕获(IOE异常){
//TODO自动生成的捕捉块
package Server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

public class Server {
    ArrayList<ObjectOutputStream> clientstreams = new ArrayList<ObjectOutputStream>();

    public Server(int port){
        try {
            ServerSocket mySocket = new ServerSocket(port);
            waitForClients(mySocket);
        } catch (IOException e) {
            System.out.println("Unable to start.");
            e.printStackTrace();
        }
    }

    private void waitForClients(ServerSocket mySocket) {
        while(true){
            try {
                System.out.println("Ready to receive");
                Socket client = mySocket.accept();
                clientstreams.add(new ObjectOutputStream(client.getOutputStream()));
                System.out.println(client.getInetAddress().getHostAddress()+" connected to the Server");
                Thread t = new Thread(new ClientHandler(client));
                t.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void shareToAll(Object objectToSchare){
        for(ObjectOutputStream stream:clientstreams){
            try {
                stream.writeObject(objectToSchare);
                stream.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private class ClientHandler implements Runnable{
        Socket clientSocket;

        public ClientHandler(Socket clientSocket){
            this.clientSocket = clientSocket;
        }
        @Override
        public void run() {
            try {
                ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
                while(true){
                    try {
                        ois.readObject();

                    } catch (ClassNotFoundException | IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }catch(SocketException e){
                System.out.println(clientSocket.getInetAddress().getHostAddress()+" disconnected from the Server");
                clientstreams.remove(clientSocket);
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }   
}