8.1 C
Sunday, June 6, 2021
4.55 out of 5
390 reviews on Udemy

Java: Socket Programming Simplified

Build the foundations for server side programming, a MUST learn for server side application developers/aspirants.
Sagar Uppuluri
11,061 students enrolled
Get deep insights about single threaded and multi-threaded client/server communication using TCP/IP
Understand UDP, HTTP and understand working of HTTPServer using a Mock Server.
Build simple peer-to-peer chat and group chat apps.

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.


Understand message transfer between 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. 


Basic Client Server Example

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.

Single Threaded Client Server

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.

Downside of single threaded client server

When a client is serviced sequentially, if a client is blocked then the server is also blocked.

Multi-threaded Client Server

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.

User Datagram Protocol

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. 

Problem Set


HTTP interaction explained

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. 

Sample Multi-threaded HttpServer

A simple multi-threaded HttpServer with 5 threads for servicing the client request.

Examples Apps

Section Introduction
Client/Server Example with Database Interaction
Simple Chat using TCP
Group Chat using UDP

Source Code

Source Code for Download

What's next?

Bonus Lecture: OFFER Coupons for my other courses
You can view and review the lecture materials indefinitely, like an on-demand channel.
Definitely! If you have an internet connection, courses on Udemy are available on any device at any time. If you don't have an internet connection, some instructors also let their students download course lectures. That's up to the instructor though, so make sure you get on their good side!
4.6 out of 5
390 Ratings

Detailed Rating

Stars 5
Stars 4
Stars 3
Stars 2
Stars 1
30-Day Money-Back Guarantee


2 hours on-demand video
3 articles
Full lifetime access
Access on mobile and TV
Certificate of Completion