cs 6411 netwoks manual for cse 2013 regulation

  • Upload
    tiruman

  • View
    895

  • Download
    0

Embed Size (px)

DESCRIPTION

A rough copy of lab maual for cse 4 th semester annauniversity chennai 2013 regulation you can try here,all the best,

Citation preview

TO IMPLEMENT SLIDING WINDOW PROTOCOL SLIDING WINDOW//slireceiver.java

import java.io.*;import java.net.*;class sliclient{public static void main(String aafhbasgj[])throws Exception{

Socket s=new Socket("localhost",6483);String opmsg;BufferedReader br=new BufferedReader(new InputStreamReader(System.in));DataInputStream in=new DataInputStream(s.getInputStream());PrintStream dos=new PrintStream(s.getOutputStream());int iterations=Integer.parseInt(in.readLine());String msg=in.readLine();int no=Integer.parseInt(msg);int i=0,intr=0,iframe=0;int framecounter=0;while(intr0m

waiting for ack.....

receiver > packet recieved

data sent>1y

waiting for ack.....

receiver > packet recieved

data sent>0n

waiting for ack.....

receiver > packet recieved

data sent>1a

waiting for ack.....

Time out resending data....

data sent>1a

waiting for ack.....

receiver > packet recieved

data sent>0m

waiting for ack.....

receiver > packet recieved

data sent>1e

waiting for ack.....

receiver > packet recieved

All data sent. exiting.

//RECEIVER OUTPUTwaiting for connection...

Connection established :

receiver >0m

receiver >1y

receiver >0n

receiver >1a

receiver >1a duplicate data

receiver >0m

receiver >1e

Data recived=myname

waiting for connection...

CLIENT-SERVER APPLICATION FOR CHAT

AIM:

To write a client-server application for chat using TCP

ALGORITHM: CLIENT

1.start the program

2. Include necessary package in java

3. To create a socket in client to server.

4. The client establishes a connection to the server.

5. The client accept the connection and to send the data from client to server and vice versa

6. The client communicate the server to send the end of the message

7. Stop the program.

ALGORITHM: SERVER

1.start the program

2. Include necessary package in java

3. To create a socket in server to client

4. The server establishes a connection to the client.

5. The server accept the connection and to send the data from server to client and vice versa

6. The server communicate the client to send the end of the message

7. Stop the program.

TCPserver1.java

import java.net.*;

import java.io.*;

public class TCPserver1

{

public static void main(String arg[])

{

ServerSocket s=null;

String line;

DataInputStream is=null,is1=null;

PrintStream os=null;

Socket c=null;

try

{

s=new ServerSocket(9999);

}

catch(IOException e)

{

System.out.println(e);

}

try

{

c=s.accept();

is=new DataInputStream(c.getInputStream());

is1=new DataInputStream(System.in);

os=new PrintStream(c.getOutputStream());

do

{

line=is.readLine();

System.out.println("Client:"+line);

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

line=is1.readLine();

os.println(line);

}while(line.equalsIgnoreCase("quit")==false);

is.close();

os.close();

}

catch(IOException e)

{

System.out.println(e);

}

}

}

TCPclient1.java

import java.net.*;

import java.io.*;

public class TCPclient1

{

public static void main(String arg[])

{

Socket c=null;

String line;

DataInputStream is,is1;

PrintStream os;

try

{

c=new Socket("10.0.200.36",9999);

}

catch(IOException e)

{

System.out.println(e);

}

try

{

os=new PrintStream(c.getOutputStream());

is=new DataInputStream(System.in);

is1=new DataInputStream(c.getInputStream());

do

{

System.out.println("Client:");

line=is.readLine();

os.println(line);

System.out.println("Server:" + is1.readLine());

}while(line.equalsIgnoreCase("quit")==false);

is1.close();

os.close();

}

catch(IOException e)

{

System.out.println("Socket Closed!Message Passing is over");

}

}

OUT PUT :

Server

C:\Program Files\Java\jdk1.5.0\bin>javac TCPserver1.java

Note: TCPserver1.java uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

C:\Program Files\Java\jdk1.5.0\bin>java TCPserver1

Client: Hai Server

Server:

Hai Client

Client: How are you

Server:

Fine

Client: quit

Server:

quit

Client

C:\Program Files\Java\jdk1.5.0\bin>javac TCPclient1.java

Note: TCPclient1.java uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1

Client:

Hai Server

Server: Hai Client

Client:

How are you

Server: Fine

Client:

quit

Server: quit

RESULT:

Thus the above program a client-server application for chat using TCP / IP was executed and successfully.

PROGRAM USING UDP SOCKET

AIM:

To write a client-server application for chat using UDP

ALGORITHM: CLIENT

1. Include necessary package in java

2. To create a socket in client to server.

3. The client establishes a connection to the server.

4. The client accept the connection and to send the data from client to server and vice versa

5. The client communicate the server to send the end of the message

6. Stop the program.

ALGORITHM: SERVER

1. Include necessary package in java

2. To create a socket in server to client

3. The server establishes a connection to the client.

4. The server accept the connection and to send the data from server to client and vice versa

5. The server communicate the client to send the end of the message

6. Stop the program.

Program :

UDPserver.java

import java.io.*;

import java.net.*;

class UDPserver

{

public static DatagramSocket ds;

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

public static int clientport=789,serverport=790;

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

{

ds=new DatagramSocket(clientport);

System.out.println("press ctrl+c to quit the program");

BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));

InetAddress ia=InetAddress.getByName("localhost");

while(true)

{

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

ds.receive(p);

String psx=new String(p.getData(),0,p.getLength());

System.out.println("Client:" + psx);

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

String str=dis.readLine();

if(str.equals("end"))

break;

buffer=str.getBytes();

ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));

}

}

}

