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)

Java rmi
Java rmiJava rmi
Java rmi
kamal kotecha
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
Amandeep Kaur
 
Socket programming
Socket programmingSocket programming
Socket programming
chandramouligunnemeda
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
FAKHRUN NISHA
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
VisualBee.com
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
Mohammed Abdalla Youssif
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
Les socket ing1_issat
Les socket ing1_issatLes socket ing1_issat
Les socket ing1_issat
sloumaallagui
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Socket programming or network programming
Socket programming or network programmingSocket programming or network programming
Socket programming or network programming
Mmanan91
 
Spring boot
Spring bootSpring boot
Spring boot
sdeeg
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
Haldia Institute of Technology
 

Similar to Socket programming using java (20)

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
elliando dias
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Client server project
Client server projectClient server project
Client server project
yogendra kumar patel
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Sockets
SocketsSockets
Sockets
sivindia
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Samsil Arefin
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
RoshniSundrani
 
Chapter_2_part5.ppt in the department of computer science
Chapter_2_part5.ppt in the department of computer scienceChapter_2_part5.ppt in the department of computer science
Chapter_2_part5.ppt in the department of computer science
zahrabashir8320
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
arnold 7490
 
Socket Programming_theory.ppt
Socket Programming_theory.pptSocket Programming_theory.ppt
Socket Programming_theory.ppt
mdrobinhossain4
 
Java 1
Java 1Java 1
Java 1
VidyaVarshini3
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
Socket & Server Socket
Socket & Server SocketSocket & Server Socket
Socket & Server Socket
Hemant Chetwani
 
17-Networking.pdf
17-Networking.pdf17-Networking.pdf
17-Networking.pdf
sophia763824
 
JavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdfJavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdf
jose19881
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
TienTran779192
 
Understanding computer networks
Understanding computer networksUnderstanding computer networks
Understanding computer networks
UC San Diego
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla
 
Networking in Python2025 (programs allll)
Networking in Python2025 (programs allll)Networking in Python2025 (programs allll)
Networking in Python2025 (programs allll)
PriyankaPatil919748
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Samsil Arefin
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
RoshniSundrani
 
Chapter_2_part5.ppt in the department of computer science
Chapter_2_part5.ppt in the department of computer scienceChapter_2_part5.ppt in the department of computer science
Chapter_2_part5.ppt in the department of computer science
zahrabashir8320
 
Socket Programming_theory.ppt
Socket Programming_theory.pptSocket Programming_theory.ppt
Socket Programming_theory.ppt
mdrobinhossain4
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
JavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdfJavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdf
jose19881
 
Understanding computer networks
Understanding computer networksUnderstanding computer networks
Understanding computer networks
UC San Diego
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla
 
Networking in Python2025 (programs allll)
Networking in Python2025 (programs allll)Networking in Python2025 (programs allll)
Networking in Python2025 (programs allll)
PriyankaPatil919748
 
Ad

More from UC San Diego (18)

A primer on network devices
A primer on network devicesA primer on network devices
A primer on network devices
UC San Diego
 
Datacenter traffic demand characterization
Datacenter traffic demand characterizationDatacenter traffic demand characterization
Datacenter traffic demand characterization
UC San Diego
 
Smart Homes, Buildings and Internet-of-things
Smart Homes, Buildings and Internet-of-thingsSmart Homes, Buildings and Internet-of-things
Smart Homes, Buildings and Internet-of-things
UC San Diego
 
Social Networks analysis to characterize HIV at-risk populations - Progress a...
Social Networks analysis to characterize HIV at-risk populations - Progress a...Social Networks analysis to characterize HIV at-risk populations - Progress a...
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
eyeTalk - A system for helping people affected by motor neuron problemseyeTalk - A system for helping people affected by motor neuron problems
eyeTalk - A system for helping people affected by motor neuron problems
UC San Diego
 
Pirc net poster
Pirc net posterPirc net poster
Pirc net poster
UC San Diego
 
Ajaxism
AjaxismAjaxism
Ajaxism
UC San Diego
 
Basic terminologies for a developer
Basic terminologies for a developerBasic terminologies for a developer
Basic terminologies for a developer
UC San Diego
 
Fields in computer science
Fields in computer scienceFields in computer science
Fields in computer science
UC San Diego
 
FOSS Introduction
FOSS IntroductionFOSS Introduction
FOSS Introduction
UC San Diego
 
Network Programming with Umit project
Network Programming with Umit projectNetwork Programming with Umit project
Network Programming with Umit project
UC San Diego
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
Airline reservation system db design
Airline reservation system db designAirline reservation system db design
Airline reservation system db design
UC San Diego
 
Workshop on Network Security
Workshop on Network SecurityWorkshop on Network Security
Workshop on Network Security
UC San Diego
 
Routing basics
Routing basicsRouting basics
Routing basics
UC San Diego
 
Technology Quiz
Technology QuizTechnology Quiz
Technology Quiz
UC San Diego
 
Android application development
Android application developmentAndroid application development
Android application development
UC San Diego
 
Pervasive Web Application Architecture
Pervasive Web Application ArchitecturePervasive Web Application Architecture
Pervasive Web Application Architecture
UC San Diego
 
A primer on network devices
A primer on network devicesA primer on network devices
A primer on network devices
UC San Diego
 
Datacenter traffic demand characterization
Datacenter traffic demand characterizationDatacenter traffic demand characterization
Datacenter traffic demand characterization
UC San Diego
 
Smart Homes, Buildings and Internet-of-things
Smart Homes, Buildings and Internet-of-thingsSmart Homes, Buildings and Internet-of-things
Smart Homes, Buildings and Internet-of-things
UC San Diego
 
Social Networks analysis to characterize HIV at-risk populations - Progress a...
Social Networks analysis to characterize HIV at-risk populations - Progress a...Social Networks analysis to characterize HIV at-risk populations - Progress a...
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
eyeTalk - A system for helping people affected by motor neuron problemseyeTalk - A system for helping people affected by motor neuron problems
eyeTalk - A system for helping people affected by motor neuron problems
UC San Diego
 
Basic terminologies for a developer
Basic terminologies for a developerBasic terminologies for a developer
Basic terminologies for a developer
UC San Diego
 
Fields in computer science
Fields in computer scienceFields in computer science
Fields in computer science
UC San Diego
 
Network Programming with Umit project
Network Programming with Umit projectNetwork Programming with Umit project
Network Programming with Umit project
UC San Diego
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
Airline reservation system db design
Airline reservation system db designAirline reservation system db design
Airline reservation system db design
UC San Diego
 
Workshop on Network Security
Workshop on Network SecurityWorkshop on Network Security
Workshop on Network Security
UC San Diego
 
Android application development
Android application developmentAndroid application development
Android application development
UC San Diego
 
Pervasive Web Application Architecture
Pervasive Web Application ArchitecturePervasive Web Application Architecture
Pervasive Web Application Architecture
UC San Diego
 
Ad

Recently uploaded (20)

Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POSHow to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdfIntroduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptxChalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptxPEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POSHow to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdfIntroduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 

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.