SlideShare a Scribd company logo
SOCKET PROGRAMMING IN JAVA



             Narendran Thangarajan,
             @naren_live,
             II Year, BE, CSE,
             SSN College of Engg, Chennai.
What started it all..
 Internet has emerged as a global ubiquitous
  media for communication

 It has changed the way we live, learn, enjoy,
  communicate, interact, engage, etc.

 To take advantage of this, businesses are
  ceaselessly trying to offer their services through
  the internet.

 So a huge demand for software developers good
  in creating internet-enabled applications.
Client Server Applications

        The Web Service is provided by the server
        and the clients use these services


   Client
                                             Server
                        Network


Client machine
                                       Server machine
        A client, where sockets and network
            This is
                    A server, come in !!
Sockets
 Introduced in BSD 4.1 UNIX 1981.


 Sockets are the endpoints of any
  communication over the internet.

 Sockets are identified by socket addresses.


 Socket Address = IP Address + Port Number
Why IP Address + Port number?
 • IP Address is of the form 10.0.0.1

 • Port Number can be anything from 0 to 65,535.
Destination Socket = 10.0.0.2 : 80

IP Address – Choose network

                      20.0.0.0



     10.0.0.0                     30.0.0.0




                       40.0.0.0
Destination Socket = 10.0.0.2 : 80


IP Address -> MAC Address – Locate the
                          specific system



            10.0.0.1

        10.0.0.2
Port Number – Process specific
Port 10000



                           Port 11000




                         Port 120000
Understanding Ports

         OutLook             AVG
Gmail               YM
         Express            Update




Port 1    Port 2   Port 3   Port 4

          Transport Layer
                                         Packet

                                         Port num    data


                                     Network layer
 Thus virtually, sockets are a connection
  between the two processes in different
  systems.

 Eg : Let the socket pairs be
   10.0.0.1 : 80 and 20.0.0.1 : 2000
   192.168.21.10 : 3000 and 192.168.100.1 : 6000
Networking Basics – the larger picture

     Applications Layer
       Standard apps
         HTTP             TCP STACK
         FTP
         Telnet               Application
       User apps          (http,ftp,telnet,…)
     Transport Layer           Transport
       TCP                   (TCP, UDP,..)
       UDP
                                Network
     Network Layer              (IP,..)
       IP
     Link Layer                  Link
                            (device driver,..)
       Device drivers
Network Basics - Where are these sockets?

      Applications Layer
          Standard apps
            HTTP              TCP STACK
            FTP
            Telnet                Application
          User apps           (http,ftp,telnet,…)


     Programming Interface:      Sockets
          SOCKETS

                                    Transport
      Transport Layer            (TCP, UDP,..)
          TCP
          UDP                      Network
                                     (IP,..)
        Network Layer                Link
            IP
        Link Layer
                                (device driver,..)
            Device drivers
Now into Socket
 programming..
Socket Programming with TCP
 Server starts first..
   Server Process must be running first
   Server must have created a socket which welcomes
     client’s connection. (Welcoming socket)
 Client contacts server by..
   Creating Client local TCP socket
   Specify IP Address and port number of server process.
   When Client socket is created, the connection is
    established.
   When connection is established, server creates a new
    socket (Connection Socket) to communicate with that
    client and the Welcoming socket is once again waiting
    for connections for other clients.
Client/Server Socket Interaction in
TCP
       Server
     create socket,
     port=x, for
     incoming request:
     welcomeSocket =
         ServerSocket()                          Client
     wait for incoming
                          TCP                create socket,
     connection request   connection setup   connect to hostid, port=x
     connectionSocket =                      clientSocket =
     welcomeSocket.accept()                         Socket()

                                               send request using
     read request from                         clientSocket
     connectionSocket

     write reply to
     connectionSocket                          read reply from
                                               connectionSocket
     close
     connectionSocket                           close
                                                clientSocket
Step 1 :
                        Connection request
            port
   Server
                                                    Client




