SHARE
TWEET

Untitled

a guest Mar 20th, 2017 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ping -c 3 www.vit.ac.in
  2. ping -q www.vit.ac.in
  3. ping -s 512 -c 5 www.vit.ac.in
  4. ping -W 5 -c 5 www.vit.ac.in
  5. ping -V
  6.  
  7. IFCONFIG: MAC,IP,INTERFACES,NETWORK SETTING
  8. ifconfig
  9. ifconfig -a
  10. ifconfig -v lo
  11.  
  12. NETSTAT:
  13. listening port: -l
  14. tcp port: -t
  15. udp port:-u
  16. listening conn:-l
  17. statistics of all proto:-s
  18. kernel ip routing:-r
  19. kernel interface:-i
  20. service with pid:
  21.  
  22. nslookup www.google.com
  23. arp -v
  24.  
  25.  
  26. UDP CHAT CLIENT
  27.  
  28. import java.io.*;
  29. import java.net.*;
  30.  
  31. public class UDPChatClient {
  32.     public static void main(String args[]) throws Exception
  33.     {
  34.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  35.        
  36.         InetAddress IPAddress = InetAddress.getByName("localhost");
  37.        
  38.         String msg = "";
  39.         while(true)
  40.         {
  41.             byte[] sendData = new byte[1024];
  42.             byte[] receiveData = new byte[1024];
  43.             DatagramSocket clientSocket = new DatagramSocket();
  44.             System.out.printf("Send a Message: ");
  45.             msg = inFromUser.readLine();
  46.             if (msg.equals("bye"))
  47.             {
  48.                 System.out.printf("Exiting Chat!");
  49.                 clientSocket.close();
  50.                 break;
  51.             }
  52.             sendData = msg.getBytes();
  53.  
  54.             DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
  55.             clientSocket.send(sendPacket);
  56.  
  57.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  58.             clientSocket.receive(receivePacket);
  59.  
  60.             String reply = new String(receivePacket.getData());
  61.             System.out.println("Reply: " + reply);
  62.             clientSocket.close();
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. UDP CHAT SERVER
  70.  
  71. import java.io.*;
  72. import java.net.*;
  73.  
  74. public class UDPChatServer {
  75.     public static void main(String args[]) throws Exception
  76.     {
  77.         DatagramSocket serverSocket = new DatagramSocket(6789);
  78.  
  79.        
  80.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  81.         String reply;
  82.         while(true)
  83.         {
  84.             byte[] receiveData = new byte[1024];
  85.             byte[] sendData = new byte[1024];
  86.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  87.             serverSocket.receive(receivePacket);
  88.             String msg = new String(receivePacket.getData());
  89.             System.out.println("Message Received: " + msg);
  90.  
  91.             InetAddress IPAddress = receivePacket.getAddress();
  92.             int port = receivePacket.getPort();
  93.                            
  94.             System.out.printf("Enter reply: ");
  95.             reply = inFromUser.readLine();
  96.             if (reply.equals("bye"))
  97.             {
  98.                 System.out.printf("Exiting Chat!");
  99.                 serverSocket.close();
  100.                 break;
  101.             }
  102.             sendData = reply.getBytes();
  103.             DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
  104.             serverSocket.send(sendPacket);
  105.         }
  106.        
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. TCP CHAT Server
  114.  
  115. import java.io.*;
  116. import java.io.BufferedReader;
  117. import java.io.InputStream;
  118. import java.io.InputStreamReader;
  119. import java.io.OutputStream;
  120. import java.io.PrintWriter;
  121. import java.net.*;
  122. public class GossipServer
  123. {
  124.   public static void main(String[] args) throws Exception
  125.   {
  126.       ServerSocket sersock = new ServerSocket(3000);
  127.       Socket sock = sersock.accept( );                          
  128.                               // reading from keyboard (keyRead object)
  129.       BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  130.                       // sending to client (pwrite object)
  131.       OutputStream ostream = sock.getOutputStream();
  132.       PrintWriter pwrite = new PrintWriter(ostream, true);
  133.  
  134.                               // receiving from server ( receiveRead  object)
  135.       InputStream istream = sock.getInputStream();
  136.       BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  137.  
  138.       String receiveMessage, sendMessage;              
  139.       while(true)
  140.       {
  141.         if((receiveMessage = receiveRead.readLine()) != null)  
  142.         {
  143.            System.out.println(receiveMessage);        
  144.         }        
  145.         sendMessage = keyRead.readLine();
  146.         pwrite.println(sendMessage);            
  147.         pwrite.flush();
  148.       }              
  149.     }                    
  150. }  
  151.  
  152.  
  153. TCP CHAT CLIENT
  154.  
  155. import java.io.*;
  156. import java.net.*;
  157. public class GossipClient
  158. {
  159.   public static void main(String[] args) throws Exception
  160.   {
  161.      Socket sock = new Socket("127.0.0.1", 3000);
  162.                                // reading from keyboard (keyRead object)
  163.      BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  164.                               // sending to client (pwrite object)
  165.      OutputStream ostream = sock.getOutputStream();
  166.      PrintWriter pwrite = new PrintWriter(ostream, true);
  167.  
  168.                               // receiving from server ( receiveRead  object)
  169.      InputStream istream = sock.getInputStream();
  170.      BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  171.  
  172.      System.out.println("Start the chitchat, type and press Enter key");
  173.  
  174.      String receiveMessage, sendMessage;              
  175.      while(true)
  176.      {
  177.         sendMessage = keyRead.readLine();  // keyboard reading
  178.         pwrite.println(sendMessage);       // sending to server
  179.         pwrite.flush();                    // flush the data
  180.         if((receiveMessage = receiveRead.readLine()) != null) //receive from server
  181.         {
  182.             System.out.println(receiveMessage); // displaying at DOS prompt
  183.         }        
  184.       }              
  185.     }                    
  186. }  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. Q2 Write a Java program to run the basic networking commands.
  193.  
  194. import java.net.*;
  195. import java.util.*;
  196. import java.io.*;
  197. public class command
  198. {
  199.         public static void main(String args[])
  200.         throws Exception
  201.         {
  202.                 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  203.                 System.out.println("enter command:");
  204.                 String s=br.readLine();
  205.                 Process p=Runtime.getRuntime().exec(s);
  206.                 p.waitFor();
  207.                 BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
  208.                 String line=reader.readLine();
  209.                 while(line!=null)
  210.                 {
  211.                         System.out.println(line);
  212.                         line=reader.readLine();0
  213.                 }
  214.                 System.out.println("done!");
  215.  
  216. }
  217. }
  218.  
  219.  
  220. Q3 Write a program to display the name of the computer and its IP address that you are currently working on
  221.  
  222. import java.net.*;
  223. import java.io.*;
  224. public class cm
  225. {
  226. public static void main(String args[])
  227. {
  228. try
  229. {
  230. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  231. System.out.println("Enter the url which you want to find ip address and host name");
  232. String str=br.readLine();
  233. InetAddress add=InetAddress.getByName(str);
  234. System.out.println("Local Host Information");
  235. System.out.println("Host Name:"+add.getHostName());
  236. System.out.println("IP address:"+add.getHostAddress());
  237. }
  238. catch(Exception e)
  239. {
  240. System.out.println(e);
  241. }
  242. }
  243.  
  244.  
  245.  
  246. Q4 Write a program to print the IP address of  “www.google.com” all IP addresses of  “www.microsoft.com”.
  247.  
  248.  
  249.  
  250.  
  251. import java.net.*;
  252. import java.io.*;
  253. public class ip
  254. {
  255. public static void main(String args[])
  256. throws UnknownHostException {
  257.         System.out.println(InetAddress.getByName("www.google.com"));
  258.         InetAddress[] inetAddresses=InetAddress.getAllByName("www.microsoft.com");
  259.         for(InetAddress ipAddress:inetAddresses)
  260.         {
  261.                 System.out.println(ipAddress);
  262.         }
  263. }
  264. }
  265.  
  266.  
  267.  
  268. Q5 Write a program to print all Network Interfaces of “localhost”.
  269.  
  270. import java.net.*;
  271. import java.util.*;
  272. import java.io.*;
  273. public class ni
  274. {
  275. public static void main(String args[]) throws UnknownHostException,SocketException
  276. {
  277.  
  278.     Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  279. if(interfaces==null)
  280. {
  281. System.out.println("No network interfaces found");
  282. }
  283. else
  284. {
  285. for(NetworkInterface netIf:Collections.list(interfaces))
  286. {
  287. System.out.println("Display Name:" + netIf.getDisplayName());
  288. System.out.println("Name : " + netIf.getName());
  289. System.out.println();
  290. }
  291. }
  292. }
  293. }
  294.  
  295.  
  296. Q6 Implement the simple version of “nslookup” utility
  297.  
  298. import java.net.*;
  299. import java.io.*;
  300. import java.util.*;
  301. public class nslook
  302. {
  303. public static void main(String args[]) throws Exception
  304. {
  305.     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  306.     System.out.println("Enter the hostname");
  307.     String cmd=br.readLine();
  308.     Process p=Runtime.getRuntime().exec("nslookup "+cmd);
  309.     p.waitFor();
  310.     BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
  311.     String line=reader.readLine();
  312.     while(line!=null)
  313.     {
  314.         System.out.println(line);
  315.         line=reader.readLine();
  316.     }
  317.     System.out.println("Done");
  318. }
  319. }
  320.  
  321.  
  322. Q7 Write a program to download the contents associated with a HTTP URL and save it in a file.
  323.  
  324.  
  325.  
  326. import java.io.*;
  327.     import java.net.*;
  328.     public class GetJavaUrl
  329.     {
  330.       public static void main(String[] args)
  331.          {
  332.            URL u;
  333.            InputStream is=null;
  334.            DataInputStream dis;
  335.            String s;
  336.            try
  337.            {
  338.              u=new URL("ftp://10.30.2.53");
  339.              is=u.openStream();
  340.              dis = new DataInputStream(new BufferedInputStream(is));
  341.              while((s=dis.readLine())!=null)
  342.               {
  343.                 System.out.println(s);
  344.               }
  345.             }
  346.            catch(MalformedURLException mue)
  347.             {
  348.               System.out.println("404: Error NOT FOUND");
  349.               mue.printStackTrace();
  350.               System.exit(1);
  351.             }
  352.            catch(IOException ioe)
  353.             {
  354.               System.out.println("404: Error NOT FOUND");
  355.               ioe.printStackTrace();
  356.               System.exit(1);
  357.             }
  358.           finally
  359.             {
  360.                try
  361.                    {
  362.                      is.close();
  363.                    }
  364.                catch(IOException ioe)
  365.                    {
  366.                    }
  367.             }
  368.         }
  369.    
  370. }
  371.  
  372.  
  373.  
  374.  
  375. cyclesheet2
  376.  
  377.  
  378. Q1 Write a program to list all ports hosting a TCP server in a specified host and identify available servers in well-known ports?
  379.  
  380.  
  381. import java.net.Socket;
  382. import java.net.*;
  383.  
  384. public class A {
  385. public static void main(String[] args) throws Exception{
  386.     try {
  387.         for(int i=0;i<1024;i++)
  388.         {
  389.             Socket s=new Socket("localhost",i);
  390.             System.out.println(i);
  391.         }
  392.     }
  393.     catch(Exception e)
  394.     {
  395.         System.out.println(e);
  396.     }
  397. }
  398. }
  399.  
  400.  
  401.  
  402. Q2 Implement echo server and client in java using TCP sockets.
  403.  
  404. server
  405.  
  406. import java.io.*;
  407.  
  408. import java.net.*;
  409.  
  410.  
  411.  
  412. public class EchoServer
  413.  
  414. {
  415.  
  416. public static void main(String args[]) throws Exception
  417.  
  418. {
  419.  
  420. try
  421.  
  422. {
  423.  
  424. int Port;
  425.  
  426. BufferedReader Buf =new BufferedReader(new
  427.  
  428. InputStreamReader(System.in));
  429.  
  430. System.out.print(" Enter the Port Address : " );
  431.  
  432. Port=Integer.parseInt(Buf.readLine());
  433.  
  434. ServerSocket sok =new ServerSocket(Port);
  435.  
  436. System.out.println(" Server is Ready To Receive a Message. ");
  437.  
  438. System.out.println(" Waiting ..... ");
  439.  
  440. Socket so=sok.accept();
  441.  
  442. if(so.isConnected()==true)
  443.  
  444.             System.out.println(" Client Socket is Connected Succecfully. ");
  445.  
  446. InputStream in=so.getInputStream();
  447.  
  448. OutputStream ou=so.getOutputStream();
  449.  
  450. PrintWriter pr=new PrintWriter(ou);
  451.  
  452. BufferedReader buf=new BufferedReader(new
  453.  
  454. InputStreamReader(in));
  455.  
  456. String str=buf.readLine();
  457.  
  458. System.out.println(" Message Received From Client : " + str);
  459.  
  460. System.out.println(" This Message is Forwarded To Client. ");
  461.  
  462. pr.println(str);
  463.  
  464. pr.flush();
  465.  
  466. }
  467.  
  468.  catch(Exception e)
  469.  
  470.   {
  471.  
  472.   System.out.println(" Error : " + e.getMessage());
  473.  
  474.   }
  475.  
  476. }
  477.  
  478. }
  479.  
  480.  
  481.  
  482. client
  483.  
  484. import java.io.*;
  485.  
  486. import java.net.*;
  487.  
  488.  
  489.  
  490. public class EchoClient
  491.  
  492. {
  493.  
  494. public static void main(String args[]) throws Exception
  495.  
  496. {
  497.  
  498. try {
  499.  
  500. int Port;
  501.  
  502. BufferedReader Buf =new BufferedReader(new
  503.  
  504. InputStreamReader(System.in));
  505.  
  506. System.out.print(" Enter the Port Address : " );
  507.  
  508. Port=Integer.parseInt(Buf.readLine());
  509.  
  510. Socket sok=new Socket("localhost",Port);
  511.  
  512. if(sok.isConnected()==true)
  513.  
  514.             System.out.println(" Server Socket is Connected Succecfully. ");
  515.  
  516. InputStream in=sok.getInputStream();
  517.  
  518. OutputStream ou=sok.getOutputStream();
  519.  
  520. PrintWriter pr=new PrintWriter(ou);
  521.  
  522. BufferedReader buf1=new BufferedReader(new
  523.  
  524. InputStreamReader(System.in));
  525.  
  526.  
  527. BufferedReader buf2=new BufferedReader(new
  528.  
  529. InputStreamReader(in));
  530.  
  531. String str1,str2;
  532.  
  533. System.out.print(" Enter the Message : ");
  534.  
  535. str1=buf1.readLine();
  536.  
  537. pr.println(str1);
  538.  
  539. pr.flush();
  540.  
  541. System.out.println(" Message Send Successfully. ");
  542.  
  543. str2=buf2.readLine();
  544.  
  545. System.out.println(" Message From Server : " + str2);
  546.  
  547.      }
  548.  
  549.   catch(Exception e)
  550.  
  551.   {
  552.  
  553.    System.out.println(" Error : " + e.getMessage());
  554.  
  555.   }
  556.  
  557. }
  558.  
  559. }
  560.  
  561.  
  562. Q3 Implement date server and client in java using TCP sockets
  563.  
  564.  
  565. client
  566.  
  567. import java.io.*;
  568.  
  569. import java.net.*;
  570.  
  571.  
  572.  
  573. class DateClient
  574.  
  575. {
  576.  
  577.     public static void main(String args[]) throws Exception
  578.  
  579.     {
  580.  
  581.         Socket soc=new Socket(InetAddress.getLocalHost(),5217);        
  582.  
  583.         BufferedReader in=new BufferedReader(new InputStreamReader( soc.getInputStream() ) );
  584.  
  585.    System.out.println(in.readLine());
  586.  
  587.     }    
  588.  
  589. }
  590.  
  591.  
  592.  
  593. server
  594.  
  595. import java.net.*;
  596.  
  597. import java.io.*;
  598.  
  599. import java.util.*;
  600.  
  601.  
  602.  
  603. class DateServer
  604.  
  605. {
  606.  
  607.     public static void main(String args[]) throws Exception
  608.  
  609.     {
  610.  
  611.         ServerSocket s=new ServerSocket(5217);
  612.         while(true)
  613.  
  614.         {
  615.  
  616.             System.out.println("Waiting For Connection ...");
  617.  
  618.             Socket soc=s.accept();
  619.  
  620.             DataOutputStream out=new DataOutputStream(soc.getOutputStream());
  621.  
  622.             out.writeBytes("Server Date" + (new Date()).toString() + "\n");
  623.  
  624.             out.close();
  625.  
  626.             soc.close();
  627.  
  628.         }
  629.  
  630.        
  631.  
  632.     }
  633.  
  634. }
  635.  
  636.  
  637. Q4,Q6
  638. Implement a chat server and client in java using TCP sockets.
  639. Write a program to implement a simple message transfer from client to server process using TCP/IP.
  640.  
  641.  
  642. SERVER
  643.  
  644. import java.io.*;
  645. import java.io.BufferedReader;
  646. import java.io.InputStream;
  647. import java.io.InputStreamReader;
  648. import java.io.OutputStream;
  649. import java.io.PrintWriter;
  650. import java.net.*;
  651. public class GossipServer
  652. {
  653.   public static void main(String[] args) throws Exception
  654.   {
  655.       ServerSocket sersock = new ServerSocket(3000);
  656.       System.out.println("Server  ready for chatting");
  657.       Socket sock = sersock.accept( );                          
  658.                               // reading from keyboard (keyRead object)
  659.       BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  660.                       // sending to client (pwrite object)
  661.       OutputStream ostream = sock.getOutputStream();
  662.       PrintWriter pwrite = new PrintWriter(ostream, true);
  663.  
  664.                               // receiving from server ( receiveRead  object)
  665.       InputStream istream = sock.getInputStream();
  666.       BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  667.  
  668.       String receiveMessage, sendMessage;              
  669.       while(true)
  670.       {
  671.         if((receiveMessage = receiveRead.readLine()) != null)  
  672.         {
  673.            System.out.println(receiveMessage);        
  674.         }        
  675.         sendMessage = keyRead.readLine();
  676.         pwrite.println(sendMessage);            
  677.         pwrite.flush();
  678.       }              
  679.     }                    
  680. }  
  681.  
  682.  
  683. CLINET
  684.  
  685. import java.io.*;
  686. import java.net.*;
  687. public class GossipClient
  688. {
  689.   public static void main(String[] args) throws Exception
  690.   {
  691.      Socket sock = new Socket("127.0.0.1", 3000);
  692.                                // reading from keyboard (keyRead object)
  693.      BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  694.                               // sending to client (pwrite object)
  695.      OutputStream ostream = sock.getOutputStream();
  696.      PrintWriter pwrite = new PrintWriter(ostream, true);
  697.  
  698.                               // receiving from server ( receiveRead  object)
  699.      InputStream istream = sock.getInputStream();
  700.      BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  701.  
  702.      System.out.println("Start the chitchat, type and press Enter key");
  703.  
  704.      String receiveMessage, sendMessage;              
  705.      while(true)
  706.      {
  707.         sendMessage = keyRead.readLine();  // keyboard reading
  708.         pwrite.println(sendMessage);       // sending to server
  709.         pwrite.flush();                    // flush the data
  710.         if((receiveMessage = receiveRead.readLine()) != null) //receive from server
  711.         {
  712.             System.out.println(receiveMessage); // displaying at DOS prompt
  713.         }        
  714.       }              
  715.     }                    
  716. }  
  717.  
  718.  
  719.  
  720. Q7 Implement a TCP based server program to authenticate the client’s User Name and Password. The validity of the client must be sent as the reply message to the client and display it on the standard output.
  721.  
  722. CLIENT
  723.  
  724. import java.io.*;
  725. import java.net.*;
  726. class TCPClientAuth {
  727.  
  728.     public static void main(String argv[]) throws Exception
  729.     {
  730.         String username;
  731.         String password;
  732.     String auth;
  733.  
  734.         BufferedReader inFromUser =
  735.           new BufferedReader(new InputStreamReader(System.in));
  736.  
  737.         Socket clientSocket = new Socket("localhost", 6789);
  738.  
  739.         DataOutputStream outToServer =
  740.           new DataOutputStream(clientSocket.getOutputStream());
  741.  
  742.                 BufferedReader inFromServer =
  743.           new BufferedReader(new
  744.           InputStreamReader(clientSocket.getInputStream()));
  745.    
  746.     System.out.print("Username :");
  747.         username = inFromUser.readLine();
  748.  
  749.     System.out.print("Password :");
  750.     password = inFromUser.readLine();
  751.  
  752.  
  753.         outToServer.writeBytes(username + '\n' + password + '\n' );
  754.  
  755.         auth = inFromServer.readLine();
  756.  
  757.         System.out.println("FROM SERVER: " + auth);
  758.  
  759.         clientSocket.close();
  760.                    
  761.     }
  762. }
  763.  
  764. SERVER
  765.  
  766.  
  767. import java.io.*;
  768. import java.net.*;
  769.  
  770. class TCPServerAuth {
  771.  
  772.   public static void main(String argv[]) throws Exception
  773.     {
  774.       String username;
  775.       String password;
  776.       String auth;
  777.  
  778.       ServerSocket welcomeSocket = new ServerSocket(6789);
  779.  
  780.       while(true) {
  781.  
  782.             Socket connectionSocket = welcomeSocket.accept();
  783.  
  784.            BufferedReader inFromClient =
  785.               new BufferedReader(new
  786.               InputStreamReader(connectionSocket.getInputStream()));
  787.  
  788.            
  789.  
  790.            DataOutputStream  outToClient =
  791.              new DataOutputStream(connectionSocket.getOutputStream());
  792.  
  793.            username = inFromClient.readLine();
  794.        password = inFromClient.readLine();
  795.  
  796.  
  797.            if (username.equals("Admin")){
  798.         if (password.equals("root")){
  799.             auth = "Login Successful!";
  800.         }else{
  801.             auth = "Wrong Password";
  802.         }
  803.        }else{
  804.         auth = "Wrong Username";
  805.        }
  806.  
  807.            outToClient.writeBytes(auth);
  808.        connectionSocket.close();
  809.         }
  810.     }
  811. }
  812.  
  813. ************************************************
  814.  
  815. CYCLESHEET 2
  816.  
  817. Q8.Find the physical address of a host when its logical address is known (ARP protocol) using TCP/IP.
  818.  
  819. CLIENT:
  820. import java.io.*;
  821. import java.util.*;
  822. import java.net.*;
  823.  
  824. public class q8client
  825. {
  826. public static void main(String[] args)
  827. {
  828. try
  829. {
  830. Socket s = new Socket("localhost",5555);
  831. BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
  832. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  833. DataInputStream din = new DataInputStream(s.getInputStream());
  834. System.out.println("Enter the logical address: ");
  835. String str = b.readLine();
  836. dout.writeBytes(str + "\n");
  837. String str1 = din.readLine();
  838. System.out.println("Physical Address of str: " + str1);
  839. s.close();
  840. }
  841. catch (Exception e)
  842. {
  843. }
  844. }}
  845.  
  846. SERVER:
  847.  
  848. import java.io.*;
  849. import java.util.*;
  850. import java.net.*;
  851.  
  852. public class q8server
  853. {
  854. public static void main(String[] args)
  855. {
  856. try
  857. {
  858. ServerSocket ss = new ServerSocket(5555);
  859. Socket s = ss.accept();
  860. BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
  861. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  862. DataInputStream din = new DataInputStream(s.getInputStream());
  863. String str = din.readLine();
  864. String logical[] = {"172.16.27.34","195.24.54.204"};
  865. String physical[] = {"8E:AE:08:AA","0A:78:6C:AD"};
  866. for(int i=0;i<logical.length;i++)
  867. {
  868. if (str.equals(logical[i]))
  869. {
  870. dout.writeBytes(physical[i] + "\n");
  871. break;
  872. }
  873. }
  874. s.close();
  875. }
  876. catch (Exception e)
  877. {
  878. }
  879. }
  880. }
  881.  
  882.  
  883. OUTPUT:
  884.  
  885.  
  886. =====================================================
  887.  
  888.  
  889.  
  890.  
  891.  
  892. Q9.Find the logical address of a host when its physical address is known (RARP protocol) using TCP/IP.
  893.  
  894. CLIENT:
  895. import java.io.*;
  896. import java.net.*;
  897. class TCPLogClient {
  898.  
  899.     public static void main(String argv[]) throws Exception
  900.     {
  901.         String logicAdd;
  902.         String phyAdd;
  903.  
  904.         BufferedReader inFromUser =
  905.           new BufferedReader(new InputStreamReader(System.in));
  906.  
  907.         Socket clientSocket = new Socket("localhost", 6789);
  908.  
  909.         DataOutputStream outToServer =
  910.           new DataOutputStream(clientSocket.getOutputStream());
  911.  
  912.                 BufferedReader inFromServer =
  913.           new BufferedReader(new
  914.           InputStreamReader(clientSocket.getInputStream()));
  915.    
  916.     System.out.println("Enter Physical address of host");
  917.         phyAdd = inFromUser.readLine();
  918.  
  919.         outToServer.writeBytes(phyAdd + '\n');
  920.  
  921.         logicAdd = inFromServer.readLine();
  922.  
  923.         System.out.println("FROM SERVER: Logical Address " + logicAdd);
  924.  
  925.         clientSocket.close();
  926.                    
  927.     }
  928. }
  929.  
  930. SERVER:
  931. import java.io.*;
  932. import java.net.*;
  933.  
  934. class TCPLogServer {
  935.  
  936.   public static void main(String argv[]) throws Exception
  937.     {
  938.       String phyAdd;
  939.        
  940.  
  941.       ServerSocket welcomeSocket = new ServerSocket(6789);
  942.  
  943.       while(true) {
  944.  
  945.             Socket connectionSocket = welcomeSocket.accept();
  946.  
  947.            BufferedReader inFromClient =
  948.               new BufferedReader(new
  949.               InputStreamReader(connectionSocket.getInputStream()));
  950.  
  951.  
  952.           String logical[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
  953.       String physical[] = {"8E:AE:08:AA","0A:78:6C:AD", "d4:6d:50:84:db:c8"};
  954.  
  955.            DataOutputStream  outToClient =
  956.              new DataOutputStream(connectionSocket.getOutputStream());
  957.  
  958.            phyAdd = inFromClient.readLine();
  959.     for(int i = 0; i < physical.length; i++)
  960.     {
  961.         if (phyAdd.equals(physical[i]))
  962.         {
  963.             outToClient.writeBytes(logical[i] + "\n");
  964.             connectionSocket.close();
  965.         }
  966.     }
  967.  
  968.            
  969.         }
  970.     }
  971. }
  972.  
  973. =======================================
  974.  
  975.  
  976. 10.Implement Domain Name System (DNS) using TCP/IP
  977.  
  978. SERVER:
  979. import java.io.*;
  980. import java.net.*;
  981. class TCPLoServer {
  982.  public static void main(String argv[]) throws Exception
  983.     {
  984.       String D;
  985.        ServerSocket welcomeSocket = new ServerSocket(6789);
  986.     while(true) {
  987.    Socket connectionSocket = welcomeSocket.accept();
  988. BufferedReader inFromClient =
  989.               new BufferedReader(new
  990.               InputStreamReader(connectionSocket.getInputStream()));
  991. String hostaddr[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
  992.       String DNS[] = {"www.google.com","www.facebook.com", "www.vit.ac.in"};
  993.            DataOutputStream  outToClient =
  994.              new DataOutputStream(connectionSocket.getOutputStream());
  995.    D = inFromClient.readLine();
  996. for(int i = 0; i < hostaddr.length; i++)
  997.     {
  998.         if (D.equals(hostaddr[i]))
  999.         {
  1000.             outToClient.writeBytes(DNS[i] + "\n");
  1001.             connectionSocket.close();
  1002.         }
  1003.     }
  1004.  
  1005.            
  1006.         }
  1007.     }
  1008. }
  1009.  
  1010.  
  1011.  
  1012. CLIENT:
  1013. import java.io.*;
  1014. import java.net.*;
  1015. class TCPLoClient {
  1016.  
  1017.     public static void main(String argv[]) throws Exception
  1018.     {
  1019.         String hostaddr;
  1020.         String DNS;
  1021.  
  1022.         BufferedReader inFromUser =
  1023.           new BufferedReader(new InputStreamReader(System.in));
  1024.  
  1025.         Socket clientSocket = new Socket("localhost", 6789);
  1026.  
  1027.         DataOutputStream outToServer =
  1028.           new DataOutputStream(clientSocket.getOutputStream());
  1029.  
  1030.                 BufferedReader inFromServer =
  1031.           new BufferedReader(new
  1032.           InputStreamReader(clientSocket.getInputStream()));
  1033.      
  1034.     System.out.println("Enter IP address of host");
  1035.         DNS = inFromUser.readLine();
  1036.  
  1037.         outToServer.writeBytes(DNS + '\n');
  1038.  
  1039.        hostaddr = inFromServer.readLine();
  1040.  
  1041.         System.out.println("FROM SERVER: Logical Address " + hostaddr);
  1042.  
  1043.         clientSocket.close();
  1044.                    
  1045.     }
  1046. }
  1047.  
  1048.  
  1049.  
  1050. *************************************************
  1051.  
  1052. 11.Implement a TCP/IP based Server program in Java to compute the factorial of a number (can contain arbitrary number of digits). Write a client program to test the working of the same.
  1053.  
  1054. CLIENT:
  1055. import java.io.*;
  1056. import java.net.*;
  1057. class client {
  1058.  public static void main(String argv[]) throws Exception
  1059.  {
  1060.   String sentence;
  1061.   String modifiedSentence;
  1062.   BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  1063.   Socket clientSocket = new Socket("localhost", 2222);
  1064.   DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  1065.   BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  1066.   System.out.println("Enter a number - ");
  1067.   sentence = inFromUser.readLine();
  1068.   outToServer.writeBytes(sentence + '\n');
  1069.   modifiedSentence = inFromServer.readLine();
  1070.   System.out.println ("Factorial: " + modifiedSentence );
  1071.   clientSocket.close();
  1072.  }
  1073. }
  1074. SREVER:
  1075.  
  1076. import java.io.*;
  1077. import java.net.*;
  1078. class serv {
  1079.  public static void main(String argv[]) throws Exception
  1080.  {
  1081.   String clientSentence;
  1082.   String capitalizedSentence;
  1083.   ServerSocket welcomeSocket = new ServerSocket(2222);
  1084.    Socket connectionSocket = welcomeSocket.accept();
  1085.    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  1086.    DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream());
  1087.    clientSentence = inFromClient.readLine();
  1088.    int num = Integer.parseInt(clientSentence);
  1089.    int fact=1;
  1090.    for(int i=1;i<=num;i++)
  1091.     fact=fact*i;
  1092.    System.out.println("Factorial : "+fact);
  1093.    capitalizedSentence = String.valueOf(fact);
  1094.    //capitalizedSentence = clientSentence.toUpperCase() + '\n';
  1095.    outToClient.writeBytes(capitalizedSentence);
  1096.  }
  1097. }
  1098.  
  1099. ******************************************************
  1100.  
  1101. Ques12.Find the class of the IP address using TCP/IP.
  1102. import java.io.*;
  1103. import java.net.*;
  1104.  
  1105. class TCPIpclassServer {
  1106.  
  1107.   public static void main(String argv[]) throws Exception
  1108.     {
  1109.       String ip;
  1110.     int fbyte;
  1111.     String ipclass = "Invalid";
  1112.        
  1113.  
  1114.       ServerSocket welcomeSocket = new ServerSocket(6789);
  1115.  
  1116.       while(true) {
  1117.  
  1118.             Socket connectionSocket = welcomeSocket.accept();
  1119.  
  1120.            BufferedReader inFromClient =
  1121.               new BufferedReader(new
  1122.               InputStreamReader(connectionSocket.getInputStream()));
  1123.  
  1124.  
  1125.            DataOutputStream  outToClient =
  1126.              new DataOutputStream(connectionSocket.getOutputStream());
  1127.  
  1128.            ip = inFromClient.readLine();
  1129.        int index;
  1130.            index = ip.indexOf(".");
  1131.            ip = ip.substring(0, index);
  1132.      
  1133.  
  1134.        fbyte = Integer.parseInt(ip);
  1135.     if (fbyte >= 0 && fbyte <= 127)
  1136.         ipclass = "Class A";
  1137.     else if (fbyte >= 128 && fbyte <= 191)
  1138.         ipclass = "Class B";
  1139.     else if (fbyte >= 192 && fbyte <= 223)
  1140.         ipclass = "Class C";
  1141.     else if (fbyte >= 224 && fbyte <= 239)
  1142.         ipclass = "Class D";
  1143.     else if (fbyte >= 240 && fbyte <= 255)
  1144.         ipclass = "Class E";
  1145.  
  1146.     outToClient.writeBytes(ipclass + "\n");
  1147.     connectionSocket.close();
  1148.      
  1149.         }
  1150.     }
  1151. }
  1152.  
  1153. //client
  1154.  
  1155. import java.io.*;
  1156. import java.net.*;
  1157. class TCPIpclassClient {
  1158.  
  1159.     public static void main(String argv[]) throws Exception
  1160.     {
  1161.         String ip;
  1162.     String ipclass;
  1163.  
  1164.         BufferedReader inFromUser =
  1165.           new BufferedReader(new InputStreamReader(System.in));
  1166.  
  1167.         Socket clientSocket = new Socket("localhost", 6789);
  1168.  
  1169.         DataOutputStream outToServer =
  1170.           new DataOutputStream(clientSocket.getOutputStream());
  1171.  
  1172.                 BufferedReader inFromServer =
  1173.           new BufferedReader(new
  1174.           InputStreamReader(clientSocket.getInputStream()));
  1175.    
  1176.     System.out.println("Enter an IP address: ");
  1177.         ip = inFromUser.readLine();
  1178.  
  1179.         outToServer.writeBytes(ip + '\n');
  1180.  
  1181.         ipclass = inFromServer.readLine();
  1182.  
  1183.         System.out.println("FROM SERVER: Class for IP: "+ ip + " is : " + ipclass);
  1184.  
  1185.         clientSocket.close();
  1186.                    
  1187.     }
  1188. }
  1189.  
  1190. *************************************************
  1191.  
  1192.  
  1193. CYCLESHEET 3
  1194.  
  1195.  
  1196. 1.Implement echo server and client in java using UDP sockets.
  1197.  
  1198. import java.io.*;
  1199. import java.net.*;
  1200.  
  1201. public class EServer
  1202. {
  1203.  public static void main(String[] args) throws IOException
  1204.  {
  1205.  
  1206.   ServerSocket S = new ServerSocket(3000);
  1207.  
  1208.   while(true)
  1209.   {
  1210.    Socket Client = S.accept();
  1211.  
  1212.    InputStream in = Client.getInputStream();
  1213.  
  1214.    DataInputStream Dis = new DataInputStream(in);
  1215.  
  1216.    System.out.println(Dis.readUTF());
  1217.  
  1218.    Client = new Socket("localhost",4000);
  1219.  
  1220.   BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));
  1221.  
  1222.    String Str = buff.readLine();
  1223.  
  1224.    OutputStream out = Client.getOutputStream();
  1225.  
  1226.    DataOutputStream Dos = new DataOutputStream(out);
  1227.  
  1228.    Str = "Server Says :: " + Str;
  1229.  
  1230.    Dos.writeUTF(Str);
  1231.  
  1232.    Client.close();
  1233.  
  1234.   }
  1235.  
  1236.  }
  1237.  
  1238. }
  1239.  
  1240.  
  1241.  
  1242. //client
  1243. import java.io.*;
  1244. import java.net.*;
  1245. import java.util.*;
  1246.  
  1247. public class EClient
  1248. {
  1249.  public static void main(String[] args) throws IOException
  1250.  {
  1251.   Socket C = new Socket("localhost",3000);
  1252.   BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));
  1253.   String Str = buff.readLine();
  1254.  
  1255.   OutputStream out = C.getOutputStream();
  1256.  
  1257.   DataOutputStream Dos = new DataOutputStream(out);
  1258.        
  1259.   Dos.writeUTF("Client Say :: " + Str);
  1260.   Dos.flush();
  1261.  
  1262.   ServerSocket S = new ServerSocket(4000);
  1263.   Socket Client = S.accept();
  1264.   InputStream in = Client.getInputStream();
  1265.   DataInputStream Dis = new DataInputStream(in);
  1266.   System.out.println(Dis.readUTF());
  1267.   Client.close();
  1268.  }
  1269. }
  1270.  
  1271. OUTPUT:
  1272.  
  1273.  
  1274. ***************************************************
  1275.  
  1276.  
  1277.  
  1278. 2.Write a program to implement a text based message transfer from client to server process using UDP.
  1279.  
  1280. //server
  1281. import java.io.BufferedReader;
  1282. import java.io.BufferedWriter;
  1283. import java.io.InputStream;
  1284. import java.io.InputStreamReader;
  1285. import java.io.OutputStream;
  1286. import java.io.OutputStreamWriter;
  1287. import java.net.ServerSocket;
  1288. import java.net.Socket;
  1289.  
  1290. public class Server
  1291. {
  1292.  
  1293.     private static Socket socket;
  1294.  
  1295.     public static void main(String[] args)
  1296.     {
  1297.         try
  1298.         {
  1299.  
  1300.             int port = 25000;
  1301.             ServerSocket serverSocket = new ServerSocket(port);
  1302.             System.out.println("Server Started and listening to the port 25000");
  1303.  
  1304.             //Server is running always. This is done using this while(true) loop
  1305.             while(true)
  1306.             {
  1307.                 //Reading the message from the client
  1308.                 socket = serverSocket.accept();
  1309.                 InputStream is = socket.getInputStream();
  1310.                 InputStreamReader isr = new InputStreamReader(is);
  1311.                 BufferedReader br = new BufferedReader(isr);
  1312.                 String number = br.readLine();
  1313.                 System.out.println("Message received from client is "+number);
  1314.  
  1315.                 //Multiplying the number by 2 and forming the return message
  1316.                 String returnMessage;
  1317.                 try
  1318.                 {
  1319.                     int numberInIntFormat = Integer.parseInt(number);
  1320.                     int returnValue = numberInIntFormat*2;
  1321.                     returnMessage = String.valueOf(returnValue) + "\n";
  1322.                 }
  1323.                 catch(NumberFormatException e)
  1324.                 {
  1325.                     //Input was not a number. Sending proper message back to client.
  1326.                     returnMessage = "Please send a proper number\n";
  1327.                 }
  1328.  
  1329.                 //Sending the response back to the client.
  1330.                 OutputStream os = socket.getOutputStream();
  1331.                 OutputStreamWriter osw = new OutputStreamWriter(os);
  1332.                 BufferedWriter bw = new BufferedWriter(osw);
  1333.                 bw.write(returnMessage);
  1334.                 System.out.println("Message sent to the client is "+returnMessage);
  1335.                 bw.flush();
  1336.             }
  1337.         }
  1338.         catch (Exception e)
  1339.         {
  1340.             e.printStackTrace();
  1341.         }
  1342.         finally
  1343.         {
  1344.             try
  1345.             {
  1346.                 socket.close();
  1347.             }
  1348.             catch(Exception e){}
  1349.         }
  1350.     }
  1351. }
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360. //client
  1361.  
  1362. import java.io.BufferedReader;
  1363. import java.io.BufferedWriter;
  1364. import java.io.InputStream;
  1365. import java.io.InputStreamReader;
  1366. import java.io.OutputStream;
  1367. import java.io.OutputStreamWriter;
  1368. import java.net.InetAddress;
  1369. import java.net.Socket;
  1370.  
  1371. public class Client
  1372. {
  1373.  
  1374.     private static Socket socket;
  1375.  
  1376.     public static void main(String args[])
  1377.     {
  1378.         try
  1379.         {
  1380.             String host = "localhost";
  1381.             int port = 25000;
  1382.             InetAddress address = InetAddress.getByName(host);
  1383.             socket = new Socket(address, port);
  1384.  
  1385.             //Send the message to the server
  1386.             OutputStream os = socket.getOutputStream();
  1387.             OutputStreamWriter osw = new OutputStreamWriter(os);
  1388.             BufferedWriter bw = new BufferedWriter(osw);
  1389.  
  1390.             String number = "2";
  1391.  
  1392.             String sendMessage = number + "\n";
  1393.             bw.write(sendMessage);
  1394.             bw.flush();
  1395.             System.out.println("Message sent to the server : "+sendMessage);
  1396.  
  1397.             //Get the return message from the server
  1398.             InputStream is = socket.getInputStream();
  1399.             InputStreamReader isr = new InputStreamReader(is);
  1400.             BufferedReader br = new BufferedReader(isr);
  1401.             String message = br.readLine();
  1402.             System.out.println("Message received from the server : " +message);
  1403.         }
  1404.         catch (Exception exception)
  1405.         {
  1406.             exception.printStackTrace();
  1407.         }
  1408.         finally
  1409.         {
  1410.             //Closing the socket
  1411.             try
  1412.             {
  1413.                 socket.close();
  1414.             }
  1415.             catch(Exception e)
  1416.             {
  1417.                 e.printStackTrace();
  1418.             }
  1419.         }
  1420.     }
  1421. }
  1422.  
  1423. OUTPUT:
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430. **********************************************
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436. 3.Implement a chat server and client in java using UDP sockets.
  1437.  
  1438. //server
  1439. import java.io.*;
  1440. import java.net.*;
  1441.  
  1442. public class UDPChatServer {
  1443.     public static void main(String args[]) throws Exception
  1444.     {
  1445.         DatagramSocket serverSocket = new DatagramSocket(6789);
  1446.        
  1447.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  1448.         String reply;
  1449.         while(true)
  1450.         {
  1451.             byte[] receiveData = new byte[1024];
  1452.             byte[] sendData = new byte[1024];
  1453.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1454.             serverSocket.receive(receivePacket);
  1455.  
  1456.             String msg = new String(receivePacket.getData());
  1457.             System.out.println("Message Received: " + msg);
  1458.  
  1459.             InetAddress IPAddress = receivePacket.getAddress();
  1460.             int port = receivePacket.getPort();
  1461.                            
  1462.             System.out.printf("Enter reply: ");
  1463.             reply = inFromUser.readLine();
  1464.             if (reply.equals("bye"))
  1465.             {
  1466.                 System.out.printf("Exiting Chat!");
  1467.                 serverSocket.close();
  1468.                 break;
  1469.             }
  1470.             sendData = reply.getBytes();
  1471.             DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
  1472.             serverSocket.send(sendPacket);
  1473.         }
  1474.        
  1475.     }
  1476. }
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487. //client
  1488. import java.io.*;
  1489. import java.net.*;
  1490.  
  1491. public class UDPChatClient {
  1492.     public static void main(String args[]) throws Exception
  1493.     {
  1494.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  1495.        
  1496.         InetAddress IPAddress = InetAddress.getByName("localhost");
  1497.        
  1498.         String msg = "";
  1499.         while(true)
  1500.         {
  1501.             byte[] sendData = new byte[1024];
  1502.             byte[] receiveData = new byte[1024];
  1503.             DatagramSocket clientSocket = new DatagramSocket();
  1504.             System.out.printf("Send a Message: ");
  1505.             msg = inFromUser.readLine();
  1506.             if (msg.equals("bye"))
  1507.             {
  1508.                 System.out.printf("Exiting Chat!");
  1509.                 clientSocket.close();
  1510.                 break;
  1511.             }
  1512.             sendData = msg.getBytes();
  1513.  
  1514.             DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
  1515.             clientSocket.send(sendPacket);
  1516.  
  1517.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1518.             clientSocket.receive(receivePacket);
  1519.  
  1520.             String reply = new String(receivePacket.getData());
  1521.             System.out.println("Reply: " + reply);
  1522.             clientSocket.close();
  1523.         }
  1524.     }
  1525. }
  1526.  
  1527.  
  1528. ****************************************
  1529.  
  1530.  
  1531.  
  1532. 4. Implement a DNS server and client in java using UDP sockets.
  1533.  
  1534. //server
  1535. import java.io.*;
  1536. import java.net.*;
  1537. import java.util.*;
  1538. class Serverdns12
  1539. {
  1540.  
  1541.             public static void main(String args[])
  1542.             {
  1543.             try
  1544.             {
  1545.                         DatagramSocket server=new DatagramSocket(1309);
  1546.                         while(true)
  1547.                         {
  1548.                                     byte[] sendbyte=new byte[1024];
  1549.                                     byte[] receivebyte=new byte[1024];
  1550.                                     DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
  1551.                                     server.receive(receiver);
  1552.                                     String str=new String(receiver.getData());
  1553.                                     String s=str.trim();
  1554.                                     //System.out.println(s);
  1555.                                     InetAddress addr=receiver.getAddress();
  1556.                                     int port=receiver.getPort();
  1557.                                     String ip[]={"165.165.80.80","165.165.79.1"};
  1558.                                     String name[]={"www.aptitudeguru.com","www.downloadcyclone.blogspot.com"};
  1559.                                     for(int i=0;i<ip.length;i++)
  1560.                                     {
  1561.                                                 if(s.equals(ip[i]))
  1562.                                                 {
  1563.                                                             sendbyte=name[i].getBytes();
  1564.                                                             DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
  1565.                                                             server.send(sender);
  1566.                                                             break;
  1567.                                                 }
  1568.                                                 else if(s.equals(name[i]))
  1569.                                                 {
  1570.                                                             sendbyte=ip[i].getBytes();
  1571.                                                             DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
  1572.                                                             server.send(sender);
  1573.                                                             break;
  1574.                                                 }
  1575.                                     }        
  1576.                                     break;
  1577.                         }
  1578.             }
  1579.             catch(Exception e)
  1580.             {
  1581.                         System.out.println(e);
  1582.             }
  1583.             }
  1584. }
  1585.  
  1586.  
  1587.  
  1588.  
  1589. //client
  1590.  
  1591. import java.io.*;
  1592. import java.net.*;
  1593. import java.util.*;
  1594. class Clientdns12
  1595. {
  1596.             public static void main(String args[])
  1597.             {
  1598.             try
  1599.             {
  1600.                         DatagramSocket client=new DatagramSocket();
  1601.                         InetAddress addr=InetAddress.getByName("127.0.0.1");
  1602.  
  1603.                         byte[] sendbyte=new byte[1024];
  1604.                         byte[] receivebyte=new byte[1024];
  1605.  
  1606.                         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  1607.                         System.out.println("Enter the DOMAIN NAME or IP adress:");
  1608.                         String str=in.readLine();
  1609.                         sendbyte=str.getBytes();
  1610.                         DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
  1611.                         client.send(sender);
  1612.                         DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
  1613.                         client.receive(receiver);
  1614.                         String s=new String(receiver.getData());
  1615.                         System.out.println("IP address or DOMAIN NAME: "+s.trim());  
  1616.                         client.close();
  1617.             }
  1618.             catch(Exception e)
  1619.             {
  1620.                         System.out.println(e);
  1621.             }
  1622.             }
  1623. }
  1624.  
  1625. ***************************
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631. 5. Find the logical address of a host when its physical address is known (RARP protocol) using UDP.
  1632.  
  1633. //SERVER
  1634. import java.io.*;
  1635. import java.net.*;
  1636.  
  1637. public class UDPRARPServer {
  1638.     public static void main(String args[]) throws Exception
  1639.     {
  1640.         DatagramSocket serverSocket = new DatagramSocket(6789);
  1641.         byte[] receiveData = new byte[1024];
  1642.         byte[] sendData = new byte[1024];
  1643.  
  1644.         String logical[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
  1645.         String physical[] = {"8E:AE:08:AA","0A:78:6C:AD", "d4:6d:50:84:db:c8"};  
  1646.         String reply = "";
  1647.  
  1648.         while(true)
  1649.         {
  1650.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1651.             serverSocket.receive(receivePacket);
  1652.  
  1653.             String phyaddr = new String(receivePacket.getData());
  1654.             System.out.println("Physical Address received: " + phyaddr);
  1655.             phyaddr = phyaddr.trim();    
  1656.             InetAddress IPAddress = receivePacket.getAddress();
  1657.             int port = receivePacket.getPort();
  1658.  
  1659.             for(int i = 0; i < physical.length; i++)
  1660.             {
  1661.                 if (phyaddr.equals(physical[i]))
  1662.                 {
  1663.                     reply = logical[i];
  1664.                     sendData = reply.getBytes();
  1665.                     DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
  1666.                     serverSocket.send(sendPacket);
  1667.                 }
  1668.             }
  1669.                        
  1670.         }
  1671.     }
  1672. }
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683. //CLIENT
  1684. import java.io.*;
  1685. import java.net.*;
  1686.  
  1687. public class UDPRARPClient {
  1688.     public static void main(String args[]) throws Exception
  1689.     {
  1690.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  1691.         DatagramSocket clientSocket = new DatagramSocket();
  1692.         InetAddress IPAddress = InetAddress.getByName("localhost");
  1693.         byte[] sendData = new byte[1024];
  1694.         byte[] receiveData = new byte[1024];
  1695.        
  1696.         System.out.printf("Enter Physical address: ");
  1697.         String phyaddr = inFromUser.readLine();
  1698.         sendData = phyaddr.getBytes();
  1699.  
  1700.         DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
  1701.         clientSocket.send(sendPacket);
  1702.  
  1703.         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1704.         clientSocket.receive(receivePacket);
  1705.  
  1706.         String logaddr = new String(receivePacket.getData());
  1707.         System.out.println("Logical Address:" + logaddr);
  1708.         clientSocket.close();
  1709.     }
  1710. }
  1711.  
  1712.  
  1713. ********************************
  1714.  
  1715.  
  1716. 6.Find the physical address of a host when its logical address is known (ARP protocol) using UDP.
  1717.  
  1718. //server
  1719. import java.io.*;
  1720. import java.net.*;
  1721. import java.util.*;
  1722. class Serverarp12
  1723. {
  1724.             public static void main(String args[])
  1725.             {
  1726.             try
  1727.             {
  1728.                         DatagramSocket server=new DatagramSocket(1309);
  1729.                         while(true)
  1730.                         {
  1731.                                     byte[] sendbyte=new byte[1024];
  1732.                                     byte[] receivebyte=new byte[1024];
  1733.                                     DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
  1734.                                     server.receive(receiver);
  1735.                                     String str=new String(receiver.getData());
  1736.                                     String s=str.trim();
  1737.                                     //System.out.println(s);
  1738.                                     InetAddress addr=receiver.getAddress();
  1739.                                     int port=receiver.getPort();
  1740.                                     String ip[]={"165.165.80.80","165.165.79.1"};
  1741.                                     String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
  1742.                                     for(int i=0;i<ip.length;i++)
  1743.                                     {
  1744.                                                 if(s.equals(ip[i]))
  1745.                                                 {
  1746.                                                             sendbyte=mac[i].getBytes();
  1747.                                                             DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
  1748.                                                             server.send(sender);
  1749.                                                             break;
  1750.                                                 }
  1751.                                     }
  1752.                                     break;
  1753.  
  1754.  
  1755.                         }
  1756.             }
  1757.             catch(Exception e)
  1758.             {
  1759.                         System.out.println(e);
  1760.             }
  1761.             }
  1762. }
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776. //client
  1777. import java.io.*;
  1778. import java.net.*;
  1779. import java.util.*;
  1780. class Clientarp12
  1781. {
  1782.             public static void main(String args[])
  1783.             {
  1784.             try
  1785.             {
  1786.                         DatagramSocket client=new DatagramSocket();
  1787.                         InetAddress addr=InetAddress.getByName("127.0.0.1");
  1788.  
  1789.                         byte[] sendbyte=new byte[1024];
  1790.                         byte[] receivebyte=new byte[1024];
  1791.                         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  1792.                         System.out.println("Enter the logical address (IP):");
  1793.                         String str=in.readLine();
  1794.                         sendbyte=str.getBytes();
  1795.                         DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
  1796.                         client.send(sender);
  1797.                         DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
  1798.                         client.receive(receiver);
  1799.                         String s=new String(receiver.getData());
  1800.                         System.out.println("The Physical Address is(MAC): "+s.trim());    
  1801.                         client.close();
  1802.             }
  1803.             catch(Exception e)
  1804.             {
  1805.                         System.out.println(e);
  1806.             }
  1807.             }
  1808. }
  1809.  
  1810.  
  1811.  
  1812. *********************************
  1813.  
  1814.  
  1815. 7.Implement Client - Server communication to access Date using UDP in Java.
  1816.  
  1817.  
  1818. //server
  1819. import java.io.*;
  1820. import java.net.*;
  1821. import java.util.*;
  1822.  
  1823. public class UDPDateServer {
  1824.     public static void main(String args[]) throws Exception
  1825.     {
  1826.         DatagramSocket serverSocket = new DatagramSocket(6789);
  1827.         byte[] receiveData = new byte[1024];
  1828.         byte[] sendData = new byte[1024];
  1829.         String GetDate = "";
  1830.         while(true)
  1831.         {
  1832.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1833.             serverSocket.receive(receivePacket);
  1834.  
  1835.             String msg = new String(receivePacket.getData());
  1836.             System.out.println("Message: " + msg);
  1837.  
  1838.             InetAddress IPAddress = receivePacket.getAddress();
  1839.             int port = receivePacket.getPort();
  1840.             msg = msg.trim();
  1841.             if (msg.equals("date"))
  1842.             {
  1843.                 Date d = new Date();
  1844.                 GetDate = d.toString();
  1845.             }
  1846.        
  1847.             sendData = GetDate.getBytes();
  1848.             DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
  1849.             serverSocket.send(sendPacket);
  1850.         }
  1851.     }
  1852. }
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.  
  1861.  
  1862.  
  1863.  
  1864.  
  1865.  
  1866. //client
  1867.  
  1868. import java.io.*;
  1869. import java.net.*;
  1870.  
  1871.  
  1872. public class UDPDateClient {
  1873.     public static void main(String args[]) throws Exception
  1874.     {
  1875.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  1876.         DatagramSocket clientSocket = new DatagramSocket();
  1877.         InetAddress IPAddress = InetAddress.getByName("localhost");
  1878.         byte[] sendData = new byte[1024];
  1879.         byte[] receiveData = new byte[1024];
  1880.        
  1881.         System.out.printf("Enter Request: ");
  1882.         String msg = inFromUser.readLine();
  1883.         sendData = msg.getBytes();
  1884.  
  1885.         DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
  1886.         clientSocket.send(sendPacket);
  1887.  
  1888.         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1889.         clientSocket.receive(receivePacket);
  1890.  
  1891.         String reply = new String(receivePacket.getData());
  1892.         System.out.println("FROM SERVER:" + reply);
  1893.         clientSocket.close();
  1894.     }
  1895. }
  1896.  
  1897.  
  1898.  
  1899.  
  1900. 7 USING UDP
  1901.  
  1902. import java.net.*;
  1903. import java.io.*;
  1904.  
  1905. public class Clientnew {
  1906.  
  1907. public static void main(String[] args) throws Exception{
  1908.  
  1909.     System.out.println("Server Time >>>>");
  1910.  
  1911.     DatagramSocket cs=new DatagramSocket();
  1912.  
  1913.     InetAddress ip=InetAddress.getByName("localhost");
  1914.  
  1915.     byte[] rd=new byte[100];
  1916.     byte[] sd=new byte[100];
  1917.  
  1918.     DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,1234);
  1919.  
  1920.     DatagramPacket rp=new DatagramPacket(rd,rd.length);
  1921.  
  1922.     cs.send(sp);
  1923.  
  1924.     cs.receive(rp);
  1925.  
  1926.     String time=new String(rp.getData());
  1927.  
  1928.     System.out.println(time);
  1929.  
  1930.     cs.close();
  1931.  
  1932. }
  1933.  
  1934. }
  1935.  
  1936.  
  1937.  
  1938. ==============================
  1939.  
  1940. import java.net.*;
  1941. import java.io.*;
  1942. import java.util.*;
  1943.  
  1944. public class Server {
  1945.  
  1946. public static void main(String[] args) throws Exception{
  1947.  
  1948. DatagramSocket ss=new DatagramSocket(1234);
  1949.  
  1950. while(true){
  1951.  
  1952. System.out.println("Server is up....");
  1953.  
  1954. byte[] rd=new byte[100];
  1955. byte[] sd=new byte[100];
  1956.  
  1957. DatagramPacket rp=new DatagramPacket(rd,rd.length);
  1958.  
  1959. ss.receive(rp);
  1960.  
  1961. InetAddress ip= rp.getAddress();
  1962.  
  1963. int port=rp.getPort();
  1964.  
  1965. Date d=new Date();   // getting system time
  1966.  
  1967. String time= d + "";  // converting it to String
  1968.  
  1969. sd=time.getBytes();   // converting that String to byte
  1970.  
  1971. DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port);
  1972.  
  1973. ss.send(sp);
  1974.  
  1975. rp=null;
  1976.  
  1977. System.out.println("Done !! ");
  1978.  
  1979. }
  1980.  
  1981. }
  1982.  
  1983. }
  1984. 1. Write a Java program to display the server’s date and time details at the client end.
  1985.  
  1986. Server:
  1987.  
  1988. import java.io.*;
  1989. import java.net.*;
  1990. import java.util.Date;
  1991. class server1 {
  1992.   public static void main(String argv[]) throws Exception
  1993.     {
  1994.       String capitalizedSentence;
  1995.       ServerSocket welcomeSocket = new ServerSocket(6789);
  1996.       while(true) {
  1997.             Socket connectionSocket = welcomeSocket.accept();
  1998. DataOutputStream  outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  1999.            capitalizedSentence = new Date().toString() + '\n';
  2000.            outToClient.writeBytes(capitalizedSentence);
  2001.         }
  2002.     }
  2003. }
  2004.  
  2005.  
  2006. Client:
  2007.  
  2008. import java.io.*;
  2009. import java.net.*;
  2010. class client1 {
  2011.     public static void main(String argv[]) throws Exception
  2012.     {  
  2013.    String modifiedSentence;
  2014.    Socket clientSocket = new Socket("localhost", 6789);
  2015. BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  2016.         modifiedSentence = inFromServer.readLine();
  2017.         System.out.println("FROM SERVER: " + modifiedSentence);
  2018.         clientSocket.close();              
  2019.     }
  2020. }
  2021.  
  2022.  
  2023. 2. Write a Java program to display the client’s address at the server end.
  2024.  
  2025. Server:
  2026.  
  2027. import java.io.*;
  2028. import java.net.*;
  2029.  
  2030. class server1 {
  2031.   public static void main(String argv[]) throws Exception
  2032.     {
  2033.       ServerSocket welcomeSocket = new ServerSocket(6789);
  2034.       while(true) {
  2035.             Socket connectionSocket = welcomeSocket.accept();
  2036.             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));  
  2037. System.out.println("Address Received from client : "+ inFromClient.readLine());
  2038.         }
  2039.     }
  2040. }
  2041.  
  2042.  
  2043. Client:
  2044.  
  2045. import java.io.*;
  2046. import java.net.*;
  2047. class client1 {
  2048.     public static void main(String argv[]) throws Exception
  2049.     {  
  2050.    Socket clientSocket = new Socket("localhost", 6789);
  2051. DataOutputStream outToServer =  new DataOutputStream(clientSocket.getOutputStream());
  2052.         outToServer.writeBytes(InetAddress.getLocalHost().getHostAddress() + '\n');
  2053.         clientSocket.close();
  2054.                    
  2055.     }
  2056. }
  2057.  
  2058.  
  2059.  
  2060. 3. Write a Java program to implement an echo UDP server.
  2061. 4. Write a Java program to develop a simple Chat application.
  2062.  
  2063.  
  2064. 5. The message entered in the client is sent to the server and the server encodes the message and
  2065. returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
  2066. b as c ...z as a. This process is done using the TCP/IP protocol. Write a Java program for the above
  2067.  
  2068. server:
  2069.  
  2070. import java.io.*;
  2071. import java.net.*;
  2072.  
  2073. class server1 {
  2074.  
  2075.     public static void main(String argv[]) throws Exception {
  2076.         String clientSentence;
  2077.         ServerSocket welcomeSocket = new ServerSocket(6789);
  2078.         while (true) {
  2079.             Socket connectionSocket = welcomeSocket.accept();
  2080.             BufferedReader inFromClient
  2081.                     = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  2082.             DataOutputStream outToClient
  2083.                     = new DataOutputStream(connectionSocket.getOutputStream());
  2084.             clientSentence = inFromClient.readLine();
  2085.             System.out.println("Received from client : " + clientSentence);
  2086.             String a = clientSentence.toLowerCase();
  2087.             String b = "";//abc
  2088.             String x = "abcdefghijklmnopqrstuvwxyz";
  2089. //code to encode
  2090.             for (int i = 0; i < a.length(); i++) {
  2091.                 if (a.charAt(i) == 'z') {
  2092.                     b += 'a';
  2093.                 } else {
  2094.                     for (int j = 0; j < x.length(); j++) {
  2095.                         if (x.charAt(j) == a.charAt(i)) {
  2096.                             b += x.charAt(j + 1);
  2097.                             break;
  2098.                         }
  2099.                     }
  2100.                 }
  2101.             }
  2102.             outToClient.writeBytes(b+"\n");
  2103.         }  
  2104.     }
  2105. }
  2106.  
  2107. client:
  2108.  
  2109. import java.io.*;
  2110. import java.net.*;
  2111. class client1 {
  2112.     public static void main(String argv[]) throws Exception {
  2113.         String sentence;
  2114.         String modifiedSentence;
  2115.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  2116.         Socket clientSocket = new Socket("localhost", 6789);
  2117.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  2118.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  2119.         sentence = inFromUser.readLine();
  2120.         outToServer.writeBytes(sentence + '\n');
  2121.  
  2122.         modifiedSentence = inFromServer.readLine();
  2123.  
  2124.         System.out.println("FROM SERVER: " + modifiedSentence);
  2125.  
  2126.         clientSocket.close();
  2127.  
  2128.     }
  2129. }
  2130.  
  2131. 6. 5th pro with udp
  2132.  
  2133.  
  2134. 7. Write a Java program to display the name and address of the computer that we are currently
  2135. working on.
  2136.  
  2137. import java.net.InetAddress;
  2138. public class pro1 {
  2139.   public static void main(String []args) throws Exception{
  2140.       InetAddress a=InetAddress.getLocalHost();
  2141.       System.out.println(a);
  2142.   }  
  2143. }
  2144.  
  2145. LAB:
  2146. 1. Write a Java program to display the server’s date and time details at the client end.
  2147.  
  2148.  
  2149. server:
  2150.  
  2151. import java.util.*;
  2152. import java.net.*;
  2153. import java.io.*;
  2154. public class datetime
  2155. {
  2156. public static void main(String args[])throws Exception
  2157. {
  2158. ServerSocket listener=new ServerSocket(9090);
  2159. while(true)
  2160. {
  2161. Socket socket=listener.accept();
  2162. PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
  2163. out.println(new Date().toString());
  2164. socket.close();
  2165. listener.close();
  2166. }
  2167. }
  2168. }
  2169.  
  2170. client:
  2171.  
  2172. import java.util.*;
  2173. import java.net.*;
  2174. import java.io.*;
  2175. public class dateclient
  2176. {
  2177. public static void main(String args[])throws Exception
  2178. {
  2179. Socket s=new Socket("127.0.0.1",9090);
  2180. BufferedReader input=new BufferedReader(new InputStreamReader(s.getInputStream()));
  2181. String answer=input.readLine();
  2182. System.out.println("date+time:::\t"+answer);
  2183. System.exit(0);
  2184. }
  2185. }
  2186.  
  2187.  
  2188. output:
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202. 2. Write a Java program to display the client’s address at the server end.
  2203.  
  2204. Server:
  2205.  
  2206. import java.io.*;
  2207. import java.net.*;
  2208.  
  2209. class server1 {
  2210.   public static void main(String argv[]) throws Exception
  2211.     {
  2212.       ServerSocket welcomeSocket = new ServerSocket(6789);
  2213.       while(true) {
  2214.             Socket connectionSocket = welcomeSocket.accept();
  2215.             BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));  
  2216. System.out.println("Address Received from client : "+ inFromClient.readLine());
  2217.         }
  2218.     }
  2219. }
  2220.  
  2221.  
  2222. Client:
  2223.  
  2224. import java.io.*;
  2225. import java.net.*;
  2226. class client1 {
  2227.     public static void main(String argv[]) throws Exception
  2228.     {  
  2229.    Socket clientSocket = new Socket("localhost", 6789);
  2230. DataOutputStream outToServer =  new DataOutputStream(clientSocket.getOutputStream());
  2231.         outToServer.writeBytes(InetAddress.getLocalHost().getHostAddress() + '\n');
  2232.         clientSocket.close();
  2233.                    
  2234.     }
  2235. }
  2236.  
  2237.  
  2238.  
  2239. 4) Write a Java program to develop a simple Chat application
  2240.  
  2241.  
  2242.  
  2243. import java.io.*;
  2244. import java.io.BufferedReader;
  2245. import java.io.InputStream;
  2246. import java.io.InputStreamReader;
  2247. import java.io.OutputStream;
  2248. import java.io.PrintWriter;
  2249. import java.net.*;
  2250. public class GossipServer
  2251. {
  2252.   public static void main(String[] args) throws Exception
  2253.   {
  2254.       ServerSocket sersock = new ServerSocket(3000);
  2255.       Socket sock = sersock.accept( );                          
  2256.                              
  2257.       BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  2258.                      
  2259.       OutputStream ostream = sock.getOutputStream();
  2260.       PrintWriter pwrite = new PrintWriter(ostream, true);
  2261.  
  2262.                              
  2263.       InputStream istream = sock.getInputStream();
  2264.       BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  2265.  
  2266.       String receiveMessage, sendMessage;              
  2267.       while(true)
  2268.       {
  2269.         if((receiveMessage = receiveRead.readLine()) != null)  
  2270.         {
  2271.            System.out.println(receiveMessage);        
  2272.         }        
  2273.         sendMessage = keyRead.readLine();
  2274.         pwrite.println(sendMessage);            
  2275.         pwrite.flush();
  2276.       }              
  2277.     }                    
  2278. }  
  2279.  
  2280.  
  2281. CHAT CLIENT
  2282.  
  2283. import java.io.*;
  2284. import java.net.*;
  2285. public class GossipClient
  2286. {
  2287.   public static void main(String[] args) throws Exception
  2288.   {
  2289.      Socket sock = new Socket("127.0.0.1", 3000);
  2290.                              
  2291.      BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  2292.                              
  2293.      OutputStream ostream = sock.getOutputStream();
  2294.      PrintWriter pwrite = new PrintWriter(ostream, true);
  2295.  
  2296.      InputStream istream = sock.getInputStream();
  2297.      BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  2298.  
  2299.      System.out.println("Start the chitchat, type and press Enter key");
  2300.  
  2301.      String receiveMessage, sendMessage;              
  2302.      while(true)
  2303.      {
  2304.         sendMessage = keyRead.readLine();  
  2305.         pwrite.println(sendMessage);      
  2306.         pwrite.flush();                    
  2307.        if((receiveMessage = receiveRead.readLine()) != null)
  2308.         {
  2309.             System.out.println(receiveMessage);
  2310.         }        
  2311.       }              
  2312.     }                    
  2313. }  
  2314.  
  2315.  
  2316.  
  2317. 5). The message entered in the client is sent to the server and the server encodes the message and
  2318. returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
  2319. b as c ...z as a. This process is done using the TCP/IP protocol. Write a Java program for the above
  2320.  
  2321. server:
  2322.  
  2323. import java.io.*;
  2324. import java.net.*;
  2325.  
  2326. class server1 {
  2327.  
  2328.     public static void main(String argv[]) throws Exception {
  2329.         String clientSentence;
  2330.         ServerSocket welcomeSocket = new ServerSocket(6789);
  2331.         while (true) {
  2332.             Socket connectionSocket = welcomeSocket.accept();
  2333.             BufferedReader inFromClient
  2334.                     = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  2335.             DataOutputStream outToClient
  2336.                     = new DataOutputStream(connectionSocket.getOutputStream());
  2337.             clientSentence = inFromClient.readLine();
  2338.             System.out.println("Received from client : " + clientSentence);
  2339.             String a = clientSentence.toLowerCase();
  2340.             String b = "";//abc
  2341.             String x = "abcdefghijklmnopqrstuvwxyz";
  2342.             for (int i = 0; i < a.length(); i++) {
  2343.                 if (a.charAt(i) == 'z') {
  2344.                     b += 'a';
  2345.                 } else {
  2346.                     for (int j = 0; j < x.length(); j++) {
  2347.                         if (x.charAt(j) == a.charAt(i)) {
  2348.                             b += x.charAt(j + 1);
  2349.                             break;
  2350.                         }
  2351.                     }
  2352.                 }
  2353.             }
  2354.             outToClient.writeBytes(b+"\n");
  2355.         }  
  2356.     }
  2357. }
  2358.  
  2359. client:
  2360.  
  2361. import java.io.*;
  2362. import java.net.*;
  2363. class client1 {
  2364.     public static void main(String argv[]) throws Exception {
  2365.         String sentence;
  2366.         String modifiedSentence;
  2367.         BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  2368.         Socket clientSocket = new Socket("localhost", 6789);
  2369.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  2370.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  2371.         sentence = inFromUser.readLine();
  2372.         outToServer.writeBytes(sentence + '\n');
  2373.  
  2374.         modifiedSentence = inFromServer.readLine();
  2375.  
  2376.         System.out.println("FROM SERVER: " + modifiedSentence);
  2377.  
  2378.         clientSocket.close();
  2379.  
  2380.     }
  2381. }
  2382.  
  2383.  
  2384. 7)Write a Java program to display the name and address of the computer that we are currently
  2385.  
  2386. working on.
  2387.  
  2388.  
  2389. import java.net.*;
  2390.  
  2391. import java.io.*;
  2392.  
  2393. import java.util.*;
  2394.  
  2395. public class cm
  2396.  
  2397. {
  2398.  
  2399. public static void main(String args[]) throws UnknownHostException
  2400.  
  2401. {
  2402.  
  2403. InetAddress add=InetAddress.getLocalHost();
  2404.  
  2405. System.out.println("Host Name:"+add.getHostName());
  2406.  
  2407. System.out.println("IP address:"+add.getHostAddress());
  2408.  
  2409. }
  2410.  
  2411. }
  2412.  
  2413.  
  2414.  
  2415. 9. Using threading concepts, write a Java program to create a daemon process.  
  2416.  
  2417. import java.util.*;
  2418. import java.io.*;
  2419. import java.net.*;
  2420. class daemon extends Thread
  2421. {
  2422. public daemon()
  2423. {
  2424. setDaemon(true);
  2425. }
  2426. public void run()
  2427. {
  2428. if(Thread.currentThread().isDaemon())
  2429. {
  2430. int count=0;
  2431. while(true)
  2432. {
  2433. System.out.println("hello "+count++);
  2434. try
  2435. {
  2436. sleep(1000);
  2437. }
  2438. catch(InterruptedException e){}
  2439. }
  2440. }
  2441. }
  2442. public static void main(String args[])
  2443. {  
  2444. daemon d=new daemon();
  2445. d.start();
  2446. System.out.println("Main method started and goes on sleep");
  2447. try{
  2448. Thread.sleep(7500);
  2449. }
  2450. catch(InterruptedException e){}
  2451. System.out.println("Main Thread end");
  2452. }}
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458. 3. Write a Java program to implement an echo UDP server
  2459.  
  2460. import java.util.*;
  2461.  
  2462. public class ClientEcho
  2463. {
  2464.   public static void main( String args[] ) throws Exception
  2465.   {
  2466.     InetAddress add = InetAddress.getByName("snrao");  
  2467.                                          
  2468.     DatagramSocket dsock = new DatagramSocket( );
  2469.     String message1 = "This is client calling";    
  2470.     byte arr[] = message1.getBytes( );  
  2471.     DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
  2472.     dsock.send(dpack);                                   // send the packet
  2473.     Date sendTime = new Date();                          // note the time of sending the message
  2474.  
  2475.     dsock.receive(dpack);                                // receive the packet
  2476.     String message2 = new String(dpack.getData( ));
  2477.     Date receiveTime = new Date( );   // note the time of receiving the message
  2478.     System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
  2479.   }
  2480. }
  2481.  
  2482. import java.util.*;
  2483.  
  2484. public class ServerEcho
  2485. {
  2486.   public static void main( String args[]) throws Exception
  2487.   {
  2488.     DatagramSocket dsock = new DatagramSocket(7);
  2489.     byte arr1[] = new byte[150];                                
  2490.     DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
  2491.    
  2492.     while(true)
  2493.     {
  2494.       dsock.receive(dpack);
  2495.      
  2496.       byte arr2[] = dpack.getData();
  2497.       int packSize = dpack.getLength();
  2498.       String s2 = new String(arr2, 0, packSize);
  2499.  
  2500.       System.out.println( new Date( ) + "  " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
  2501.       dsock.send(dpack);
  2502.     }
  2503.   }          
  2504. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top