Send object via socket

混江龙づ霸主 提交于 2020-01-05 08:06:55

问题


I want to send x object over socket but when I run this code i got nothings. it is stop at new ObjectInputStream(socket.getInputStream()) and don't do any thing else.

Server class:

public class Server {
private static final int PORT = 9001;
ServerSocket listener;
private Handler h[] = new Handler[5];
private int clientCount = 0;
public Server() throws Exception{
    System.out.println("The server is running.");
    listener = new ServerSocket(PORT);
    run();
}
public void run(){
    while (true) {
        try {
            addClient(listener.accept());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  private void addClient(Socket socket) throws Exception{
    h[clientCount] = new Handler(this, socket,clientCount);
    h[clientCount].open(); 
    clientCount++;

}
public static void main(String[] args) throws Exception {
    Server s = new Server();

}
}

Handler class // Handle class:

public class Handler extends Thread {
private Server  server;
private Socket socket;
private int ID = -1;
private ObjectInputStream obIn = null;
private ObjectOutputStream obOut = null;
public Handler(Server _server, Socket _socket, int i){
    super();
      server = _server;
      socket = _socket;
      ID     = i;
}
 public void open() 
   {  
      try {
          obIn = new ObjectInputStream(socket.getInputStream());
      obOut = new ObjectOutputStream(socket.getOutputStream());
      x= ob.readObject();
    } catch (Exception e) {
          e.printStackTrace();
    }   
   }
}

The client:

public class Client  {
ObjectInputStream oin;
ObjectOutputStream oot;
private Socket socket = null;
public Client() {
    String serverAddress = "127.0.0.1";
    try {
     socket = new Socket(serverAddress, 9001);
        oin = new ObjectInputStream(socket.getInputStream());
        oot = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("hello i am a client");
        oot.writeObject(x);
    } catch (Exception e) {
    e.printStackTrace();
    }
}
public static void main(String[] args) throws Exception {
    Client client = new Client();
}
}

回答1:


You must declare the ObjectOutputStream before you declare the ObjectInputStream.

When you create an ObjectInputStream, it waits for data from an ObjectOutputStream. It's waiting on that data (the header).

public ObjectInputStream(InputStream in) throws IOException {
     verifySubclass();
     bin = new BlockDataInputStream(in);
     handles = new HandleTable(10);
     vlist = new ValidationList();
     enableOverride = false;
     readStreamHeader(); //this is whats causing it to block
     bin.setBlockDataMode(true);
}


protected void readStreamHeader() throws IOException, StreamCorruptedException {
     short s0 = bin.readShort();
     short s1 = bin.readShort();
     if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
          throw new StreamCorruptedException(String.format("invalid stream header: %04X%04X", s0, s1));
     }
}

If you declare ObjectOutputStream first, it sends the data, which isn't blocking



来源:https://stackoverflow.com/questions/26824825/send-object-via-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!