Step 2 :
             port




   Server




                                             port
                                                    Client
    port            Connection
Types of Sockets in TCP

 ServerSocket – the socket used by servers
 Socket – Socket used by clients


 Create a ServerSocket in the server and make
  it to wait for connections from Sockets from
  other clients
The concept of Streams
       Client output    Server input
       stream           stream



     Client              Server
     socket              socket




    Client input       Server output
    stream             stream
Socket Programming with UDP

 No Connection between client and server.
   No handshaking
   The sender has to explicitly mention the IP
    address and the port of the destination.
   The server should extract the IP Address of the
    datagram everytime.


 Uses DatagramSocket.
Client/server socket interaction:
UDP
   Server                   Client
  create socket,       create socket,
  port=x, for          clientSocket =
  incoming request:    DatagramSocket()
  serverSocket =
  DatagramSocket()
                       Create, address (hostid, port=x),
                       send datagram request
                       using clientSocket
   read request from
   serverSocket

   write reply to
   serverSocket
                        read reply from
   specifying client
                        clientSocket
   host address,
   port number           close
                         clientSocket
Coding time..
Conclusion

 Socket Programming is very easy in Java.


 Usually each and every socket is handled by a
  separate thread in real-time client/server
  environments.
Queries

More Related Content

What's hot (20)

PDF
Introduction aux-sockets
Aissam エサム Afifi アフフ
 
PPTX
Formation1 sockets
Mariem SOMRANI
 
PPT
Ppt of socket
Amandeep Kaur
 
PPT
The Evolution of Java
Fu Cheng
 
PDF
Cours 4 intergiciels orientes messages jms
Mariem ZAOUALI
 
DOCX
Java interface
HoneyChintal
 
PPTX
for loop in java
Majid Ali
 
PDF
Support programmation orientée objet c# .net version f8
ENSET, Université Hassan II Casablanca
 
PPTX
Multiuser chat application using java
asifnoman240
 
PDF
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
PPTX
Servlets et JSP
Heithem Abbes
 
PPTX
Java Introduction
sunmitraeducation
 
PPT
Threads And Synchronization in C#
Rizwan Ali
 
PPT
Java Servlets
BG Java EE Course
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPTX
Socket Programming
VisualBee.com
 
PPTX
Http Protocol
N R Z Malik
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PDF
Basic Java Programming
Math-Circle
 
PDF
Java Multithreading Using Executors Framework
Arun Mehra
 
Introduction aux-sockets
Aissam エサム Afifi アフフ
 
Formation1 sockets
Mariem SOMRANI
 
Ppt of socket
Amandeep Kaur
 
The Evolution of Java
Fu Cheng
 
Cours 4 intergiciels orientes messages jms
Mariem ZAOUALI
 
Java interface
HoneyChintal
 
for loop in java
Majid Ali
 
Support programmation orientée objet c# .net version f8
ENSET, Université Hassan II Casablanca
 
Multiuser chat application using java
asifnoman240
 
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Servlets et JSP
Heithem Abbes
 
Java Introduction
sunmitraeducation
 
Threads And Synchronization in C#
Rizwan Ali
 
Java Servlets
BG Java EE Course
 
Java Server Pages(jsp)
Manisha Keim
 
Socket Programming
VisualBee.com
 
Http Protocol
N R Z Malik
 
Basic Java Programming
Math-Circle
 
Java Multithreading Using Executors Framework
Arun Mehra
 

Similar to Socket programming using java (20)

PPT
Ports & sockets
myrajendra
 
PDF
Socket Programming
elliando dias
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PPT
Np unit1
vamsitricks
 
PPTX
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
PPT
Sockets
sivindia
 
DOCX
Final networks lab manual
Jaya Prasanna
 
PPT
Socket programming-tutorial-sk
sureshkarthick37
 
PDF
How a network connection is created A network connection is initi.pdf
arccreation001
 
PDF
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
PDF
Gu3112991305
IJERA Editor
 
PPTX
Client server chat application
Samsil Arefin
 
