View RSS Feed

Java Socket

Writing to a Socket

Rate this Entry
by , 04-25-2012 at 10:28 PM (474 Views)
For writing to Java Socket, it is necessary to get the OutputStream. This is done as following:

Java Code:
Socket socket = new Socket("jenkov.com", 80);
OutputStream out = socket.getOutputStream();

out.write("some data".getBytes());
out.flush();
out.close();

socket.close();
This is so simple!

Flush() shall be called when one desires the data which has been sent across web to server. TCP/IP implementation present in OS buffers data & also sends it in big chunks so that it may fit to the TCP/IP packet size.

Submit "Writing to a Socket" to Facebook Submit "Writing to a Socket" to Digg Submit "Writing to a Socket" to del.icio.us Submit "Writing to a Socket" to StumbleUpon Submit "Writing to a Socket" to Google

Categories
Tutorial

Comments