Client-Server Chat Application
What is Socket :
 A socket is one endpoint of a two-way communication link
between two programs running on a network.
 It is one of the most fundamental technologies of
computer network programming. Sockets allow network
software applications to communicate using standard
mechanisms built into network hardware and operating
systems.
 A socket represents a single connection between exactly
two pieces of software (a so-called point-to-point
connection). More than two pieces of software can
communicate with client/server or distributed systems by
using multiple sockets.
.
 Socket-based software usually runs on two separate computers
on the network, but sockets can also be used to communicate
locally (inter-process) on a single computer like our project.
 Sockets are bidirectional, meaning that either side of the
connection is capable of both sending and receiving data.
Sometimes the one application that initiates communication is
termed the "client" and the other application the "server“.
Socket Interface Types:
 Socket interfaces can be divided into three
categories:
1. Stream Socket
2. Datagram Socket
3. Raw Socket
[We will talk about Raw Socket only]
Raw Socket:
 In computer networking, a raw socket is an internet
socket that allows direct sending and receiving of Internet
Protocol packets without any protocol-specific transport
layer formatting.
 It bypasses the library's built-in support for standard
protocols like TCP and UDP. Raw sockets are used for
custom low-level protocol development in Client-Server
communication.
 Raw sockets are used in security related applications
Function of the Client-Server Chat
Application:
Basic principles:
 In a connection-oriented client-to-server model, the
socket on the server process waits for requests from a
client.
 The client-to-server data exchange takes place when a
client connects to the server through a socket.
 A socket is bound to a port number so that the TCP layer
can identify the application that data is destined to be
sent to. An endpoint is a combination of an IP address and
a port number.
.
 The server first establishes (binds) an address
that clients can use to find the server.
 When the address is established, the server waits
for clients to request a service to the server.
 The server performs the client's request and sends
the reply back to the client.
8
The java.net package supports two common
network protocols:
TCP: (Transmission
Control Protocol)
 reliable delivery
 in-order guaranteed
 connection-oriented
 bidirectional
UDP: (User Datagram
Protocol)
 unreliable delivery
 no order guarantees
 no notion of “connection” –
app indicates dest. for each
packet
 can send or receive
App
socket
3 2 1
Dest.
App
socket
3 2 1
D1
D3
D2
Methods used in ServerSocket Class:
The java.net.ServerSocket class is used by server application to obtain a
port and listen for client requests.The ServerSocket class has four
constructors:
 public ServerSocket (int port) throws IOException Attempts
to create a server socket bound to the specified port. An exception
occurs if the port is already bound by another application.
 public ServerSocket(int backlog) throws IOException
Similar to the previous constructor, the backlog parameter specifies
how many Incoming clients to store in a wait queue.
.
 public ServerSocket(int address) throws IOException
Similar to the previous constructor, the int address parameter specifies the
local IP address to bind to. The int address is used for servers that may have
multiple IP addresses, allowing the server to specify which of its IP addresses to
accept client requests on.
 public ServerSocket() throws IOException
Creates an unbound server socket. When using this constructor, use the
bind() method when you are ready to bind the server socket. If the
ServerSocket constructor does not throw an exception, it means that your
application has successfully bound to the specified port and is ready for
client requests.
CLIENT SERVER DIAGRAM:
CODE ILLUSTRATION FOR CLIENT:
 public class ChatClient
 {
 // Declarations

 ChatClient()
 {
 //codes
 }

 private void ConnectToServer()
 {
 // code
 }
 private void SendMessageToServer(String Message)
 {
 }
 private void InitializeAppletComponents()
 {
 // Applet Initailization
 }
 private void LoginToChat()
 {
 ConnectToServer();
 }
 public static void main(String[] args) {
 ChatClient mainFrame = new ChatClient();
 }
CODE ILLUSTRATION FOR SERVER:
 public class ChatServer
 {
 //Declarations
 public ChatServer()
 {
 //Codes
 };
 }
 private void SendMessageToClient (Socket clientsocket, String message)
 {
 //Codes
 }
 public static void main(String[] args) {
 ChatServer mainFrame = new ChatServer();
 mainFrame.setVisible(true);
 }
 }
 }
