SlideShare a Scribd company logo
TCP Sockets
A socket is a connection between two hosts. It can
perform seven basic operations:
• Connect to a remote machine
• Send data
• Receive data
• Close a connection
• Bind to a port
• Listen for incoming data
• Accept connections from remote machines on the
bound port
Client Sockets
• The program creates a new socket with a Socket( )
constructor.
• The socket attempts to connect to the remote host.
• Once the connection is established, the local and remote
hosts get input and output streams from the socket and use
those streams to send data to each other.
• This connection is full-duplex; both hosts can send and
receive data simultaneously.
• What the data means depends on the protocol; different
commands are sent to an FTP server than to an HTTP server.
There will normally be some agreed-upon hand-shaking
followed by the transmission of data from one to the other.
• When the transmission of data is complete, one or both sides
close the connection. Some protocols, such as HTTP 1.0,
require the connection to be closed after each request is
serviced. Others, such as FTP, allow multiple requests to be
processed in a single connection.
Tcp sockets
Tcp sockets
Tcp sockets
Constructors
Constructor creates a TCP socket to the
specified port on the specified host and
attempts to connect to the remote host
Continued..
public Socket(String host, int port) throws
UnknownHostException, IOException
• public Socket(String host, int port, InetAddress
interface, int localPort) throws IOException
• public Socket(InetAddress host, int port,
InetAddress interface, int localPort) throws
IOException
if 0 is passed for the local port number, java
randomly selects port number between 1024 and
65,535.
Getting Information About a
Socket
• public InetAddress getInetAddress( )
• public int getPort( )
• public int getLocalPort( )
• public InetAddress getLocalAddress( )
public String toString( ) (Object Method)
Sockets for Servers
• Java provides a ServerSocket class to allow
programmers to write servers.
• A ServerSocket runs on the server and listens
for incoming TCP connections.
• Each ServerSocket listens on a particular port
on the server machine.
• When a client Socket on a remote host attempts
to connect to that port, the server wakes up,
negotiates the connection between the client
and the server, and opens a regular Socket
between the two hosts.
• The operating system stores incoming
connection requests addressed to a
particular port in a first-in, first-out queue.
• The default length of the queue is normally
50, though this can vary from operating
system to operating system.
The basic life cycle of a server
1. A new ServerSocket is created on a particular port using a
ServerSocket( ) constructor.
2. The ServerSocket listens for incoming connection attempts on
that port using its accept( ) method.
3. accept( ) blocks until a client attempts to make a connection, at
which point accept( ) returns a Socket object connecting the client
and the server.
4. Socket's getInputStream( )method, getOutputStream( ) method,
are called to get input and output streams that communicate with
the client.
5. The server and the client interact according to an agreed-up on
protocol until it is time to close the connection.
6. The server, the client, or both close the connection.
7. The server returns to step 2 and waits for the next connection
Constructors
• public ServerSocket(int port) throws IOException,
BindException
• public ServerSocket(int port, int queueLength)throws
IOException, BindException
• public ServerSocket(int port, int
queueLength,InetAddress bindAddress) throws
IOException
• An IOException when creating a ServerSocket almost
always means one of two things.
– Either another server socket is already using the requested port,
– or you're trying to connect to a port from 1 to 1023 on Unix
without root (superuser) privileges.
get Methods
• public InetAddress getInetAddress( )
• public int getLocalPort( )

More Related Content

What's hot (20)

PDF
Network programming Using Python
Karim Sonbol
 
PPT
Socket programming in C
Deepak Swain
 
PDF
Socket programming using C
Ajit Nayak
 
PPT
Java Socket Programming
Vipin Yadav
 
PPT
Ppt of socket
Amandeep Kaur
 
PPTX
Socket programming in c
Md. Golam Hossain
 
PPTX
Socket programming
Anurag Tomar
 
PPT
Application Layer and Socket Programming
elliando dias
 
PDF
Sockets
Indrasena Reddy
 
PPT
Socket Programming Tutorial
Jignesh Patel
 
PPT
Socket programming-tutorial-sk
sureshkarthick37
 
PDF
Advanced Sockets Programming
elliando dias
 
PPTX
Network programming using python
Ali Nezhad
 
PDF
Programming TCP/IP with Sockets
elliando dias
 
PDF
Socket programming
Rajivarnan (Rajiv)
 
PPTX
Socket Programming
VisualBee.com
 
PPT
Basic socket programming
Kristian Arjianto
 
PDF
Socket programming using java
UC San Diego
 
DOC
socket programming
prashantzagade
 
PPT
Socket programming
chandramouligunnemeda
 
Network programming Using Python
Karim Sonbol
 
Socket programming in C
Deepak Swain
 
Socket programming using C
Ajit Nayak
 
Java Socket Programming
Vipin Yadav
 
Ppt of socket
Amandeep Kaur
 
Socket programming in c
Md. Golam Hossain
 
Socket programming
Anurag Tomar
 
Application Layer and Socket Programming
elliando dias
 
Socket Programming Tutorial
Jignesh Patel
 
