Socket programming in Java: Socket & IO

Posted on

Java Stream, File and IO

ServerSocket v.s. Socket

ServerSocket:

Set up the connection with address and port number. Listen to the response from client. Once the setup is finished and ServerSocket accept(), the control power is transferred to Socket.

Socket:

The actual role for communication between client and server.

Server-side:

  1. Setup ServerSocket with port number.
  2. accept()
  3. accept() returns a Socket

Client-side:

  1. Initiate a client Socket
  2. Use Socket.getOutputStream/getInputStream

Basic structure of client:

  1. Open a socket.
  2. Open an input stream and output stream to the socket.
  3. Read from and write to the stream according to the server’s protocol.
  4. Close the streams.
  5. Close the socket.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s