Chapter 5 Networking Basics
Chapter 5 Networking Basics
Chapter 4
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
Syllabus
Prashant bhandare
Class Range
A 1 – 126*
B 128 – 191
C 192 – 223
D 224 – 239
E 240-255
The
Prashant bhandare first byte of a class C network is
between 192 and 224, with the last byte
actually identifying an individual computer
among the 256 allowed on a single class C
network.
CookieStore SocketImplFactory
DatagramSocketImplFactory SocketOption
InetAddress
Prashant bhandare
class InetAddressTest
{
public static void main(String args[]) throws
UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address =
InetAddress.getByName("www.HerbSchildt.com");
System.out.println(Address);
InetAddress SW[] =
InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Prashant bhandare
Instance Methods
Prashant bhandare
Returns a string that represents the host address associated with the
String getHostAddress( )
InetAddress object.
Returns a string that represents the host name associated with the
String getHostName( )
InetAddress object.
Returns a string that lists the host name and the IP address for
String toString( )
convenience.
Internet
Prashant bhandare addresses are looked up in a series of
hierarchically cached servers.
For the most part, you can simply use InetAddress when
working with IP addresses because it can accommodate
both styles.
Prashant bhandare
import java.io.*;
public class LowPortScanner
{
public static void main(String[] args)
{
String host = "localhost";
for (int i = 1; i < 1024; i++)
{
try {
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "
+ host);
}
catch (UnknownHostException ex)
Prashant bhandare
{
System.err.println(ex);
break;
}
catch (IOException ex)
{
// must not be a server on this port
}
} //end for
} // end main
} // end PortScanner
Whois
Prashant bhandare
import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) {
String data = "HELLO HOW ARE YOU.....";
try {
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Client not started \n");
}
}
}
TCP Client
Prashant bhandare
import java.lang.*;
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Server not started\n");
}
}
}
URL
Prashant bhandare
One of the most important aspects of the Web is that Tim Berners-
Lee devised a scalable way to locate all of the resources of the Net.
urlc = url.openConnection()
header information.
InetSocketAddress encapsulates an IP
address with a port number.
//DSender.java
import java.net.*;
public class DSender{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(),
str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
Prashant bhandare
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}