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.
Ppt of socket

Ppt of socket

  • 1.
    Group Members Gurpreet Kaur(155) Himani Kakria Ritika Sharma Reetu Rani
  • 2.
    Contents O Socket Programming OServer-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 Providesupport 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 OIt 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.*; importjava.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"); } } }
  • 9.
  • 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(); } }
  • 11.
  • 12.
    getAddress() & getHostName() importjava.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); } } }
  • 13.
  • 14.
    Socket Class O TheSocket 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)InputStreamgetInputStream(); 1)OutputStream getOutputStream(); 1)void close();
  • 16.
    ServerSocket Class The ServerSocketclass, 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.