SlideShare une entreprise Scribd logo
1  sur  17
Group Members
Gurpreet Kaur(155)
  Himani Kakria
  Ritika Sharma
   Reetu Rani
Contents
O Socket Programming
O Server-Client Communication
O .net Package
O The InetAddress Class & its methods
      O getLocalHost()
      O getByName(String hostName)
      O getAllByName(String hostName)
      O getAddress() & getHostName()
O Socket Class
O ServerSocketClass
Socket Programming
Networking - process of making two or more computers
communicate.

The important concept associated with networking is the concept of
Sockets and Ports.

SOCKET - A socket is one end-point of a two-way communication link
between two programs running on the network.

Socket classes are used to represent the connection between a client
program and a server program.
Server-Client Communication
O Normally, a server has a socket that is bound to a specific port
    number.
O   On the client-side: The client knows the hostname of the machine
    on which the server is running and the port number on which the
    server is listening.
O   To make a connection request, the client tries to connect with the
    server on the server's machine and port.
O   If everything goes well, the server accepts the connection.
O   On the client side, if the connection is accepted, a socket is
    successfully created and the client can use the socket to
    communicate with the server.
.net Package
O Provide support for networking.


O Contains classes and interfaces to encapsulate the “Socket”
  Paradigm.

O The java.net package provides two classes—
     O Socket - implement the client side of the connection
     O Ser verSocket- implement the server side of the connection
The InetAddress Class
O It converts the domain name of Internet address into an
  Object. This class represents an Internet Protocol (IP)
  address.

O Doesn’t have Constructors.


O Three static methods are used to create instances.
      O getLocalHost()
      O getByName(String hostName)
      O getAllByName(String hostName)


O All these methods throw UnknownHostException if the
  method cannot resolve the host name.
getLocalHost()
import java.net.*;
class obtainIP
{
          public static void main(String args []) throws UnknownHostException
          {
                    InetAddress adr;
                    adr=InetAddress.getLocalHost();
                    System.out.println("nInfo about Machine: " + adr);
          }
}
getByName(String hostName)
import java.io.*;
import java.net.*;
class obtainIP2
{           public static void main(String args []) throws IOException
            {           InetAddress adr;
                        String host;
                        DataInputStream input=new DataInputStream(System.in);
                        System.out.println("Enter the Machine's Hostname");
                        host=input.readLine();
            try {
                        adr=InetAddress.getByName(host);
                        System.out.println("nInfo about Host "" + host + "" is:- " + adr);
                }
            catch( UnknownHostException e) {
                        System.out.println("No such host exist"); }
            }
}
Output
getAllByName(String hostName)

import java.io.*;                          try {
import java.net.*;                              adr=InetAddress.getAllByName(host);
class obtainIP3                                 for(int i=0;i<adr.length;i++)
{                                               {
public static void main(String args [ ])            j++;
throws IOException                                  System.out.println(j +"t" + adr[i]);
 {                                               }
     InetAddress adr[]; int j=0;                }
     String host;
     DataInputStream input=new             catch( UnknownHostException e)
DataInputStream(System.in);                    {
                                             System.out.println("No such host exist");
     System.out.println("Enter host ");        }
     host=input.readLine();                }
                                           }
Output
getAddress() & getHostName()
import java.net.*;
class obtainIP4
{           public static void main(String args []) throws UnknownHostException
            {
                        String msg;
                        byte num[];
                        InetAddress adr;
                        adr=InetAddress.getLocalHost();
                        num=adr.getAddress();
                        msg=adr.getHostName();
                        System.out.println("LocalHost= " + adr);
                        for(int i=0;i<num.length;i++)
                        {
                                     msg +=(num[i] & 255) + ".";
                                     System.out.println("Num= " + msg);
                        }
            }
}
Output
Socket Class
O The Socket class ,basically used to create client sockets, is
  designed to connect to server socket and initiate protocol
  exchanges.

O When Socket object is created, it implicitly establishes the
  connection b/w client and server.

O Socket defines the constructors to create client sockets.


Constructors:-
  Socket(String hostname, int port);
  Socket(InetAdrress ipaddr, int port);
Socket Class Methods
Methods:-

1)InputStream getInputStream();


1)OutputStream getOutputStream();


1)void close();
ServerSocket Class
The ServerSocket class, basically used to create server sockets,
is designed to be a listener, which waits for client to connect
before doing anything.