UDPclient.java

import java .io.*;

import java.net.*;

class UDPclient

{

public static DatagramSocket ds;

public static int clientport=789,serverport=790;

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

{

byte buffer[]=new byte[1024];

ds=new DatagramSocket(serverport);

BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));

System.out.println("server waiting");

InetAddress ia=InetAddress.getByName("10.0.200.36");

while(true)

{

System.out.println("Client:");

String str=dis.readLine();

if(str.equals("end"))

break;

buffer=str.getBytes();

ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));

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

ds.receive(p);

String psx=new String(p.getData(),0,p.getLength());

System.out.println("Server:" + psx);

}

}

}

Output

Server

C:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.java

C:\Program Files\Java\jdk1.5.0\bin>java UDPserver

press ctrl+c to quit the program

Client:Hai Server

Server:

Hello Client

Client:How are You

Server:

I am Fine what about you

Client

C:\Program Files\Java\jdk1.5.0\bin>javac UDPclient.java

C:\Program Files\Java\jdk1.5.0\bin>java UDPclient

server waiting

Client:

Hai Server

Server:Hello Clie

Client:

How are You

Server:I am Fine w

Client:

end

RESULT:

Thus the above program a client-server application for chat using UDP was executed and successfully

ARP Client/Server

Aim

To know the physical address of a host when its logical address is known using ARP

protocol.

Algorithm

Target/Server

1. Create a server socket.

2. Accept client connection.

3. Read IP address from the client request

4. Check its configuration file and compare with its logical address.

5. If there is a match, send the host physical address.

6. Stop

Client

1. Create a socket.

2. Send IP address to the target machine

3. Receive target's response

4. If it is a MAC address then display it and go to step 6

5. Display "Host not found"

6. Stop

Result

Thus using ARP protoocl MAC address is obtained.

Program

//ARP Server -- arpserver.java

import java.io.*;

import java.net.*;

class arpserver

{

public static void main(String args[])throws IOException

{

try

{

ServerSocket soc = new ServerSocket(2500);

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

Socket client = null;

client = soc.accept();

String str;

PrintStream ps = new

PrintStream(client.getOutputStream());

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

Runtime r = Runtime.getRuntime();

Process p = r.exec("ifconfig eth0");

BufferedReader pin=new BufferedReader(new

InputStreamReader(p.getInputStream()));

String haddr = "";

String ipaddr = br.readLine();

int flag = 0;

while((str = pin.readLine())!=null)

{

System.out.println(str);

if((str.indexOf("HWaddr")) != -1)

{

int tlen = str.length();

int hlen = tlen - 19;

haddr = str.substring(hlen,tlen);

}

else if ((str.indexOf(ipaddr)) != -1)

{

flag = 1;

}

}

if (flag == 1)

ps.println(haddr);

ps.close();

br.close();

pin.close();

client.close();

soc.close();

}

catch(IOException io)

{

System.err.println("Exception : " + io.toString());

}

}

}

//ARP Client -- arpclient.java

import java.io.*;

import java.net.*;

class arpclient

{

public static void main(String args[])

{

try

{

Socket client = new Socket("localhost", 2500);

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

PrintStream ps = new

PrintStream(client.getOutputStream());

String ipaddr,haddr = null;

BufferedReader sin = new BufferedReader(new

InputStreamReader(client.getInputStream()));

System.out.print("Enter the IP address : ");

ipaddr = br.readLine();

ps.println(ipaddr);

haddr = sin.readLine();

if (haddr == null)

System.out.println("Host does not exist");

else

System.out.println("Physical Address " + haddr);

ps.close();

br.close();

client.close();

}

catch(IOException io)

{

System.err.println(io.toString());

}

}

}

Output