DOCX
Mail Server Project Report
Kavita Sharma
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
PPT
App layer
khushali_modi
 
PDF
Socket programming assignment
Ravi Gupta
 
PPTX
Socket programming
Ujjwal Kumar
 
PDF
Sockets
Toru Watanabe
 
PDF
Sockets
Rajesh Kumar
 
Ports & sockets
myrajendra
 
Socket Programming
elliando dias
 
Socket Programming - nitish nagar
Nitish Nagar
 
Np unit1
vamsitricks
 
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
Sockets
sivindia
 
Final networks lab manual
Jaya Prasanna
 
Socket programming-tutorial-sk
sureshkarthick37
 
How a network connection is created A network connection is initi.pdf
arccreation001
 
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
Gu3112991305
IJERA Editor
 
Client server chat application
Samsil Arefin
 
Mail Server Project Report
Kavita Sharma
 
Networking Java Socket Programming
Mousmi Pawar
 
App layer
khushali_modi
 
Socket programming assignment
Ravi Gupta
 
Socket programming
Ujjwal Kumar
 
Sockets
Toru Watanabe
 
Sockets
Rajesh Kumar
 
Ad

More from UC San Diego (19)

PDF
A primer on network devices
UC San Diego
 
PDF
Datacenter traffic demand characterization
UC San Diego
 
PDF
Smart Homes, Buildings and Internet-of-things
UC San Diego
 
PDF
Social Networks analysis to characterize HIV at-risk populations - Progress a...
UC San Diego
 
PDF
eyeTalk - A system for helping people affected by motor neuron problems
UC San Diego
 
PDF
Pirc net poster
UC San Diego
 
PDF
Ajaxism
UC San Diego
 
PDF
Basic terminologies for a developer
UC San Diego
 
PDF
Fields in computer science
UC San Diego
 
PDF
Understanding computer networks
UC San Diego
 
PDF
FOSS Introduction
UC San Diego
 
PDF
Network Programming with Umit project
UC San Diego
 
PDF
Introduction to Python
UC San Diego
 
PDF
Airline reservation system db design
UC San Diego
 
PDF
Workshop on Network Security
UC San Diego
 
PDF
Routing basics
UC San Diego
 
PDF
Technology Quiz
UC San Diego
 
PDF
Android application development
UC San Diego
 
PDF
Pervasive Web Application Architecture
UC San Diego
 
A primer on network devices
UC San Diego
 
Datacenter traffic demand characterization
UC San Diego
 
Smart Homes, Buildings and Internet-of-things
UC San Diego
 
Social Networks analysis to characterize HIV at-risk populations - Progress a...
UC San Diego
 
eyeTalk - A system for helping people affected by motor neuron problems
UC San Diego
 
Pirc net poster
UC San Diego
 
Ajaxism
UC San Diego
 
Basic terminologies for a developer
UC San Diego
 
Fields in computer science
UC San Diego
 
Understanding computer networks
UC San Diego
 
FOSS Introduction
UC San Diego
 
Network Programming with Umit project
UC San Diego
 
Introduction to Python
UC San Diego
 
Airline reservation system db design
UC San Diego
 
Workshop on Network Security
UC San Diego
 
Routing basics
UC San Diego
 
Technology Quiz
UC San Diego
 
Android application development
UC San Diego
 
Pervasive Web Application Architecture
UC San Diego
 
Ad

Recently uploaded (20)

PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 

