문제

I'm having issues sending objects to a server. Right now, I have a server setup and listening for clients. The client connects, sends a test object (just a String) and outputs it to the command line. It works for the first string sent but none after that.

Server (Hivemind.java):

    // Open server socket for listening
    ServerSocket ss = null;
    boolean listening = true;
    try {
        ss = new ServerSocket(PORT_NUMBER);
    } catch (IOException e) {
        System.err.println("Cannot start listening on port " + PORT_NUMBER);
        e.printStackTrace();
    }

    // While listening is true, listen for new clients
    while (listening) {
        Socket socket = ss.accept();
        ServerDispatcher dispatcher = new ServerDispatcher(socket);
        dispatcher.start();
    }

    // Close the socket after we are done listening
    ss.close();

Server Thread (ServerDispatcher):

public ServerDispatcher(Socket socket) {
    super("ServerDispatcher");
    this.socket = socket;
}

public void run() {
    System.out.println("Client connected");
    try {
        input = socket.getInputStream();
        objInput = new ObjectInputStream(input);
        Object obj = null;

        try {
            obj = (String)objInput.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerDispatcher.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(obj);
    } catch (IOException ex) {
        Logger.getLogger(ServerDispatcher.class.getName()).log(Level.SEVERE, null, ex);
    }

Connection Class (HivemindConnect.java):

public HivemindConnect(int port) {
    this.port = port;
    url = "localhost";
}

public HivemindConnect(int port, String url) {
    this.port = port;
    this.url = url;
}

public void connect() {
    try {
        socket = new Socket(url, port);
        output = socket.getOutputStream();
        objOutput = new ObjectOutputStream(output);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void close() {
    try {
        objOutput.close();
        output.close();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void send(Object obj) {
    try {
        objOutput.writeObject(obj);
        objOutput.flush();
    } catch (IOException ex) {
        Logger.getLogger(HivemindConnect.class.getName()).log(Level.SEVERE, null, ex);
    }
}

CustomerTopComponent:

// When the TC is opened connect to the server
@Override
public void componentOpened() {
    hivemind = new HivemindConnect(9001);
    hivemind.connect();
}

private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
    hivemind.send(txtText.getText());
}

// When the TC is closed close the connection to the server
@Override
public void componentClosed() {
    hivemind.close();
}
도움이 되었습니까?

해결책

You need a loop like this:

  while(objInput.available()>0){
    Object obj = null;
    obj = (String)objInput.readObject();
    System.out.println(obj);}

Or something similar.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top