Server

$ javac arpserver.java

$ java arpserver

Server started

eth0 Link encap:Ethernet HWaddr B8:AC:6F:1B:AB:06

inet addr:172.16.6.21 Bcast:172.255.255.255 Mask:255.0.0.0

inet6 addr: fe80::baac:6fff:fe1b:ab06/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:450 errors:0 dropped:0 overruns:0 frame:0

TX packets:127 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:48118 (46.9 KiB) TX bytes:21025 (20.5 KiB)

Interrupt:16

Client

$ javac arpclient.java

$ java arpclient

Enter the IP address : 172.16.6.21

Physical Address B8:AC:6F:1B:AB:06

5D RARP Client/Server

Aim

To know the logical address of a host when its physical address is known using RARP

protocol.

Algorithm

Target/Server

1. Create a server socket.

2. Accept client connection.

3. Read MAC address from the client request

4. Check its configuration file and compare with its physical address.

5. If there is a match, send the host logical address.

6. Stop

Client

1. Create a socket.

2. Send physical address to the target machine

3. Receive target's response

4. If it is a IP address then display it and go to step 6

5. Display "Host not found"

6. Stop

Result

Thus using RARP protocol, IP address is obtained.

//RARP Server -- rarpserver.java

import java.io.*;

import java.net.*;

class rarpserver

{

public static void main(String args[])throws IOException

{

try

{

ServerSocket soc = new ServerSocket(2500);

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

Socket client = null;

client = soc.accept();

String str;

PrintStream ps = new

PrintStream(client.getOutputStream());

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

Runtime r = Runtime.getRuntime();

Process p = r.exec("ifconfig eth0");

BufferedReader pin = new BufferedReader(new

InputStreamReader(p.getInputStream()));

String ipaddr = "";

String haddr = br.readLine();

int flag = 0;

while((str = pin.readLine())!=null)

{

System.out.println(str);

if ((str.indexOf(haddr)) != -1)

{

flag = 1;

}

else if((str.indexOf("inet addr")) != -1)

{

int pos = str.indexOf("inet addr:") + 10;

int offset = pos + 13;

ipaddr = str.substring(pos,offset);

}

}

if (flag == 1)

ps.println(ipaddr);

ps.close();

br.close();

pin.close();

client.close();

soc.close();

}

catch(IOException io)

{

System.err.println("Exception : " + io.toString());

}

}

}

// RARP Client -- rarpclient.java

import java.io.*;

import java.net.*;

class rarpclient

{

public static void main(String args[])

{

try

{

Socket client = new Socket("localhost", 2500);

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

PrintStream ps = new

PrintStream(client.getOutputStream());

String haddr,ipaddr = null;

BufferedReader sin = new BufferedReader(new

InputStreamReader(client.getInputStream()));

System.out.print("Enter the physical address : ");

haddr = br.readLine();

ps.println(haddr);

ipaddr = sin.readLine();

if (ipaddr == null)

System.out.println("Host does not exist");

else

System.out.println("Logical Address " + ipaddr);

ps.close();

br.close();

client.close();

}

catch(IOException io)

{

System.err.println(io.toString());

}

}

}

Output

Server

$ javac rarpserver.java

$ java rarpserver

Server started

eth0 Link encap:Ethernet HWaddr B8:AC:6F:1B:AB:06

inet addr:172.16.6.21 Bcast:172.255.255.255 Mask:255.0.0.0

inet6 addr: fe80::baac:6fff:fe1b:ab06/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:450 errors:0 dropped:0 overruns:0 frame:0

TX packets:127 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:48118 (46.9 KiB) TX bytes:21025 (20.5 KiB)

Interrupt:16

Client

$ javac rarpclient.java

$ java rarpclient

Enter the physical address : B8:AC:6F:1B:AB:06

Logical Address 172.16.6.21Ping server and client

package in.techdive.java;

import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;

public class PingPoller{ public static void main(String[] args) { System.out.println("Ping Poller Starts...");

String ipAddress = "localhost";

try { InetAddress inet = InetAddress.getByName(ipAddress); System.out.println("Sending Ping Request to " + ipAddress);

boolean status = inet.isReachable(5000); //Timeout = 5000 milli seconds

if (status) { System.out.println("Status : Host is reachable"); } else { System.out.println("Status : Host is not reachable"); } } catch (UnknownHostException e) { System.err.println("Host does not exists"); } catch (IOException e) { System.err.println("Error in reaching the Host"); } }}

import java.io.*;

import java.net.*;

import java.util.*;

