Socket programming is an essential part of the language that enables you to understand how applications communicate over the network. This helps in enabling you to understand how client server communication works and also gives you more insights into how server applications are built.
It is important for each and every application developer (including Java developers) to have an understanding of how socket communication works. Hence this course is an absolute necessity.
The primary focus points of this course include
How TCP/IP communication works
How UDP/IP communication works
Basic client/server coding using multi-threaded client/server.
Get working knowledge of HTTP communication using Mock HTTPServer.
Build a Peer-to-Peer chat app using TCP/IP
Build a Group Chat app using UDP/IP.
Once you are familiar with the above topics you will be able to confidently deal with the current client/server applications.
Port number uniquely identifies an application running under one protocol. When a message is sent from an application, several headers gets added and they help in passing relevant information to be processed by the same layer on the other end.
java.net package provides the API for TCP and UDP communication. ServerSocket is a class which opens a socket at the server end which enables server to listen to the client request. The accept() method of the ServerSocket class waits for client and once connected returns a Socket object which is used for communication. Communication can happen through the input and output streams of the Socket class.
When a server is single threaded it normally accepts one client at a time. The downside of it is that server is also blocked when the client is blocked.
When a client is serviced sequentially, if a client is blocked then the server is also blocked.
Assign each client socket to a separate thread so that the client is serviced in parallel and also server is not blocked even if one of the client is blocked.
Having said that instead of creating separate thread for each client we can create a pool of threads and assign the available thread to the client.
TCP (Transmission control protocol) is a connection oriented protocol and hence exchange of information is possible only after establishing the connection. Where as UDP (User datagram protocol) is a connection less protocol and hence there is no need to establish a connection before exchanging information. Here you will send/receive DatagramPacket through DatagramSocket. Once sent that it the end of interaction. We can further enhance this protocol as per our need to acknowledge the packet or make it a to and fro interaction using the address available through the packet.
HTTP (Hyper Text Transfer Protocol) defines the structure of request and response on top of TCP/IP connection that enables information exchange between a browser and a server. Later it is also adopted to define REST API. It is a stateless protocol i.e. once server responds to the client it forgets about the client. HTTP request and response includes header, break line and body part. Header includes necessary support information about the application or response (in case of response) and the body part includes the data associated with either request or response.
A simple multi-threaded HttpServer with 5 threads for servicing the client request.