Video description of the Client-Server Chat Application:
Thank You

Client server chat application

  • 1.
  • 2.
    What is Socket:  A socket is one endpoint of a two-way communication link between two programs running on a network.  It is one of the most fundamental technologies of computer network programming. Sockets allow network software applications to communicate using standard mechanisms built into network hardware and operating systems.  A socket represents a single connection between exactly two pieces of software (a so-called point-to-point connection). More than two pieces of software can communicate with client/server or distributed systems by using multiple sockets.
  • 3.
    .  Socket-based softwareusually runs on two separate computers on the network, but sockets can also be used to communicate locally (inter-process) on a single computer like our project.  Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Sometimes the one application that initiates communication is termed the "client" and the other application the "server“.
  • 4.
    Socket Interface Types: Socket interfaces can be divided into three categories: 1. Stream Socket 2. Datagram Socket 3. Raw Socket [We will talk about Raw Socket only]
  • 5.
    Raw Socket:  Incomputer networking, a raw socket is an internet socket that allows direct sending and receiving of Internet Protocol packets without any protocol-specific transport layer formatting.  It bypasses the library's built-in support for standard protocols like TCP and UDP. Raw sockets are used for custom low-level protocol development in Client-Server communication.  Raw sockets are used in security related applications
  • 6.
    Function of theClient-Server Chat Application: Basic principles:  In a connection-oriented client-to-server model, the socket on the server process waits for requests from a client.  The client-to-server data exchange takes place when a client connects to the server through a socket.  A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
  • 7.
    .  The serverfirst establishes (binds) an address that clients can use to find the server.  When the address is established, the server waits for clients to request a service to the server.  The server performs the client's request and sends the reply back to the client.
  • 8.
    8 The java.net packagesupports two common network protocols: TCP: (Transmission Control Protocol)  reliable delivery  in-order guaranteed  connection-oriented  bidirectional UDP: (User Datagram Protocol)  unreliable delivery  no order guarantees  no notion of “connection” – app indicates dest. for each packet  can send or receive App socket 3 2 1 Dest. App socket 3 2 1 D1 D3 D2
  • 9.
    Methods used inServerSocket Class: The java.net.ServerSocket class is used by server application to obtain a port and listen for client requests.The ServerSocket class has four constructors:  public ServerSocket (int port) throws IOException Attempts to create a server socket bound to the specified port. An exception occurs if the port is already bound by another application.  public ServerSocket(int backlog) throws IOException Similar to the previous constructor, the backlog parameter specifies how many Incoming clients to store in a wait queue.
  • 10.
    .  public ServerSocket(intaddress) throws IOException Similar to the previous constructor, the int address parameter specifies the local IP address to bind to. The int address is used for servers that may have multiple IP addresses, allowing the server to specify which of its IP addresses to accept client requests on.  public ServerSocket() throws IOException Creates an unbound server socket. When using this constructor, use the bind() method when you are ready to bind the server socket. If the ServerSocket constructor does not throw an exception, it means that your application has successfully bound to the specified port and is ready for client requests.
  • 11.
  • 12.
    CODE ILLUSTRATION FORCLIENT:  public class ChatClient  {  // Declarations   ChatClient()  {  //codes  }   private void ConnectToServer()  {  // code  }  private void SendMessageToServer(String Message)  {  }  private void InitializeAppletComponents()  {  // Applet Initailization  }  private void LoginToChat()  {  ConnectToServer();  }  public static void main(String[] args) {  ChatClient mainFrame = new ChatClient();  }
  • 13.
    CODE ILLUSTRATION FORSERVER:  public class ChatServer  {  //Declarations  public ChatServer()  {  //Codes  };  }  private void SendMessageToClient (Socket clientsocket, String message)  {  //Codes  }  public static void main(String[] args) {  ChatServer mainFrame = new ChatServer();  mainFrame.setVisible(true);  }  }  }
  • 14.
    Video description ofthe Client-Server Chat Application:
  • 15.