Non-blocking method for reading writing objects to sockets

Discussion in 'Java' started by Sameer, May 6, 2005.

  1. Sameer

    logicstorm

    Joined:
    May 7, 2010
    Messages:
    1
    Likes Received:
    0
    Non-Blocking Object Serialization via a socket

    Client side:
    Socket server = Socket(serverAddress, serverPort).connect();
    OutputStream outStream = server.getOutputStream();
    OutputObjectStream objectOutStream = new OutputObjectStream(outStream);

    //write to server
    outStream.write(1);
    outObjectStream.write(MySerializableObject);


    ServerSide:

    Socket client = serverSocket.accept();
    InputStream inStream = client.getInputStream();
    InputObjectStream inObjectStream = new InputObjectStream(inStream);

    //main driving while loop of the server
    while (running_forever) {
    //do all kinds of work
    if (inStream.available() >0) { //NON-BLOCKING
    //read in the number the client sent (pick it off the stream)
    inStream.read();
    //read in the object the client sent
    Object = inObjectStream.readObject();
    }

    //do other kinds of work

    }
     
    logicstorm, May 7, 2010
    #21
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.