Constructors:-
       ServerSocket ( int port );
       ServerSocket ( int port, int queueLength);
Method:-
       Socket accept();

accept() is a blocking call that waits for a client to initiate
communication and returns a normal Socket, that is used for
communication with the client.
Group Members and Socket Programming Overview

Contenu connexe

Tendances (20)

Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
IP Address - IPv4 & IPv6
IP Address - IPv4 & IPv6IP Address - IPv4 & IPv6
IP Address - IPv4 & IPv6
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Java arrays
Java arraysJava arrays
Java arrays
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Subnetting
SubnettingSubnetting
Subnetting
 
Socket programming
Socket programmingSocket programming
Socket programming
 

Similaire à Group Members and Socket Programming Overview

Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...Mumbai B.Sc.IT Study
 
nw-lab_dns-server.pdf
nw-lab_dns-server.pdfnw-lab_dns-server.pdf
nw-lab_dns-server.pdfJayaprasanna4
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client servertrilestari08
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13Sasi Kala
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxDhrumilSheth3
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programmingashok hirpara
 
Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi clientichsanbarokah
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programmingAnung Ariwibowo
 
Distributed systems
Distributed systemsDistributed systems
Distributed systemsSonali Parab
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopyayaria
 

Similaire à Group Members and Socket Programming Overview (20)

java sockets
 java sockets java sockets
java sockets
 
