ObjectInput/OutputStream + BufferedInput/OutputStream über Socket

superomega

Grünschnabel
Hallo,

mein Server und Client, die ObjectInput/OutputStreams über einen Socket benutzen, funktionieren so lange gut, bis ich versuche die Daten per BufferedInput/OutputStream zu puffern.

Früher scheint das ja mal funktioniert zu haben, jedenfalls gibt es Beispiele im Netz dazu.
Benutze ich den Buffer, so hängt sich das Programm beim Initialisieren des ObjectInputStream auf.

Java: JDK 1.6.0_20-b02

Code:
public class Server implements Runnable
{

  public void ServerSocketTest()
  {

    try
    {

      ServerSocket serverSocket = new ServerSocket(1212);
      Socket socket = serverSocket.accept();

      ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
      ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
      
      //ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
      //ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));

      Request request = (Request) objectInputStream.readObject();
      while (request != null)
      {

        Response response = new Response();
        objectOutputStream.writeObject(response);
        objectOutputStream.flush();
        objectOutputStream.reset();
        
        request = (Request) objectInputStream.readObject();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public void run()
  {
    ServerSocketTest();
  }
}

Code:
public class Client implements Runnable
{

  public void SocketTest()
  {

    int iterations = 100;
    try
    {
      Socket socket = new Socket("localhost", 1212);

      ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());

      //ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
      //ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));

      double start = System.currentTimeMillis();
      for (int i = 0; i < iterations; ++i)
      {

        Request request = new Request();
        objectOutputStream.writeObject(request);
        objectOutputStream.flush();
        objectOutputStream.reset();

        Response response = (Response) objectInputStream.readObject();
      }
      double finish = System.currentTimeMillis();
      System.out.println("Per ping: " + (finish - start) / iterations);

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public void run()
  {
    SocketTest();
  }
}

Code:
public class Test
{
  public static void main(String[] argv)
  {
    Server s = new Server();
    Client c = new Client();

    Thread ts = new Thread(s);
    Thread tc = new Thread(c);

    ts.start();
    tc.start();
  }
}

Viele Grüße,
Werner
 
Die Antwort ist einfach, wenn man es weiß:

beim Erzeugen von ObjectOutputStream jeweils einmal .flush() aufrufen.

Grüße,
Werner
 
Zurück