public class HTTPFileUploadServer extends Thread {

static final String HTML_START =

"" +

"HTTP POST Server in java" +

"";

static final String HTML_END =

"" +

"";

Socket connectedClient = null;

DataInputStream inFromClient = null;

DataOutputStream outToClient = null;

public HTTPFileUploadServer(Socket client) {

connectedClient = client;

}

void closeStreams() throws Exception {

inFromClient.close();

outToClient.close();

connectedClient.close();

}

Remote procedure call

Program

// Declares remote method interfaces--CalcInf.java

import java.rmi.*;

public interface CalcInf extends Remote

{

public long add(int a, int b) throws RemoteException;

public int sub(int a, int b) throws RemoteException;

public long mul(int a, int b) throws RemoteException;

public int div(int a, int b) throws RemoteException;

public int rem(int a, int b) throws RemoteException;

}

// Implement Remote behavior--CalcImpl.java

import java.rmi.*;

import java.rmi.server.*;

public class CalcImpl extends UnicastRemoteObject implements

CalcInf

{

public CalcImpl() throws RemoteException { }

public long add(int a, int b) throws RemoteException

{

return a + b;

}

public int sub(int a, int b) throws RemoteException

{

int c = a > b ? a - b : b - a;

return c;

}

public long mul(int a, int b) throws RemoteException

{

return a * b;

}

public int div(int a, int b) throws RemoteException

{

return a / b;

}

public int rem(int a, int b) throws RemoteException

{

return a % b;

}

}

// Server that names the service implemented--CalcServer.java

import java.rmi.*;

public class CalcServer

{

public static void main(String args[])

{

try {

CalcInf C = new CalcImpl();

Naming.rebind("CalcService", C);

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

// Client that invokes remote host methods--CalcClient.java

import java.rmi.*;

import java.net.*;

public class CalcClient

{

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

{

try {

CalcInf C = (CalcInf) Naming.lookup("rmi://" +

args[0] + "/CalcService");

int a, b;

if (args.length != 3)

{

System.out.println("Usage: java CalcClient

");

System.exit(-1);

}

a = Integer.parseInt(args[1]);

b = Integer.parseInt(args[2]);

System.out.println( "\nBasic Remote Calc\n" );

System.out.println("Summation : " + C.add(a, b));

System.out.println("Difference : " + C.sub(a, b));

System.out.println("Product : " + C.mul(a, b));

System.out.println("Quotient : " + C.div(a, b));

System.out.println("Remainder : " + C.rem(a, b));

}

catch (Exception E) {

System.out.println(E.getMessage());

}

}

}

Output

Server

C:\>javac CalcInf.java

C:\>javac CalcImpl.java

C:\>javac CalcServer.java

C:\>javac CalcClient.java

C:\>rmic CalcImpl

C:\>start rmiregistry

C:\>java CalcServer

Client

C:\>java CalcClient 172.16.6.45 6 8

Basic Remote Calc

Summation : 14

Difference : 2

Product : 48

Quotient : 0

Remainder :

Program for subnetting and subnet masking in java.

import java.util.Scanner;class Subnet{public static void main(String args[]){Scanner sc = new Scanner(System.in);System.out.print(Enter the ip address: );String ip = sc.nextLine();String split_ip[] = ip.split(\\.); //SPlit the string after every .String split_bip[] = new String[4]; //split binary ipString bip = ;for(int i=0;i 18 => 10010 => 00010010bip += split_bip[i];}System.out.println(IP in binary is +bip);System.out.print(Enter the number of addresses: );int n = sc.nextInt();

//Calculation of maskint bits = (int)Math.ceil(Math.log(n)/Math.log(2)); /*eg if address = 120, log 120/log 2 gives log to the base 2 => 6.9068, ceil gives us upper integer */System.out.println(Number of bits required for address = +bits);int mask = 32-bits;System.out.println(The subnet mask is = +mask);

//Calculation of first address and last addressint fbip[] = new int[32];for(int i=0; i31-bits;i)//Get first address by ANDing last n bits with 0fbip[i] &= 0;String fip[] = {,,,};for(int i=0;i 1)

{

System.out.println("Usage: java hostipaddr");

System.exit(-1);

}

if (args.length == 0)

Clt = new Socket(InetAddress.getLocalHost(),5555);

else

Clt = new Socket(InetAddress.getByName(args[0]),

5555);

toServer = new PrintWriter(new BufferedWriter(new

OutputStreamWriter(Clt.getOutputStream())), true);

fromServer = new BufferedReader(new

InputStreamReader(Clt.getInputStream()));

fromUser = new BufferedReader(new

InputStreamReader(System.in));

String CltMsg, SrvMsg;

System.out.println("Type \"end\" to Quit");

while (true)

{

System.out.print("\nMessage to Server : ");

CltMsg = fromUser.readLine();

toServer.println(CltMsg);

if (CltMsg.equals("end"))

break;

SrvMsg = fromServer.readLine();

System.out.println("Client