Unit 7- Socket Programming in Java(9hr)

5th Semester

Goal : Socket programming to build any networked application g. www(IE, Firefox), FTP (WinSCP)
The four applications are

  • A trivial date server and client, one-way communication.
  • A capitalize server and client,two-way communication.
  • A two-player tic tac toe game, illustrating a server that needs to keep track of the state of a game, and inform each client of it, so they can each update their own displays.
  • A multi-user chat application, in which a server must broadcast messages to all of its client.

e.g. Telephone Dial Analogue

  • Socket() : end point for communication
  • Bind() : Assign a unique telephone number
  • Listen() : Wait for caller
  • Connect() : Dial a number
  • Accept() : Receive a call
  • Send()/ Receive(), Read()/ Write(): Talk or Message exchange
  • Close() : Hang

Server Socket

The ServerSocket class provides the niche to write everything about servers in Java. The main job of a server socket is to wait for incoming calls and respond accordingly.
The work flow of the server program can be defined as follows:

  1. Create server socket on a particular
  2. Listen to any attempt of connection to that
  3. If a connection attempt succeeds, get the host of stream objects from the Socket and communicate with the
  4. The communication, however, is established according to the agreed
  5. Close the
  6. Wait further for any more communication attempts (return to Step 2).

Summarize saying that since TCP requires a connection, it needs the following APIs on server and client
server : socket(), bind(), listen(), accept(), recv(), send()
client : socket(), connect(), send(), recv()

UDP/ Datagram communication:

The datagram communication protocol, known as UDP (user datagram protocol), is a connectionless protocol, meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket’s address.
PIC

As shown in the Figure, the steps of establishing a UDP socket communication on the client side are as follows:

  • Create a socket using the socket() function;
  • Send and receive data by means of the recvfrom() and sendto() functions.

The steps of establishing a UDP socket communication on the server side are as follows:

  • Create a socket with the socket() function;
  • Bind the socket to an address using the bind() function;
  • Send and receive data by means of recvfrom() and sendto().

TCP/ Stream communication:

The stream communication protocol is known as TCP (transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions.
PIC

As shown in the figure, the steps for establishing a TCP socket on the client side are the following:

  • Create a socket using the socket() function;
  • Connect the socket to the address of the server using the connect() function;
  • Send and receive data by means of the read() and write() functions.
  • Close the connection by means of the close() function.

The steps involved in establishing a TCP socket on the server side are as follows:

  • Create a socket with the socket() function;
  • Bind the socket to an address using the bind() function;
  • Listen for connections with the listen() function;
  • Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server.
  • Send and receive data by means of send() and receive().
  • Close the connection by means of the close() function.