Socket programming-tutorial-sk
sureshkarthick37
 
Advanced Sockets Programming
elliando dias
 
Network programming using python
Ali Nezhad
 
Programming TCP/IP with Sockets
elliando dias
 
Socket programming
Rajivarnan (Rajiv)
 
Socket Programming
VisualBee.com
 
Basic socket programming
Kristian Arjianto
 
Socket programming using java
UC San Diego
 
socket programming
prashantzagade
 
Socket programming
chandramouligunnemeda
 

Viewers also liked (19)

PPTX
TCP echo 서버 및 클라이언트 예제 스터디
quxn6
 
PDF
Ip tunnelling and_vpn
Rajesh Porwal
 
PDF
1ip Tunneling And Vpn Technologies 101220042129 Phpapp01
Hussein Elmenshawy
 
PPT
Vulnerabilities in IP Protocols
babak danyal
 
PPTX
Basics of sockets
AviNash ChaVhan
 
PDF
Gre tunnel pdf
Rajesh Porwal
 
PPTX
Controlling remote pc using mobile
Archana Maharjan
 
PPT
Remote Login
hariprasadnr
 
PDF
UNDOCUMENTED Vyatta vRouter: Unbreakable VPN Tunneling (MEMO)
Naoto MATSUMOTO
 
PPTX
What Is VPN
SwitchVPN
 
PPTX
ENCAPSULATION AND TUNNELING
Mohammad Adil
 
PPTX
Remote access service
Apoorw Pandey
 
PPTX
IP Security
Keshab Nath
 
PPT
IP Security in Network Security NS6
koolkampus
 
PPTX
VPN, Its Types,VPN Protocols,Configuration and Benefits
qaisar17
 
PPT
Ports & sockets
myrajendra
 
PPT
Input output organization
abdulugc
 
PDF
UDP - User Datagram Protocol
Peter R. Egli
 
TCP echo 서버 및 클라이언트 예제 스터디
quxn6
 
Ip tunnelling and_vpn
Rajesh Porwal
 
1ip Tunneling And Vpn Technologies 101220042129 Phpapp01
Hussein Elmenshawy
 
Vulnerabilities in IP Protocols
babak danyal
 
Basics of sockets
AviNash ChaVhan
 
Gre tunnel pdf
Rajesh Porwal
 
Controlling remote pc using mobile
Archana Maharjan
 
Remote Login
hariprasadnr
 
UNDOCUMENTED Vyatta vRouter: Unbreakable VPN Tunneling (MEMO)
Naoto MATSUMOTO
 
What Is VPN
SwitchVPN
 
ENCAPSULATION AND TUNNELING
Mohammad Adil
 
Remote access service
Apoorw Pandey
 
IP Security
Keshab Nath
 
IP Security in Network Security NS6
koolkampus
 
VPN, Its Types,VPN Protocols,Configuration and Benefits
qaisar17
 
Ports & sockets
myrajendra
 
Input output organization
abdulugc
 
UDP - User Datagram Protocol
Peter R. Egli
 
Ad

Similar to Tcp sockets (20)

PDF
Java networking programs - theory
Mukesh Tekwani
 
PPT
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
Abodahab
 
PDF
How a network connection is created A network connection is initi.pdf
arccreation001
 
PPT
Md13 networking
Rakesh Madugula
 
DOC
Socket
Amandeep Kaur
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PDF
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
PPTX
Java socket programming
Mohammed Abdalla Youssif
 
DOCX
Mail Server Project Report
Kavita Sharma
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Sockets
sivindia
 
PPT
Sockets in unix
swtjerin4u
 
DOCX
Lab manual cn-2012-13
Sasi Kala
 
PPTX
Client server chat application
Samsil Arefin
 
PPTX
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
PPT
Socket Programming it-slideshares.blogspot.com
phanleson
 
PPTX
A.java
JahnaviBhagat
 
PPTX
Socket programming in Java (PPTX)
UC San Diego
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
PPT
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Java networking programs - theory
Mukesh Tekwani
 
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
Abodahab
 
How a network connection is created A network connection is initi.pdf
arccreation001
 
Md13 networking
Rakesh Madugula
 
Socket Programming - nitish nagar
Nitish Nagar
 
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
Java socket programming
Mohammed Abdalla Youssif
 
Mail Server Project Report
Kavita Sharma
 
Network programming in Java
Tushar B Kute
 
Sockets
sivindia
 
Sockets in unix
swtjerin4u
 
Lab manual cn-2012-13
Sasi Kala
 
Client server chat application
Samsil Arefin
 
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
Socket Programming it-slideshares.blogspot.com
phanleson
 
Socket programming in Java (PPTX)
UC San Diego
 
Networking Java Socket Programming
Mousmi Pawar
 
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Ad

More from babak danyal (20)

DOCX
applist
babak danyal
 
PPT
Java IO Package and Streams
babak danyal
 
PPT
Swing and Graphical User Interface in Java
babak danyal
 
PPTX
block ciphers and the des
babak danyal
 