Socket programming using java

  • 1. SOCKET PROGRAMMING IN JAVA Narendran Thangarajan, @naren_live, II Year, BE, CSE, SSN College of Engg, Chennai.
  • 2. What started it all..  Internet has emerged as a global ubiquitous media for communication  It has changed the way we live, learn, enjoy, communicate, interact, engage, etc.  To take advantage of this, businesses are ceaselessly trying to offer their services through the internet.  So a huge demand for software developers good in creating internet-enabled applications.
  • 3. Client Server Applications The Web Service is provided by the server and the clients use these services Client Server Network Client machine Server machine A client, where sockets and network This is A server, come in !!
  • 4. Sockets  Introduced in BSD 4.1 UNIX 1981.  Sockets are the endpoints of any communication over the internet.  Sockets are identified by socket addresses.  Socket Address = IP Address + Port Number
  • 5. Why IP Address + Port number? • IP Address is of the form 10.0.0.1 • Port Number can be anything from 0 to 65,535.
  • 6. Destination Socket = 10.0.0.2 : 80 IP Address – Choose network 20.0.0.0 10.0.0.0 30.0.0.0 40.0.0.0
  • 7. Destination Socket = 10.0.0.2 : 80 IP Address -> MAC Address – Locate the specific system 10.0.0.1 10.0.0.2
  • 8. Port Number – Process specific Port 10000 Port 11000 Port 120000
  • 9. Understanding Ports OutLook AVG Gmail YM Express Update Port 1 Port 2 Port 3 Port 4 Transport Layer Packet Port num data Network layer
  • 10.  Thus virtually, sockets are a connection between the two processes in different systems.  Eg : Let the socket pairs be  10.0.0.1 : 80 and 20.0.0.1 : 2000  192.168.21.10 : 3000 and 192.168.100.1 : 6000
  • 11. Networking Basics – the larger picture  Applications Layer  Standard apps  HTTP TCP STACK  FTP  Telnet Application  User apps (http,ftp,telnet,…)  Transport Layer Transport  TCP (TCP, UDP,..)  UDP Network  Network Layer (IP,..)  IP  Link Layer Link (device driver,..)  Device drivers
  • 12. Network Basics - Where are these sockets?  Applications Layer  Standard apps  HTTP TCP STACK  FTP  Telnet Application  User apps (http,ftp,telnet,…)  Programming Interface: Sockets  SOCKETS Transport  Transport Layer (TCP, UDP,..)  TCP  UDP Network (IP,..)  Network Layer Link  IP  Link Layer (device driver,..)  Device drivers
  • 13. Now into Socket programming..
  • 14. Socket Programming with TCP  Server starts first..  Server Process must be running first  Server must have created a socket which welcomes client’s connection. (Welcoming socket)  Client contacts server by..  Creating Client local TCP socket  Specify IP Address and port number of server process.  When Client socket is created, the connection is established.  When connection is established, server creates a new socket (Connection Socket) to communicate with that client and the Welcoming socket is once again waiting for connections for other clients.
  • 15. Client/Server Socket Interaction in TCP Server create socket, port=x, for incoming request: welcomeSocket = ServerSocket() Client wait for incoming TCP create socket, connection request connection setup connect to hostid, port=x connectionSocket = clientSocket = welcomeSocket.accept() Socket() send request using read request from clientSocket connectionSocket write reply to connectionSocket read reply from connectionSocket close connectionSocket close clientSocket
  • 16. Step 1 : Connection request port Server Client Step 2 : port Server port Client port Connection
  • 17. Types of Sockets in TCP  ServerSocket – the socket used by servers  Socket – Socket used by clients  Create a ServerSocket in the server and make it to wait for connections from Sockets from other clients
  • 18. The concept of Streams Client output Server input stream stream Client Server socket socket Client input Server output stream stream
  • 19. Socket Programming with UDP  No Connection between client and server.  No handshaking  The sender has to explicitly mention the IP address and the port of the destination.  The server should extract the IP Address of the datagram everytime.  Uses DatagramSocket.
  • 20. Client/server socket interaction: UDP Server Client create socket, create socket, port=x, for clientSocket = incoming request: DatagramSocket() serverSocket = DatagramSocket() Create, address (hostid, port=x), send datagram request using clientSocket read request from serverSocket write reply to serverSocket read reply from specifying client clientSocket host address, port number close clientSocket
  • 22. Conclusion  Socket Programming is very easy in Java.  Usually each and every socket is handled by a separate thread in real-time client/server environments.