COMPUTER NETWORKS PROGRAMS

1. WRITE A PROGRAM TO MANIPULATE THE IP ADDRESS OF A SYSTEM.
import java.net.*;
public class inetaddrs
{
 public static void main(String args[]) throws UnknownHostException
 {
  InetAddress local=InetAddress.getLocalHost();
  System.out.println("Name :"+local.getHostName());
  System.out.println("Ip Address :"+local.getHostAddress());
 }
}

2. WRITE A PROGRAM TO OBTAIN THE INFORMATION ABOUT THE HOST, PORTNO. PROTOCOL, FILE AND PATH.
import java.lang.*;
import java.io.*;
import java.net.*;
class url
{
 public static void main(String args[]) throws Exception
 {
  URL obj=new URL("http://www.dreamtechpress.com:80/index.html");
  System.out.println("Host Name is:"+obj.getHost());
  System.out.println("Port NO is:"+obj.getPort());
  System.out.println("Protocol is:"+obj.getProtocol());
  System.out.println("File used is:"+obj.getFile());
  System.out.println("Path used is:"+obj.getPath());
  System.out.println("External form is :"+obj.toExternalForm());
 }
}

3. WRITE A PROGRAM TO ACCEPT A WEBSITE NAME AND RETURN ITS IP ADDRESS.
  

Import java.net.*;
public class acptipaddrs
{
 public static void main(String args[])
 {
  try
  {
   InetAddress ia=InetAddress.getByName("www.pavantutorials.in");
   System.out.println(ia);
  }catch(Exception ex)
  {
   System.out.println(ex);
  }
 }
}

4. WRITE A PROGRAM TO IMPLEMENT ECHO CLIENT AND ECHO SERVER
 ECHO CLIENT :  