PPT
key distribution in network security
babak danyal
 
PPT
Lecture10 Signal and Systems
babak danyal
 
PPT
Lecture8 Signal and Systems
babak danyal
 
PPT
Lecture7 Signal and Systems
babak danyal
 
PPT
Lecture6 Signal and Systems
babak danyal
 
PPT
Lecture5 Signal and Systems
babak danyal
 
PPT
Lecture4 Signal and Systems
babak danyal
 
PPT
Lecture3 Signal and Systems
babak danyal
 
PPT
Lecture2 Signal and Systems
babak danyal
 
PPT
Lecture1 Intro To Signa
babak danyal
 
PPT
Lecture9 Signal and Systems
babak danyal
 
PPT
Lecture9
babak danyal
 
PPT
Cns 13f-lec03- Classical Encryption Techniques
babak danyal
 
PPT
Classical Encryption Techniques in Network Security
babak danyal
 
DOCX
Problems at independence
babak danyal
 
DOCX
Quaid-e-Azam and Early Problems of Pakistan
babak danyal
 
applist
babak danyal
 
Java IO Package and Streams
babak danyal
 
Swing and Graphical User Interface in Java
babak danyal
 
block ciphers and the des
babak danyal
 
key distribution in network security
babak danyal
 
Lecture10 Signal and Systems
babak danyal
 
Lecture8 Signal and Systems
babak danyal
 
Lecture7 Signal and Systems
babak danyal
 
Lecture6 Signal and Systems
babak danyal
 
Lecture5 Signal and Systems
babak danyal
 
Lecture4 Signal and Systems
babak danyal
 
Lecture3 Signal and Systems
babak danyal
 
Lecture2 Signal and Systems
babak danyal
 
Lecture1 Intro To Signa
babak danyal
 
Lecture9 Signal and Systems
babak danyal
 
Lecture9
babak danyal
 
Cns 13f-lec03- Classical Encryption Techniques
babak danyal
 
Classical Encryption Techniques in Network Security
babak danyal
 
Problems at independence
babak danyal
 
Quaid-e-Azam and Early Problems of Pakistan
babak danyal
 

Recently uploaded (20)

PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 

Tcp sockets

  • 1. TCP Sockets A socket is a connection between two hosts. It can perform seven basic operations: • Connect to a remote machine • Send data • Receive data • Close a connection • Bind to a port • Listen for incoming data • Accept connections from remote machines on the bound port
  • 2. Client Sockets • The program creates a new socket with a Socket( ) constructor. • The socket attempts to connect to the remote host. • Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other. • This connection is full-duplex; both hosts can send and receive data simultaneously. • What the data means depends on the protocol; different commands are sent to an FTP server than to an HTTP server. There will normally be some agreed-upon hand-shaking followed by the transmission of data from one to the other. • When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP 1.0, require the connection to be closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.
  • 6. Constructors Constructor creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host
  • 7. Continued.. public Socket(String host, int port) throws UnknownHostException, IOException • public Socket(String host, int port, InetAddress interface, int localPort) throws IOException • public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException if 0 is passed for the local port number, java randomly selects port number between 1024 and 65,535.
  • 8. Getting Information About a Socket • public InetAddress getInetAddress( ) • public int getPort( ) • public int getLocalPort( ) • public InetAddress getLocalAddress( ) public String toString( ) (Object Method)
  • 9. Sockets for Servers • Java provides a ServerSocket class to allow programmers to write servers. • A ServerSocket runs on the server and listens for incoming TCP connections. • Each ServerSocket listens on a particular port on the server machine. • When a client Socket on a remote host attempts to connect to that port, the server wakes up, negotiates the connection between the client and the server, and opens a regular Socket between the two hosts.
  • 10. • The operating system stores incoming connection requests addressed to a particular port in a first-in, first-out queue. • The default length of the queue is normally 50, though this can vary from operating system to operating system.
  • 11. The basic life cycle of a server 1. A new ServerSocket is created on a particular port using a ServerSocket( ) constructor. 2. The ServerSocket listens for incoming connection attempts on that port using its accept( ) method. 3. accept( ) blocks until a client attempts to make a connection, at which point accept( ) returns a Socket object connecting the client and the server. 4. Socket's getInputStream( )method, getOutputStream( ) method, are called to get input and output streams that communicate with the client. 5. The server and the client interact according to an agreed-up on protocol until it is time to close the connection. 6. The server, the client, or both close the connection. 7. The server returns to step 2 and waits for the next connection
  • 12. Constructors • public ServerSocket(int port) throws IOException, BindException • public ServerSocket(int port, int queueLength)throws IOException, BindException • public ServerSocket(int port, int queueLength,InetAddress bindAddress) throws IOException • An IOException when creating a ServerSocket almost always means one of two things. – Either another server socket is already using the requested port, – or you're trying to connect to a port from 1 to 1023 on Unix without root (superuser) privileges.
  • 13. get Methods • public InetAddress getInetAddress( ) • public int getLocalPort( )