Wednesday, December 19, 2007

JAVA 2.0 NOTE

>>PREVIOUS>>
11. Networking in Java


Java.net package

The objectives are

Network Datagram

Protocol DatagramPacket

Socket DatagramSocket

Client/Server TCP/IP

Internet (IP) Address Socket

Domain Name Service ServerSocket


InetAddress URL


Network is a set of computers physically connected together. It enables the sharing of computer
pheriperals and resources by different computers.

The communication between the computers requires certain set of rules called protocols. Some important protocols are TCP/IP, HTTP, FTP, SMTP and NNTP. Port no 21 is for FTP, 23 is for Telnet, 25 is for e-mail and 80 is for HTTP.

IP is a low-level routing protocol that breaks data into small packets and sends them to an address across a network. TCP is a higher level protocol that manages string together these packets, sorting and retransmitting them to reliably transmit your data. UDP (user Datagram Protocol) can be used to support fast, connectionless, unreliable transport of packets.

Socket is the place used to plug in just like electric sockets, from the socket the communication starts.

Client is the machine, which sends some request to another computer. The computer that does the request is called the Server.

A proxy server speaks the client side of protocol to another server. A client would connect to a proxy server, which have no restrictions, and the proxy server would in turn communicate for the client.

Every computer connected to the network has unique address it contains four numbers between 0 and 255 separated by period. Ex is 80.0.0.50

It is very difficult to remember the IP of the computer. To overcome this problem domain name service (DNS) is used. It maps the IP address by string of characters. Ex is www.microsoft.com

InetAddress is a class, which is used to encapsulate IP address and DNS.

InetAddress getLoaclHost( ) throws unknowHostException

InetAddress getByName (String hostName)

InetAddress getAllByName (String hostName)


URL

Uniform Resource Locater. It contains four parts protocol, separated by : and //, host name of IP address, port number, file path.

For ex http://www.starwave.com/index.html

Constructors of URL throws MalformedURLException

URL (String urlspecifier)

URL (String protocolName, String hostName, int port,String path)

URL (String protocolName, String hostName, String path)

getPort( ), getHost( ),getFile( ) and toExternalForm( )



To access content information of a URL, use url.openConnection( ) method.


Datagrams

Datagrams are bundles of information passed between machines. It contains two classes

DatagramPacket for container of data

DatagramSocket for send or receive the DatagramPacket

DatagramPacket constructors are

DatagramPacket (byte data[ ], int size);

DatagramPacket (byte data[ ], int size, ipAddress, int port);


Methods of DatagramPacket are

InetAddress getAddress( ), int getPort( ) , byte( ) , getData( ) and

int getLength( )DatagramSocket constructors are

DatagramSocket.send (DatagramPacket d);

DatagramSocket.receive (DatagramPacket p);

client.java

import java.net.*;

class client

{

public static DatagramSocket ds;

public static byte buffer[] = new byte[1024];


public static void main(String arg[]) throws Exception

{
ds = new DatagramSocket(6);

while(true)

{

String a;

DatagramPacket p = new DatagramPacket(buffer,buffer.length);

ds.receive(p);

a=new String(p.getData(),p.getLength(),1);

if (a.equals("q"))

{

System.out.println("Server response is shut off now");

return;

}

System.out.println(new String(p.getData(),0,p.getLength()));

}

}


}
server.java

import java.net.*;

class server

{

public static DatagramSocket ds;

public static byte buffer[] = new byte[1024];

public static void main(String a[]) throws Exception

{

InetAddress ia = InetAddress.getByName("rad-tm-04");

System.out.println(ia);

ds = new DatagramSocket(123);

int pos=0;

while(true)

{

int c = System.in.read();

switch(c)

{

case 'q':

System.out.println("Server quits");

return;

case '\r':

break;

case '\n':

ds.send(new DatagramPacket(buffer,pos,ia,456));

pos=0;

break;

default:

buffer[pos++] = (byte) c;

}

}

}

}



TCP/IP


TCP/IP sockets are used to implement reliable, bidirectional, persistent, point to point, stream based connection between hosts on the Internet.

It contains two classes. They are Socket and ServerSocket. ServerSocket class is designed to wait for clients to connect and Socket class is used to connect to ServerSocket.

ServerSocket (int port)

ServerSocket (int port, int maxqu)

ServerSocket (int port, int maxqu, InetAddress localAddress)

ServerSocket has a method accept( ) that waits for client to initiate communication.

Socket (String hostName, int port)

Socket (InetAddress ipAddress, int port)


>>NEXT>>

No comments: