O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning an Object

Socket clientSock = serverSocket.accept();
											ObjectOutputStream os = new ObjectOutputStream(
											clientSock.getOutputStream( ));
											// return an object
											os.writeObject(new Date());
											os.close();

In this phrase, we return a serialized object to a client. We get a Socket instance returned from the accept() method of the ServerSocket after making a connection with a client. We then create an ObjectOutputStream instance, passing the output stream obtained from the client socket. An ObjectOutputStream is used to write primitive datatypes and graphs of Java objects to an OutputStream. In this example, we write a Date object to the output stream and then close the stream.

The writeObject() method causes the object passed as a parameter to be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required