import java.io.*; import java.net.*; public class echoclient { public static void main(String args[]) { try { Socket s=new Socket("192.168.55.19",9999); BufferedReader r=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter w=new PrintWriter(s.getOutputStream(),true); BufferedReader con=new BufferedReader(new InputStreamReader(System.in)); String line,rline; do { line=con.readLine(); if(line!=null) { w.println(line); } rline=r.readLine(); System.out.println("Echo from Server:"+rline); }while(!line.trim().equals("bye")); }catch(Exception err) { System.out.println("Error:"+err); } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- ECHO SERVER:
import java.io.*; import java.net.*; public class echoserver { public static void main(String args[]) { try { ServerSocket ser=new ServerSocket(9999); while(true) { Socket client=ser.accept(); BufferedReader r=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter w=new PrintWriter(client.getOutputStream(),true); System.out.println("Welcome to java echo server"); String line; do { line=r.readLine(); if(line!=null) w.println("Server"+line); System.out.println(line); }while(!line.trim().equals("bye")); if(line.equals("bye")) System.exit(0); client.close(); } }catch(Exception err) { System.out.println("Error"+err); } } }

5. WRITE A PROGRAM TO IMPLEMENT REMOTE CLIENT/SERVER.
 REMOTE CLIENT  
Import java.io.*; Import java.net.*; public class RemoteClient { public static void main(String args[]) { try { Socket s=new Socket("192.168.55.19",9999); BufferedReader r=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter w=new PrintWriter(s.getOutputSream(),true); BufferedReader con=new BufferedReader(new InputStreamReader(system.in)); String line,rline; do { line=con.readLine(); if(line!=null) { w.println(line); } rline=r.readLine(); System.out.println("Echo from Server:"+rline); }while(!line.trim().equals("bye")); }catch(Exception ex) { System.out.println("Error:"+err); } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- REMOTE SERVER
import java.io.*; import java.net.*; public class RemoteServer { public static void main(String args[]) { try { ServerSocket ser=new ServerSocket(9999); while(true) { Socket soc=ser.accept(); BufferedReader r=new BufferedReader(new InputStreamReader(soc.getInputStream())); PrintWriter w=new PrintWriter(soc.getOutputStream(),true); System.out.println("Welcome to Java RemoteServer"); String line; do { line=r.readLine(); if(line!=Null) w.println("Server : "+line); System.out.println(line); }while(!line.trim().equals("bye")); if(line.equals("bye")) System.exit(0); soc.close(); } }catch(Exception ex) { System.out.println("Exception is:"+ex); } } }

6. WRITE A PROGRAM TO IMPLEMENT TCP CLIENT SERVER.
 TCP CLIENT  
import java.io.*; import java.net.*; public class TcpClient { public static void main(String args[]) throws Exception { Socket s=new Socket("localhost",999); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedReader kb=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Start the chitchat type & press enter key"); String str,str1; while(!(str=kb.readLine()).equals("exit")) { dos.writeBytes(str+"\n"); str1=br.readLine(); System.out.println(str1); } dos.close(); br.close(); kb.close(); s.close(); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- TCP SERVER
import java.io.*; import java.net.*; public class TcpServer { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(999); System.out.println("Server ready for chatting"); Socket s=ss.accept(); System.out.println("Connection established"); PrintStream ps=new PrintStream(s.getOutputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedReader kb=new BufferedReader(new InputStreamReader(System.in)); while(true) { String str,str1; while((str=br.readLine())!=null) { System.out.println(str); str1=kb.readLine(); ps.println(str1); } ps.close(); br.close(); kb.close(); ss.close(); s.close(); System.exit(0); } } }

7. WRITE A PROGRAM TO IMPLEMENT FTP CLIENT SERVER.
 FTP CLIENT  
import java.io.*; import java.net.*; public class ftpclient { public static void main(String args[]) throws Exception { Socket s=new Socket("LocalHost",5555); DataInputStream dis=new DataInputStream(s.getInputStream()); DataInputStream dis1=new DataInputStream(System.in); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("\nEnter File Name(path):"); str=dis1.readLine(); dos.writeBytes(str); dos.writeBytes("\n"); FileOutputStream fo=new FileOutputStream("sss21.txt"); int str1; while((str1=dis.read())!=-1) fo.write(str1); System.out.println("\nFile received Successfully"); dis.close(); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- FTP SERVER
import java.io.*; import java.net.*; public class ftpserver { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(5555); Socket s=ss.accept(); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(s.getInputStream()); String s1; s1=dis.readLine(); FileInputStream fi=new FileInputStream(s1); int str; while((str=fi.read())!=-1) dos.writeBytes(" "+(char)+str); System.out.println("File has been sent successfully"); dos.close(); s.close(); } }

8. WRITE A PROGRAM TO IMPLEMENT SMTP.
 SMTP CLIENT  
import java.io.*; import java.net.*; class SmtpClient { public static void main(String args[]) throws Exception { Socket s=new Socket("127.0.0.1",9999); DataInputStream dis=new DataInputStream(s.getInputStream()); DataInputStream in=new DataInputStream(System.in); PrintStream ps=new PrintStream(s.getOutputStream()); ps.println("Ready"); System.out.println(dis.readLine()); String strFrom=in.readLine(); ps.println(strFrom); System.out.println(dis.readLine()); String strTo=in.readLine(); ps.println(strTo); System.out.println(dis.readLine()); String strSub=in.readLine(); ps.println(strSub); System.out.println(dis.readLine()); while(true) { String msg=in.readLine(); ps.println(msg); if(msg.equals("quit")) { System.out.println("Message is delevered to Server and Client Quits"); break; } } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- SMTP SERVER
import java.net.*; import java.io.*; class SmtpServer { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(9999); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream ps=new PrintStream(s.getOutputStream()); FileWriter f=new FileWriter("testmail.eml"); String tel=dis.readLine(); if(tel.equals("Ready")) System.out.println("Ready signal received from client"); ps.println("Enter from address:"); String from=dis.readLine(); f.write("From:"+from+"\n"); ps.println("Enter the To Address :"); String to=dis.readLine(); f.write("To:"+to+"\n"); ps.println("Enter the Subject :"); String sub=dis.readLine(); f.write("Subject:"+sub+"\n"); ps.println("Enter the Message :"); String msg=dis.readLine(); f.write("\n Message:"+msg+"\n"); f.close(); } }

9. WRITE A PROGRAM TO IMPLEMENT Domain Name System using UDP.
 DNS CLIENT  
import java.io.*; import java.net.*; import java.util.*; class DnsClient { public static void main(String args[])throws IOException { DatagramSocket client=new DatagramSocket(); InetAddress addr=InetAddress.getByName("127.0.0.1"); byte[]sendbyte=new byte[1024]; byte[]receivebyte=new byte[1024]; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter DOMAIN NAME OR IP address"); String str=in.readLine(); sendbyte=str.getBytes(); DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309); client.send(sender); DatagramPacket receiver= new DatagramPacket(receivebyte,receivebyte.length); client.receive(receiver); String s=new String(receiver.getData()); System.out.println("IP adddress or DOMAIN NAME :"+s.trim()); client.close(); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- DNS SERVER
import java.io.*; import java.net.*; import java.util.*; class DnsServer { public static void main(String args[])throws IOException { DatagramSocket server=new DatagramSocket(1309); byte[]sendbyte=new byte[1024]; byte[]receivebyte=new byte[1024]; DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length); server.receive(receiver); String str=new String(receiver.getData()); String s=str.trim(); System.out.println(s); InetAddress addr=receiver.getAddress(); int port=receiver.getPort(); String ip[]={"165.165.80.80","165.165.79.1"}; String name[]={"www.aptitudesource.com","www.Sharifguys.com"}; for(int i=0;i < ip.length;i++) { if(s.equals(ip[i])) { sendbyte=name[i].getBytes(); DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr, port); server.send(sender); }else if(s.equals(name[i])) { sendbyte=ip[i].getBytes(); DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr, port); server.send(sender); server.close(); } } } }

10. WRITE A PROGRAM TO IMPLEMENT SIMPLE CALCULATOR ON A REMOTE HOST AND INVOKE OPERATIONS FROM A CLIENT USING RMI.
 INTERFACE  
import java.rmi.Remote; import java.rmi.RemoteException; public interface Cal extends Remote { public long add(long a, long b) throws RemoteException; public long sub(long a, long b) throws RemoteException; public long mul(long a, long b) throws RemoteException; public long div(long a, long b) throws RemoteException; } --------------------------------------------------------------------------------------------------------------------------------------------------------------- INTERFACE IMPLEMENTATION
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CalcImpl extends UnicastRemoteObject implements Cal { public CalcImpl() throws RemoteException { super(); } public long add(long a, long b) throws RemoteException { return a+b; } public long sub(long a, long b) throws RemoteException { return a-b; } public long mul(long a, long b) throws RemoteException { return a*b; } public long div(long a, long b) throws RemoteException { return a/b; } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- CLIENT
import java.io.*; import java.rmi.Naming; public class CalcClient { public static void main(String args[]) { try { Cal obj=(Cal)Naming.lookup("//127.0.0.1:1099/Calobject"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter any two numbers:"); int a=Integer.parseInt(br.readLine()); int b=Integer.parseInt(br.readLine()); System.out.println("Addition is :"+obj.add(a,b)); System.out.println("Subtraction is :"+obj.sub(a,b)); System.out.println("Muultiplication is :"+obj.mul(a,b)); System.out.println("Division is :"+obj.div(a,b)); }catch(Exception e) { System.out.println(e); } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- SERVER
import java.rmi.Naming; public class CalcServer { CalcServer() { try { Cal obj=new CalcImpl(); Naming.rebind("rmi://127.0.0.1:1099/Calobject",obj); }catch(Exception e) { e.printStackTrace(); } } public static void main(String args[]) { new CalcServer(); } }

Comments

  1. please tell which mobiles are best below 10000

    ReplyDelete
  2. plz which mobiles best for students below 10000 we can publish in your blog it is useful to all students as soon as possible you can keep in your blog we are waiting

    ReplyDelete

Post a Comment

Popular posts from this blog

DBMS LAB : PL/SQL Programs

DBMS LAB : Cycle-II