29, మార్చి 2012, గురువారం

NETWORKING IMPLEMENTATION IN JAVA LIKE CONNECTION ORIENTED SERVICES AND CONNECTION LESS SERVICES AND SO ON ………


NETWORKING
NETWORK:- Interconnections of computers is called a “network”
CLIENT:-A client is a machine that sends a request for same service.
SERVER:-Server is a machine that provides services to the clients.
In a network we can have several servers and clients.
Requirements for the implementation of a network :-
1. Hardware:-physical devices
2. Software: - programs
          Software always drives the hardware.This program makes the hardware to perform the tasks.
3. Protocol: - set of rules or specifications. Rules should be followed by the computers in the network.
 some of the most popular protocols
TCP/IP: - Transmission control protocol/Internet protocol.-It is a connection oriented reliable protocol.
-used for sending messages or text messages
UDP: - User Datagram protocol
-It is a connectionless unreliable protocol-used for sending Audio and Video.
-The most widely used protocol is HTTP (Hypertext transfer protocol)-HTTP protocol transferring the pages
-FTP (file transfer protocol) – for downloading the file
-SMTP (Simple mail transfer protocol) – for sending Mails
-Pop (post office protocol) – for receiving mails.
4. Internet: - Internet is a network of computers
Internet is an interconnection network of all the computers on the earth.
5. Web browser: - it is software installed on an internet client.
6. Web server: - it is software installed on an internet server. IIS, Weblogic, Webdynamic, Websphere, Apache, JBOSS.

 IP address
Internet protocol address is a unique Id number given to every computer on a networkTo recognize each computer separately
Every computer will have separate IP address.
Every IP address has four numbers Each number range is from 0-255 only.

Ex:- 192.55.160.01      01-WWW     160-yahoo   55-homedirectory   192-file in the directory
DNS: - domain naming servers/systemIt is a service available on Internet which maps website names with corresponding IP addresses.
CLASSES OF IP ADDRESS
They are 5 types of IP addresses
Class A          0           Network7         Local address 24     16,777,216 hosts
Class B          10          Network14       Local address 16     65,536 hosts
Class C         110        Network21        Local address 8        256hosts
Class D        
Class E          These both are for Research
                

The current version is ipv6-     6bytes
IP address version 4 ipv4-       4bytes.
.com – commercial
.edu – educational
.org – government
.net – networking organization
.mil – military
Socket: - Socket represents a point connection between server and client.-In socket data/information will flow.
Different clients can be connected to same socketEvery socket will have unique identification number
Port Number:- It is a unique id number allotted to each socket.We can allot a new port number for a new client.Port number will change if the service changes.
Socket Programming: -Socket programming is the server and client programming to connect it server and client with the help of socket


Some allotted Port Numbers
PORT NO                                               APPLICATION
     13                                                         Date and time services
14                                                        FTP, which transfers file
      23                                                        Telnet, which provides Remote login
      25                                                        SMTP, which delivers Mail messages
      67                                                        Bootp, which provides configuration at boot time.
      80                                                        HTTP, which transfers Web page
109                                                                                                            pop, which enables users to access mail boxes on remote systems.
  
port number:- is a 2 byte number.(Up to 36000) first 3000 are allotted. Connect the server through socket is called Socket programming.


Server socket class is useful to create server side socket.
Socket class is used to create Client side socket
Server Socket and Socket available in java.net package.
//A server to send messages
import java.io.*;
import java.net.*;
class Server1
{
public static void main(String args[]) throws Exception
{
//create Server side Socket
ServerSocket ss = new ServerSocket(8888);
//make the Socket accept client connect
Socket s = ss.accept();
System.out.println("connection established");
//attach output Stream to the socket
OutputStream obj = s.getOutputStream();
//to send data form server to socket
PrintStream ps = new PrintStream(obj);


//send data form server
String str = "Hello Client";
ps.println(str);
ps.println("Bye");
//disconnect the server
ss.close();
s.close();
ps.close();
}
}
//client that receives messages from server
import java.io.*;
import java.net.*;
class Client1
{
public static void main(String args[]) throws Exception
{
//create the client socket
Socket s = new Socket("localhost",8888);
//connect InputStream to s
InputStream obj = s.getInputStream();
//use BufferedReader to receive data from the socket s
BufferedReader br = new BufferedReader(new InputStreamReader(obj));
//receive data coming form server
String str;
while((str = br.readLine()) != null)
System.out.println(str);
//disconnect the Client
s.close();
br.close();
}
}

connection less service implementation (UDP)

sendernet.java
import java.io.*;
import java.net.*;
public class sendernet
{
public static void main(String args[])  throws Exception
{
InetAddress destHost=InetAddress.getLocalHost();
int destPort =1234;
DatagramSocket ds=new DatagramSocket();
DataInputStream con=new DataInputStream(System.in);
String s=con.readLine();
while(true)
{
byte b[]=s.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,destHost,destPort);
ds.send(dp);
s=con.readLine();
}
}}


recivernet.java
import java.io.*;
import java.net.*;
public class recivernet
{
public static void main(String args[]) throws Exception
{
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length);
DatagramSocket ds=new DatagramSocket(1234);
while(true)
{
ds.receive(dp);
byte buf[]=dp.getData();
int n=dp.getLength();
System.out.println(new String(buf,0,n));
b=new byte[1024];
dp=new DatagramPacket(b,b.length);
}
}
}

program a server with multiple client handling concept

C:\Java\bin>ipconfig
Windows IP Configuration
Ethernet adapter Local Area Connection:
        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 167.192.0.12
        Subnet Mask . . . . . . . . . . . : 255.255.0.0
        Default Gateway . . . . . . . . . :
C:\Java\bin>

import java.io.*;
import java.net.*;
 class multiclientnet implements Runnable
{
static ServerSocket ss;
static Socket s;
public void run()
{
String name=Thread.currentThread().getName();
for(;;)
{
try
{
System.out.println("thread"+name+"ready to accept");
s=ss.accept();
System.out.println("thread"+name+"accepted connection");
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("thread "+name+"connected you");
Thread.sleep(20000);
ps.close();
s.close();
}
catch(Exception e){}
}
}
public static void main(String args[]) throws Exception
{
multiclient mc=new multiclient();
ss=new ServerSocket(777);
Thread t1= new Thread(mc,"one");
Thread t2= new Thread(mc,"two");
Thread t3= new Thread(mc,"three");
t1.start();
t2.start();
t3.start();
}}

import java.io.*;
import java.net.*;
class multiservernet
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("localhost",777);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str;
while((str=br.readLine())!=null)
{
System.out.println(str);
br.close();
s.close();
}
}








కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి