Chap 4 Networking
Chap 4 Networking
Networking
Prepared by,
Mrs Suwarna Thakre
Socket Overview
O A network socket is a lot like an electrical socket.
UnknownHostException
UnknownHostException
socket
Returns the port that the server socket is listening on. This method is useful if
you passed in 0 as the port number in a constructor and let the server find a
port for you.
O public Socket accept() throws IOException
Waits for an incoming client. This method blocks until either a client connects
to the server on the specified port or the socket times out, assuming that the
time-out value has been set using the setSoTimeout() method. Otherwise, this
method blocks indefinitely.
//program for client
import java.net.*;
import java.io.*;
public class client1
{ public static void main(String args[])
{ try
{ Socket ss=new Socket("127.0.0.1",3456);
InputStream is=ss.getInputStream();
OutputStream os=ss.getOutputStream();
DataInputStream dis=new DataInputStream(is);
DataOutputStream dos=new DataOutputStream(os);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter string");
String line=br.readLine();
dos.writeUTF(line);
String str=dis.readUTF();
System.out.println(str);}
catch(Exception e){} } }
//Server Side
import java.net.*;import java.io.*;
public class server1
{ public static void main(String args[])
{ try
{ ServerSocket ss=new ServerSocket(3456);
Socket s1=ss.accept();
System.out.println("Got Client");
InputStream is=s1.getInputStream();
OutputStream os=s1.getOutputStream();
DataInputStream dis=new DataInputStream(is);
DataOutputStream dos=new DataOutputStream(os);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String line=dis.readUTF();
System.out.println(line);
System.out.println("Enter Data");
String str=br.readLine();
dos.writeUTF(str); }
catch(Exception e) {}} }
OUTPUT
Uniform Resource Locator
(URL)
O URL: It is used to identify the
information on the Web.
components.
e.g. www.osborne.com:80
Uniform Resource Locator
(URL)
O Third component, the port number, is an
optional parameter, delimited on the left
from the host name by a colon (:) and on the
right by a slash (/).It defaults to port 80, the
predefined HTTP port.
O The fourth part is the actual file path
Methods of URL Class
public String getProtocol() it returns the protocol of the URL.
public int getDefaultPort() Returns the default port for the protocol of
the URL.
URL Constructors
O URL(String urlSpecifier) throws MalformedURLException
Creates a URL object from protocol, host, port and file name.
URL Constructors
O URL(String protocolName, String hostName, String
path) throws MalformedURLException
Identical to the previous constructor, except that
the default port for the given protocol is used.
O URL(URL urlObj, String urlSpecifier) throws
MalformedURLException
use an existing URL as a reference context and then
create a new URL from that context..
Program Using URL Class
import java.net.*;
class urldemo12346
System.out.println("Ext:" + hp.toExternalForm());
C:\jdk1.8.0_181\bin>java urldemo12346
Protocol: https
Port: -1
Host: www.geeksforgeeks.org
File: /download
Ext:https:// www.geeksforgeeks.org/download
default port443
Notice that the port is –1; this means that one was not
explicitly set.
Program Using URL Class
/URLDemo.java }
import java.io.*;
import java.net.*;
catch(Exception e)
public class urldemo1 {System.out.println(e);
{ }
public static void main(String[] args)
{ }
try }
{
String p="http";
String name="www.nba.com"; C:\jdk1.2.2\bin>javac urldemo1.java
String file="xyz//html";
URL url=new URL(p,name,file);
C:\jdk1.2.2\bin>java urldemo1
System.out.println("Protocol: "+url.getProtocol()); Protocol: http
System.out.println("Host Name: "+url.getHost()); Host Name: www.nba.com
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
Port Number: -1
File Name: xyz//html
URLConnection
O URLConnection is a general-purpose class for
URLConnection openConnection( )
It returns a URLConnection object associated with the
invoking URL object. It may throw an IOException.
Methods Of URLConnection
O public String getContentType()Returns the value of
between machines.
constructor specifies a buffer that will receive data, and the size of
a packet. It is used for receiving data over a DatagramSocket.
O DatagramPacket(byte data[ ], int offset, int size)
offset into the buffer at which data will be stored.
DatagramPacket Constructors
O DatagramPacket(byte data[ ], int size,
InetAddress ipAddress, int port)
The third form specifies a target address and
port, which are used by a DatagramSocket to
determine where the data in the packet will be
sent.
.
DatagramPacket Constructors
O DatagramPacket(byte data[ ], int offset, int size,
.
Constructors of DatagramSocket
O DatagramSocket() throws SocketEeption: it creates a
datagram socket and binds it with the available Port
Number on the localhost machine
O DatagramSocket(int port) throws SocketEeption: it
creates a datagram socket and binds it with the given
Port Number.
O DatagramSocket(int port, InetAddress address)
throws SocketEeption: it creates a datagram socket
and binds it with the specified port number and host
address.
//program for UDP client
import java.net.*;
public class udpclient
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
String str = "hello world";
InetAddress ip = InetAddress.getLocalHost();