Lecture6
Lecture6Lecture6
Lecture6
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
Internet Technology (Practical Questions Paper) [CBSGS - 75:25 Pattern] {Mast...
 
nw-lab_dns-server.pdf
nw-lab_dns-server.pdfnw-lab_dns-server.pdf
nw-lab_dns-server.pdf
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client server
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
CHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptxCHAPTER - 3 - JAVA NETWORKING.pptx
CHAPTER - 3 - JAVA NETWORKING.pptx
 
Network
NetworkNetwork
Network
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
JavaExamples
JavaExamplesJavaExamples
JavaExamples
 
Laporan multi client
Laporan multi clientLaporan multi client
Laporan multi client
 
分散式系統
分散式系統分散式系統
分散式系統
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Distributed systems
Distributed systemsDistributed systems
Distributed systems
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
DCN Practical
DCN PracticalDCN Practical
DCN Practical
 
Java networking
Java networkingJava networking
Java networking
 

Plus de Amandeep Kaur

Video/ Graphics cards
Video/ Graphics  cardsVideo/ Graphics  cards
Video/ Graphics cardsAmandeep Kaur
 
Menu pop up menu mdi form and playing audio in vb
Menu pop up menu mdi form and playing audio in vbMenu pop up menu mdi form and playing audio in vb
Menu pop up menu mdi form and playing audio in vbAmandeep Kaur
 
Image contro, and format functions in vb
Image contro, and format functions in vbImage contro, and format functions in vb
Image contro, and format functions in vbAmandeep Kaur
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vbAmandeep Kaur
 
Toolbar, statusbar, coolbar in vb
Toolbar, statusbar, coolbar in vbToolbar, statusbar, coolbar in vb
Toolbar, statusbar, coolbar in vbAmandeep Kaur
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphicsAmandeep Kaur
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphicsAmandeep Kaur
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphicsAmandeep Kaur
 
Report on browser war
Report on browser warReport on browser war
Report on browser warAmandeep Kaur
 
Report of internet connections
Report of internet connectionsReport of internet connections
Report of internet connectionsAmandeep Kaur
 

Plus de Amandeep Kaur (20)

Video/ Graphics cards
Video/ Graphics  cardsVideo/ Graphics  cards
Video/ Graphics cards
 
Menu vb
Menu vbMenu vb
Menu vb
 
Menu pop up menu mdi form and playing audio in vb
Menu pop up menu mdi form and playing audio in vbMenu pop up menu mdi form and playing audio in vb
Menu pop up menu mdi form and playing audio in vb
 
Active x control
Active x controlActive x control
Active x control
 
Image contro, and format functions in vb
Image contro, and format functions in vbImage contro, and format functions in vb
Image contro, and format functions in vb
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vb
 
Toolbar, statusbar, coolbar in vb
Toolbar, statusbar, coolbar in vbToolbar, statusbar, coolbar in vb
Toolbar, statusbar, coolbar in vb
 
Richtextbox
RichtextboxRichtextbox
Richtextbox
 
Treeview listview
Treeview listviewTreeview listview
Treeview listview
 
Progress bar
Progress barProgress bar
Progress bar
 
Filehandling
FilehandlingFilehandling
Filehandling
 
Socket
SocketSocket
Socket
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
 
Internet
InternetInternet
Internet
 
Internet working
Internet workingInternet working
Internet working
 
Report on browser war
Report on browser warReport on browser war
Report on browser war
 
Report of internet connections
Report of internet connectionsReport of internet connections
Report of internet connections
 
Report on intranet
Report on intranetReport on intranet
Report on intranet
 

Dernier

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
[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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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
 

Dernier (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
[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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 

Group Members and Socket Programming Overview

  • 1. Group Members Gurpreet Kaur(155) Himani Kakria Ritika Sharma Reetu Rani
  • 2. Contents O Socket Programming O Server-Client Communication O .net Package O The InetAddress Class & its methods O getLocalHost() O getByName(String hostName) O getAllByName(String hostName) O getAddress() & getHostName() O Socket Class O ServerSocketClass
  • 3. Socket Programming Networking - process of making two or more computers communicate. The important concept associated with networking is the concept of Sockets and Ports. SOCKET - A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program.
  • 4. Server-Client Communication O Normally, a server has a socket that is bound to a specific port number. O On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. O To make a connection request, the client tries to connect with the server on the server's machine and port. O If everything goes well, the server accepts the connection. O On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.
  • 5. .net Package O Provide support for networking. O Contains classes and interfaces to encapsulate the “Socket” Paradigm. O The java.net package provides two classes— O Socket - implement the client side of the connection O Ser verSocket- implement the server side of the connection
  • 6. The InetAddress Class O It converts the domain name of Internet address into an Object. This class represents an Internet Protocol (IP) address. O Doesn’t have Constructors. O Three static methods are used to create instances. O getLocalHost() O getByName(String hostName) O getAllByName(String hostName) O All these methods throw UnknownHostException if the method cannot resolve the host name.
  • 7. getLocalHost() import java.net.*; class obtainIP { public static void main(String args []) throws UnknownHostException { InetAddress adr; adr=InetAddress.getLocalHost(); System.out.println("nInfo about Machine: " + adr); } }
  • 8. getByName(String hostName) import java.io.*; import java.net.*; class obtainIP2 { public static void main(String args []) throws IOException { InetAddress adr; String host; DataInputStream input=new DataInputStream(System.in); System.out.println("Enter the Machine's Hostname"); host=input.readLine(); try { adr=InetAddress.getByName(host); System.out.println("nInfo about Host "" + host + "" is:- " + adr); } catch( UnknownHostException e) { System.out.println("No such host exist"); } } }
  • 10. getAllByName(String hostName) import java.io.*; try { import java.net.*; adr=InetAddress.getAllByName(host); class obtainIP3 for(int i=0;i<adr.length;i++) { { public static void main(String args [ ]) j++; throws IOException System.out.println(j +"t" + adr[i]); { } InetAddress adr[]; int j=0; } String host; DataInputStream input=new catch( UnknownHostException e) DataInputStream(System.in); { System.out.println("No such host exist"); System.out.println("Enter host "); } host=input.readLine(); } }
  • 12. getAddress() & getHostName() import java.net.*; class obtainIP4 { public static void main(String args []) throws UnknownHostException { String msg; byte num[]; InetAddress adr; adr=InetAddress.getLocalHost(); num=adr.getAddress(); msg=adr.getHostName(); System.out.println("LocalHost= " + adr); for(int i=0;i<num.length;i++) { msg +=(num[i] & 255) + "."; System.out.println("Num= " + msg); } } }
  • 14. Socket Class O The Socket class ,basically used to create client sockets, is designed to connect to server socket and initiate protocol exchanges. O When Socket object is created, it implicitly establishes the connection b/w client and server. O Socket defines the constructors to create client sockets. Constructors:- Socket(String hostname, int port); Socket(InetAdrress ipaddr, int port);
  • 15. Socket Class Methods Methods:- 1)InputStream getInputStream(); 1)OutputStream getOutputStream(); 1)void close();
  • 16. ServerSocket Class The ServerSocket class, basically used to create server sockets, is designed to be a listener, which waits for client to connect before doing anything. Constructors:- ServerSocket ( int port ); ServerSocket ( int port, int queueLength); Method:- Socket accept(); accept() is a blocking call that waits for a client to initiate communication and returns a normal Socket, that is used for communication with the client.