SlideShare a Scribd company logo
1 of 12
B.Kohila
M.Sc(info.tech),
Nadar Saraswathi College of arts and science,
Theni
 The Inetaddress class provides you with a limited interface to
DNS for doing both forward and reverse internet address
lookups
- An InetAddress class method corresponds to a DNS request
 Npublic constructoro
 Three static methods:
 InetAddress getByName(String)
 Static method used to retrieve the address for the host name passed
as the parameter.
 InetAddress [ ] getAllByName(String)
 Static method used to retrieve all the addresses for the host name
passed as a parameter.
 InetAddress getLocalHost( )
 Static method used to retrieve the address for the current, or local,
host.
 Three additional “getter” methods
 String getHostName( )
 Returns the host name.
 byte[ ] getAddress( )
 Returns the IP address.
 String getHostAddress( )
 Returns the IP address as a string.
try
{
InetAddress fullname =
netAddress.getByName(“bigyellowcat.cs.binghamton.edu");
InetAddress alias = InetAddress.getByName(“bigyellowcat");
InetAddress octets = InetAddress.getByName(“128.226.121.44");
if (fullname.equals(alias) && fullname.equals(octets))
// All is right with the world!}
catch (UnknownHostException e)
{ // Exception handling here. }
 Once a TCP socket connection is made, a virtual
stream is in place. Java’s IO model is that of a stream,
therefore the models are consistent; all you need to do
connect a TCP socket to a stream and read and write
the streams as normal
 Socket(String ip, int port)
 Creates a streaming socket and binds it to the host and port specified as
parameters.
 Socket(String ip, int port, boolean TCPorUDP)
 Creates a socket and binds it to the host and port specified as
parameters. The last parameter is used to indicate whether the socket
should be a stream or datagram socket.
 Socket(InetAddress ia, int port)
 Creates a streaming socket connected to the specified host and port.
 Socket(InetAddress ia, int port, boolean TCPorUDP)
 Creates a socket connected to the specified host and port. The last
parameter specifies whether the socket should be a stream or datagram
socket.
 InetAddress getInetAddress( )
 Returns an InetAddress object representing the host for this socket.
 Int getPort( )
 . Returns the port number on the remote host for this socket
 Int getLocalPort( )
 Returns the port number on the local host for this socket.
 InputStream getInputStream( )
 Returns an input stream for the socket.
 OutputStream getOutputStream( )
 Returns an output stream for the socket.
 Close( )
 Closes the socket.
 SetSocketImplFactory (SocketImplFactory)
 Sets the socket factory that will be used to create all sockets.
try
{
Socket socket = new Socket("somehost.somewhere.com", -1);
// Always a good idea to buffer the stream to mitigate blocking.
PrintStream out = new PrintStream( new BufferedOutputStream(socket.getOutputStream()));
out.println("Are you listening?");
DataInputStream in = new DataInputStream( new BufferedInputStream(socket.getInputStream()));
in.readLine();
// ...
// Don't forget to close the socket!
socket.close()
}
catch (Exception e)
// Exception handling logic.
 Since UDP is a connectionless protocol; there is no virtual stream
between the hosts so streams are not used for IO.
 UDP applications are not thought of in terms of clients and servers,
but rather in terms of senders and receivers.
 For conversational applications both ends (sender and receiver) will be
changing states from sender to receiver and back again
 Many UDP based applications are simple send a request then receive the
data (sender’s perspective), like a DNS request. The receiver’s perspective
is to ‘listen’ for a request, send the response, listen for more requests.
 UDP sockets send and receive Datagrams
 Constructors: two for receiving, four for sending
 DatagramPacket( byte[ ] buff , int len)
 Constructs a DatagramPacket for receiving packets of length len.
 DatagramPacket(byte[] buf, int off, int len)
 Constructs a DatagramPacket for receiving packets of length len,
specifying an offset of off bytes into the buffer.
 DatagramPacket((byte[] buf, int len, InetAddress addr, int port)
 Constructs a datagram packet for sending packets of length len to the
specified port number on the specified host.
 DatagramPacker(byte[] buf, int off, int len, InetAddress addr, int port)
 Constructs a datagram packet for sending packets of length len with
offset off to the specified port number on the specified host.
 DatagramPacket(byte[] buf, int off, int len, SocketAddress addr)
 Constructs a datagram packet for sending packets of length len with
offset off to the specified port number on the specified host.
 Constructors
 DatagramSocket()
 Constructs a datagram socket and binds it to any available port on the
local host.
 DatagramSocket(DatagramSocketImpl impl)
 Creates an unbound datagram socket with the specified
DatagramSocketImpl.
 DatagramSocket(int port)
 Constructs a datagram socket and binds it to the specified port on the
local host.
 DatagramSocket(int port, InetAddress iaddr)
 Creates a datagram socket, bound to the specified local address.
 DatagramSocket(SocketAddress bindaddr)
 Creates a datagram socket, bound to the specified local socket address.

More Related Content

What's hot

20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secrets20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secretsmistercteam
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and RubyPat Shaughnessy
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014Kevin Lo
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationOWASP Hacker Thursday
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structureSaad Gabr
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Nach os network
Nach os networkNach os network
Nach os networknaniix21_3
 
Nach os network
Nach os networkNach os network
Nach os networknaniix21_3
 
Nach os network
Nach os networkNach os network
Nach os networknaniix21_3
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Stackless Python 101
Stackless Python 101Stackless Python 101
Stackless Python 101guest162fd90
 

What's hot (19)

20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secrets20150207 howes-gpgpu8-dark secrets
20150207 howes-gpgpu8-dark secrets
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and Ruby
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitation
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Sorter
SorterSorter
Sorter
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
Nach os network
Nach os networkNach os network
Nach os network
 
Nach os network
Nach os networkNach os network
Nach os network
 
Nach os network
Nach os networkNach os network
Nach os network
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Stackless Python 101
Stackless Python 101Stackless Python 101
Stackless Python 101
 
Lesson11 transactions
Lesson11 transactionsLesson11 transactions
Lesson11 transactions
 

Similar to Java networking basics & sockets overview

Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programmingashok hirpara
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxDhrumilSheth3
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13Sasi Kala
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddressSayak Sarkar
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programmingAnung Ariwibowo
 
TCP IP
TCP IPTCP IP
TCP IPhivasu
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programmingphanleson
 
Networking
NetworkingNetworking
NetworkingTuan Ngo
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programgovindjha339843
 

Similar to Java networking basics & sockets overview (20)

Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptx
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
 
Lecture6
Lecture6Lecture6
Lecture6
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
TCP IP
TCP IPTCP IP
TCP IP
 
Java networking
Java networkingJava networking
Java networking
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
#2 (UDP)
#2 (UDP)#2 (UDP)
#2 (UDP)
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programming
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programming
 
Networking
NetworkingNetworking
Networking
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in program
 

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Recently uploaded

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Java networking basics & sockets overview

  • 2.  The Inetaddress class provides you with a limited interface to DNS for doing both forward and reverse internet address lookups - An InetAddress class method corresponds to a DNS request  Npublic constructoro
  • 3.  Three static methods:  InetAddress getByName(String)  Static method used to retrieve the address for the host name passed as the parameter.  InetAddress [ ] getAllByName(String)  Static method used to retrieve all the addresses for the host name passed as a parameter.  InetAddress getLocalHost( )  Static method used to retrieve the address for the current, or local, host.
  • 4.  Three additional “getter” methods  String getHostName( )  Returns the host name.  byte[ ] getAddress( )  Returns the IP address.  String getHostAddress( )  Returns the IP address as a string.
  • 5. try { InetAddress fullname = netAddress.getByName(“bigyellowcat.cs.binghamton.edu"); InetAddress alias = InetAddress.getByName(“bigyellowcat"); InetAddress octets = InetAddress.getByName(“128.226.121.44"); if (fullname.equals(alias) && fullname.equals(octets)) // All is right with the world!} catch (UnknownHostException e) { // Exception handling here. }
  • 6.  Once a TCP socket connection is made, a virtual stream is in place. Java’s IO model is that of a stream, therefore the models are consistent; all you need to do connect a TCP socket to a stream and read and write the streams as normal
  • 7.  Socket(String ip, int port)  Creates a streaming socket and binds it to the host and port specified as parameters.  Socket(String ip, int port, boolean TCPorUDP)  Creates a socket and binds it to the host and port specified as parameters. The last parameter is used to indicate whether the socket should be a stream or datagram socket.  Socket(InetAddress ia, int port)  Creates a streaming socket connected to the specified host and port.  Socket(InetAddress ia, int port, boolean TCPorUDP)  Creates a socket connected to the specified host and port. The last parameter specifies whether the socket should be a stream or datagram socket.
  • 8.  InetAddress getInetAddress( )  Returns an InetAddress object representing the host for this socket.  Int getPort( )  . Returns the port number on the remote host for this socket  Int getLocalPort( )  Returns the port number on the local host for this socket.  InputStream getInputStream( )  Returns an input stream for the socket.  OutputStream getOutputStream( )  Returns an output stream for the socket.  Close( )  Closes the socket.  SetSocketImplFactory (SocketImplFactory)  Sets the socket factory that will be used to create all sockets.
  • 9. try { Socket socket = new Socket("somehost.somewhere.com", -1); // Always a good idea to buffer the stream to mitigate blocking. PrintStream out = new PrintStream( new BufferedOutputStream(socket.getOutputStream())); out.println("Are you listening?"); DataInputStream in = new DataInputStream( new BufferedInputStream(socket.getInputStream())); in.readLine(); // ... // Don't forget to close the socket! socket.close() } catch (Exception e) // Exception handling logic.
  • 10.  Since UDP is a connectionless protocol; there is no virtual stream between the hosts so streams are not used for IO.  UDP applications are not thought of in terms of clients and servers, but rather in terms of senders and receivers.  For conversational applications both ends (sender and receiver) will be changing states from sender to receiver and back again  Many UDP based applications are simple send a request then receive the data (sender’s perspective), like a DNS request. The receiver’s perspective is to ‘listen’ for a request, send the response, listen for more requests.
  • 11.  UDP sockets send and receive Datagrams  Constructors: two for receiving, four for sending  DatagramPacket( byte[ ] buff , int len)  Constructs a DatagramPacket for receiving packets of length len.  DatagramPacket(byte[] buf, int off, int len)  Constructs a DatagramPacket for receiving packets of length len, specifying an offset of off bytes into the buffer.  DatagramPacket((byte[] buf, int len, InetAddress addr, int port)  Constructs a datagram packet for sending packets of length len to the specified port number on the specified host.  DatagramPacker(byte[] buf, int off, int len, InetAddress addr, int port)  Constructs a datagram packet for sending packets of length len with offset off to the specified port number on the specified host.  DatagramPacket(byte[] buf, int off, int len, SocketAddress addr)  Constructs a datagram packet for sending packets of length len with offset off to the specified port number on the specified host.
  • 12.  Constructors  DatagramSocket()  Constructs a datagram socket and binds it to any available port on the local host.  DatagramSocket(DatagramSocketImpl impl)  Creates an unbound datagram socket with the specified DatagramSocketImpl.  DatagramSocket(int port)  Constructs a datagram socket and binds it to the specified port on the local host.  DatagramSocket(int port, InetAddress iaddr)  Creates a datagram socket, bound to the specified local address.  DatagramSocket(SocketAddress bindaddr)  Creates a datagram socket, bound to the specified local socket address.