Advertisement
letscheat1234

Nalla File2

Apr 21st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 75.34 KB | None | 0 0
  1. Navaneethan
  2.  
  3. -Implement echo server and client in java using UDP sockets.
  4. -Implement a simple message transfer from client to server process using UDP.
  5. -Simple chat application in java using datagram socket and datagram packet
  6. -To create a client server program to Domain Name System using the UDP protocol client server.
  7. -UDP DATE SERVER
  8. ------------------------------------------
  9. 1) Write a program to list all ports hosting a TCP server in a specified host.
  10. 2) Write a program to display the server’s date and time details at the client end.
  11. 3) Write a program to display the client’s address at the server end.
  12. 4) Implement a simple message transfer from client to server process using TCP/IP.
  13. 5) Develop a TCP client/server application for transferring a text file from client to server
  14. 6) 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.
  15. 7) Write a program to develop a simple (text based) Chat application using TCP/IP.
  16. 8) Implement a simple message transfer from client to server process using UDP.
  17. 9) Write a program to implement an Echo UDP server. Test the working of the server by writing a client application.
  18. ------------------------------------------
  19. Basic Network Commands
  20.  
  21.  
  22.  
  23.  
  24. question one
  25. Implement echo server and client in java using UDP sockets.
  26.  
  27. Client program – ServerEcho.java
  28.  
  29. import java.net.*;
  30. import java.util.*;
  31.  
  32. public class ServerEcho
  33. {
  34.   public static void main( String args[]) throws Exception
  35.   {
  36.     DatagramSocket dsock = new DatagramSocket(7);
  37.     byte arr1[] = new byte[150];                                
  38.     DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
  39.    
  40.     while(true)
  41.     {
  42.       dsock.receive(dpack);
  43.      
  44.       byte arr2[] = dpack.getData();
  45.       int packSize = dpack.getLength();
  46.       String s2 = new String(arr2, 0, packSize);
  47.  
  48.       System.out.println( new Date( ) + "  " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
  49.       dsock.send(dpack);
  50.     }
  51.   }          
  52. }
  53.  
  54. Client program – ClientEcho.java
  55. import java.net.*;
  56. import java.util.*;
  57.  
  58. public class ClientEcho
  59. {
  60.   public static void main( String args[] ) throws Exception
  61.   {
  62.     InetAddress add = InetAddress.getByName("snrao");  
  63.                                          
  64.     DatagramSocket dsock = new DatagramSocket( );
  65.     String message1 = "This is client calling";    
  66.     byte arr[] = message1.getBytes( );  
  67.     DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
  68.     dsock.send(dpack);                                   // send the packet
  69.     Date sendTime = new Date();                          // note the time of sending the message
  70.  
  71.     dsock.receive(dpack);                                // receive the packet
  72.     String message2 = new String(dpack.getData( ));
  73.     Date receiveTime = new Date( );   // note the time of receiving the message
  74.     System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
  75.   }
  76. }
  77.  
  78.  
  79.                             OR
  80.  
  81. import java.net.*;
  82.  import java.util.*;
  83.  public class EchoServer
  84. {
  85.      public static void main( String args[]) throws Exception
  86.  {
  87.          DatagramSocket dsock = new DatagramSocket(7);
  88. byte arr1[] = new byte[150];
  89. DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
  90. while(true)
  91. { dsock.receive(dpack);
  92. byte arr2[] = dpack.getData();
  93.  int packSize = dpack.getLength();
  94. String s2 = new String(arr2, 0, packSize);
  95. System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
  96. dsock.send(dpack);
  97. }
  98. }
  99. }----------------------------------
  100. import java.net.*;
  101. import java.util.*;
  102.  public class EchoClient
  103. { public static void main( String args[] ) throws Exception {
  104. InetAddress add = InetAddress.getByName("127.0.0.1");
  105. DatagramSocket dsock = new DatagramSocket( );
  106.  String message1 = "This is client calling";
  107.  byte arr[] = message1.getBytes( );
  108.  DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
  109.  dsock.send(dpack); // send the packet
  110. Date sendTime = new Date( ); // note the time of sending the message
  111. dsock.receive(dpack); // receive the packet
  112. String message2 = new String(dpack.getData( ));
  113.  Date receiveTime = new Date( ); // note the time of receiving the message
  114.  System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
  115.   }
  116.  }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. question 2
  126. Implement a simple message transfer from client to server process using UDP.
  127. import java.io.*;
  128. import java.net.*;
  129. class UDPServerss {
  130. public static void main(String args[]) throws Exception       {
  131.      DatagramSocket serverSocket = new DatagramSocket(9876);
  132.  
  133.    byte[] receiveData = new byte[1024];
  134.  byte[] sendData = new byte[1024];
  135.     while(true)                {
  136.  
  137.               DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  138.               serverSocket.receive(receivePacket);
  139.                   String sentence = new String( receivePacket.getData());
  140.                   System.out.println("RECEIVED: " + sentence);
  141.       InetAddress IPAddress = receivePacket.getAddress();
  142.             int port = receivePacket.getPort();
  143.   String capitalizedSentence = sentence.toUpperCase();
  144.          sendData = capitalizedSentence.getBytes();
  145.       DatagramPacket sendPacket =                   new DatagramPacket(sendData, sendData.length, IPAddress, port);
  146.         serverSocket.send(sendPacket);
  147.            }
  148. }
  149. }
  150. import java.io.*;
  151. import java.net.*;
  152.  class UDPClientssss {
  153.    public static void main(String args[]) throws Exception    {
  154.       BufferedReader inFromUser =          new BufferedReader(new InputStreamReader(System.in));
  155.       DatagramSocket clientSocket = new DatagramSocket();
  156.  InetAddress IPAddress = InetAddress.getByName("localhost");
  157.       byte[] sendData = new byte[1024];
  158.     byte[] receiveData = new byte[1024];
  159.      String sentence = inFromUser.readLine();
  160.      sendData = sentence.getBytes();
  161.   DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
  162.   clientSocket.send(sendPacket);
  163.   DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  164.   clientSocket.receive(receivePacket);
  165.  String modifiedSentence = new String(receivePacket.getData());
  166.       System.out.println("FROM SERVER:" + modifiedSentence);
  167.   clientSocket.close();
  168.    }
  169.  }
  170.  
  171.    
  172.  
  173.  
  174.  
  175.  
  176. question 3
  177.  Simple chat application in java using datagram socket and datagram packet
  178. Algorithm
  179. Start the UDP chat program
  180. Import the package java.net.*;
  181. Declare the datagramsocket,datagrampacket,BufferedReader,InetAddress.
  182. Start the main function
  183. In the main function using while loop it perform the loop until str.equals is STOP
  184. There important while loop function are
  185. clientsocket = new DatagramSocket(cport);
  186. dp = new DatagramPacket(buf, buf.length);
  187. dis = new BufferedReader(new
  188. InputStreamReader(System.in));
  189. ia = InetAddress.getLocalHost(); f it is stop then break the while loop
  190. Terminate the UDP client program
  191. source code java programming UDP Chat server
  192. import java.io.*;
  193. import java.net.*;
  194. class UDPServer
  195. {
  196. public static DatagramSocket serversocket;
  197. public static DatagramPacket dp;
  198. public static BufferedReader dis;
  199. public static InetAddress ia;
  200. public static byte buf[] = new byte[1024];
  201. public static int cport = 789,sport=790;
  202. public static void main(String[] a) throws IOException
  203. {
  204. serversocket = new DatagramSocket(sport);
  205. dp = new DatagramPacket(buf,buf.length);
  206. dis = new BufferedReader
  207. (new InputStreamReader(System.in));
  208. ia = InetAddress.getLocalHost();
  209. System.out.println("Server is Running...");
  210. while(true)
  211. {
  212. serversocket.receive(dp);
  213. String str = new String(dp.getData(), 0,
  214. dp.getLength());
  215. if(str.equals("STOP"))
  216. {
  217. System.out.println("Terminated...");
  218. break;
  219. }
  220. System.out.println("Client: " + str);
  221. String str1 = new String(dis.readLine());
  222. buf = str1.getBytes();
  223. serversocket.send(new
  224. DatagramPacket(buf,str1.length(), ia, cport));
  225. }
  226. }
  227. }
  228.  
  229. Output:-
  230. C:\IPLAB>javac UDPServer.java
  231. C:\IPLAB>java UDPServer
  232. Server is Running...
  233. Client: Hello
  234. Welcome
  235. Terminated...
  236.  
  237. source code java programming UDP Chat Client
  238. import java.io.*;
  239. import java.net.*;
  240. class UDPClient
  241. {
  242. public static DatagramSocket clientsocket;
  243. public static DatagramPacket dp;
  244. public static BufferedReader dis;
  245. public static InetAddress ia;
  246. public static byte buf[] = new byte[1024];
  247. public static int cport = 789, sport = 790;
  248. public static void main(String[] a) throws IOException
  249. {
  250. clientsocket = new DatagramSocket(cport);
  251. dp = new DatagramPacket(buf, buf.length);
  252. dis = new BufferedReader(new
  253. InputStreamReader(System.in));
  254. ia = InetAddress.getLocalHost();
  255. System.out.println("Client is Running... Type 'STOP'
  256. to Quit");
  257. while(true)
  258. {
  259. String str = new String(dis.readLine());
  260. buf = str.getBytes();
  261. if(str.equals("STOP"))
  262. {
  263. System.out.println("Terminated...");
  264. clientsocket.send(new
  265. DatagramPacket(buf,str.length(), ia,
  266. sport));
  267. break;
  268. }
  269. clientsocket.send(new DatagramPacket(buf,
  270. str.length(), ia, sport));
  271. clientsocket.receive(dp);
  272. String str2 = new String(dp.getData(), 0,
  273. dp.getLength());
  274. System.out.println("Server: " + str2);
  275. }
  276. }
  277. }
  278.  
  279. Output UDP Chat Client
  280. C:\IPLAB>javac UDPClient.java
  281. C:\IPLAB>java UDPClient
  282. Client is Running... Type ‘STOP’ to Quit
  283. Hello
  284. Server: Welcome
  285. STOP
  286. Terminated...
  287. BB / REC - 41
  288.  
  289.  
  290.  
  291.                                           or
  292.  
  293.  
  294.  Client interface:
  295.  
  296.     import java.awt.*;
  297.     import javax.swing.*;
  298.     public class UDPClient extends JFrame
  299.     {  
  300.         // Variables
  301.         private JFrame frame;
  302.         private JPanel panel;
  303.         private JLabel label;
  304.         private JButton sendbutton;
  305.         private JTextField textfield;
  306.         private JTextArea textarea;
  307.         private JScrollPane scrollpane;
  308.            
  309.         public static void main (String args[]) {
  310.            
  311.             new UDPClient();           
  312.         }
  313.        
  314.         // Constructor
  315.         public UDPClient() {   
  316.        
  317.             frame = this;
  318.             panel = new JPanel(new GridBagLayout());
  319.             panel.setBackground(Color.cyan);
  320.             frame.setTitle("Chat Applet Client");
  321.             frame.getContentPane().add(panel, BorderLayout.NORTH);
  322.             frame.setVisible(true);
  323.             frame.setSize(430, 364);
  324.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  325.             //frame.setResizable(false);       
  326.             GridBagConstraints c = new GridBagConstraints();       
  327.             c.insets = new Insets(5, 5, 5, 5);
  328.            
  329.             // Server address label
  330.             label = new JLabel("Server:");
  331.             c.fill = GridBagConstraints.HORIZONTAL;
  332.             c.gridx = 0;
  333.             c.gridy = 0;       
  334.             panel.add(label, c);
  335.            
  336.             // Server address textfield
  337.             textfield = new JTextField(20);
  338.             c.fill = GridBagConstraints.HORIZONTAL;
  339.             c.gridx = 1;
  340.             c.gridy = 0;
  341.             panel.add(textfield, c);
  342.            
  343.             // 'Port#:' label
  344.             label = new JLabel("Port# :");
  345.             c.fill = GridBagConstraints.HORIZONTAL;
  346.             c.gridx = 2;
  347.             c.gridy = 0;
  348.             panel.add(label, c);
  349.            
  350.             // Port# textfield
  351.             textfield = new JTextField(6);
  352.             c.fill = GridBagConstraints.HORIZONTAL;
  353.             c.gridx = 3;
  354.             c.gridy = 0;
  355.             panel.add(textfield, c);
  356.            
  357.             // 'Conversation:' label
  358.             label = new JLabel("Conversation:");
  359.             c.fill = GridBagConstraints.HORIZONTAL;
  360.             c.gridx = 0;
  361.             c.gridy = 1;
  362.             c.gridwidth = 4;
  363.             panel.add(label, c);       
  364.            
  365.             // Conversation Window
  366.             textarea = new JTextArea(10, 2);
  367.             scrollpane = new JScrollPane(textarea);
  368.             textarea.setLineWrap(true);
  369.             textarea.setWrapStyleWord(true);
  370.             textarea.setEditable(false);
  371.             c.fill = GridBagConstraints.HORIZONTAL;
  372.             c.gridx = 0;
  373.             c.gridy = 2;
  374.             c.gridwidth = 4;
  375.             panel.add(scrollpane, c);
  376.            
  377.             // 'Message:' label
  378.             label = new JLabel("Message to Send:");
  379.             c.fill = GridBagConstraints.HORIZONTAL;
  380.             c.gridx = 0;
  381.             c.gridy = 3;
  382.             panel.add(label, c);
  383.            
  384.             // Message Window
  385.             textarea = new JTextArea(2, 2);
  386.             scrollpane = new JScrollPane(textarea);
  387.             textarea.setLineWrap(true);
  388.             textarea.setWrapStyleWord(true);
  389.             c.fill = GridBagConstraints.HORIZONTAL;
  390.             c.gridx = 0;
  391.             c.gridy = 4;
  392.             c.gridwidth = 4;       
  393.             panel.add(scrollpane, c);
  394.            
  395.             // 'Send' button
  396.             sendbutton = new JButton("Send");
  397.             c.fill = GridBagConstraints.HORIZONTAL;
  398.             c.gridx = 0;
  399.             c.gridy = 5;
  400.             c.gridwidth = 4;
  401.             panel.add(sendbutton, c);      
  402.         }  
  403.     }
  404.  
  405.  
  406.  
  407.  
  408.  
  409. Server interface:
  410.  
  411.     import java.awt.*;
  412.     import javax.swing.*;
  413.     public class UDPServer extends JFrame {
  414.         // Variables
  415.         private JFrame frame;
  416.         private JPanel panel;
  417.         private JLabel label;
  418.         private JButton startbutton;
  419.         private JButton stopbutton;
  420.         private JButton sendbutton;
  421.         private JTextField textfield;
  422.         private JTextArea textarea;
  423.         private JScrollPane scrollpane;
  424.        
  425.         //http://www.youtube.com/watch?v=IkEz5tW5bok
  426.         public static void main (String args[]) {
  427.            
  428.             new UDPServer();       
  429.         }  
  430.        
  431.         // Constructor
  432.         public UDPServer() {
  433.            
  434.             frame = this;
  435.             panel = new JPanel(new GridBagLayout());
  436.             panel.setBackground(Color.darkGray);
  437.             frame.setTitle("Chat Applet Server");
  438.             frame.getContentPane().add(panel, BorderLayout.NORTH);
  439.             frame.setVisible(true);
  440.             //frame.pack();
  441.            
  442.             frame.setSize(430, 364);
  443.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  444.             frame.setResizable(true);      
  445.             GridBagConstraints c = new GridBagConstraints();       
  446.             c.insets = new Insets(5, 5, 5, 5); 
  447.            
  448.             // 'Start Server' button
  449.             startbutton = new JButton("Start Server");
  450.             startbutton.setPreferredSize(new Dimension(130,20));
  451.             c.fill = GridBagConstraints.HORIZONTAL;
  452.             c.gridx = 0;
  453.             c.gridy = 0;
  454.             c.gridwidth = 1;
  455.             panel.add(startbutton, c);
  456.            
  457.             // 'Stop Server' button    
  458.             stopbutton = new JButton("Stop Server");
  459.             stopbutton.setPreferredSize(new Dimension(130,20));
  460.             c.fill = GridBagConstraints.HORIZONTAL;
  461.             c.gridx = 1;
  462.             c.gridy = 0;
  463.             c.gridwidth = 1;
  464.             panel.add(stopbutton, c);
  465.            
  466.             // 'Port#:' label
  467.             label = new JLabel("Port# :");
  468.             label.setForeground(Color.white);
  469.             c.fill = GridBagConstraints.HORIZONTAL;
  470.             c.gridx = 2;
  471.             c.gridy = 0;
  472.             panel.add(label, c);
  473.            
  474.             // Port# textfield
  475.             textfield = new JTextField(6);
  476.             c.fill = GridBagConstraints.HORIZONTAL;
  477.             c.gridx = 3;
  478.             c.gridy = 0;
  479.             panel.add(textfield, c);
  480.            
  481.             // 'Conversation:' label
  482.             label = new JLabel("Conversation:");
  483.             label.setForeground(Color.white);
  484.             c.fill = GridBagConstraints.HORIZONTAL;
  485.             c.gridx = 0;
  486.             c.gridy = 1;
  487.             c.gridwidth = 4;
  488.             panel.add(label, c);       
  489.            
  490.             // Conversation Window
  491.             textarea = new JTextArea("<Server not yet started!>", 10, 2);
  492.             scrollpane = new JScrollPane(textarea);
  493.             textarea.setLineWrap(true);
  494.             textarea.setWrapStyleWord(true);
  495.             textarea.setEditable(false);
  496.             c.fill = GridBagConstraints.HORIZONTAL;
  497.             c.gridx = 0;
  498.             c.gridy = 2;
  499.             c.gridwidth = 4;
  500.             panel.add(scrollpane, c);
  501.            
  502.             // 'Message:' label
  503.             label = new JLabel("Message to Send:");
  504.             label.setForeground(Color.white);
  505.             c.fill = GridBagConstraints.HORIZONTAL;
  506.             c.gridx = 0;
  507.             c.gridy = 3;
  508.             panel.add(label, c);
  509.            
  510.             // Message Window
  511.             textarea = new JTextArea(2, 2);
  512.             scrollpane = new JScrollPane(textarea);
  513.             textarea.setLineWrap(true);
  514.             textarea.setWrapStyleWord(true);
  515.             c.fill = GridBagConstraints.HORIZONTAL;
  516.             c.gridx = 0;
  517.             c.gridy = 4;
  518.             c.gridwidth = 4;       
  519.             panel.add(scrollpane, c);
  520.            
  521.             // 'Send' button
  522.             sendbutton = new JButton("Send");
  523.             c.fill = GridBagConstraints.HORIZONTAL;
  524.             c.gridx = 0;
  525.             c.gridy = 5;
  526.             c.gridwidth = 4;
  527.             panel.add(sendbutton, c);
  528.         }      
  529.     }
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536. question 4-----------------------------
  537. UDP Program – DNS CLIENT-SERVER
  538.  
  539. UDP Program (DNS CLIENT-SERVER)
  540.  
  541. AIM : To create a client server program to Domain Name System using the UDP protocol client server.
  542.  
  543. ALGORITHM
  544.  
  545. Server
  546.  
  547.     Declare the necessary arrays and variables.
  548.     Set server port address using socket().
  549.     Get the current message.
  550.     Connect to the client.
  551.     Stop the process.
  552.  
  553. Client
  554.  
  555.     Set the client machine address.
  556.     Connect to the server.
  557.     Read from the server the current message.
  558.     Display the current message.
  559.     Close the connection.
  560.  
  561. PROGRAM:
  562.  
  563. UDPclient
  564.  
  565. import java .io.*;
  566.  
  567. import java.net.*;
  568.  
  569. classUDPclient
  570.  
  571. {
  572.  
  573. public static DatagramSocket ds;
  574.  
  575. public static intclientport=789,serverport=790;
  576.  
  577. public static void main(String args[])throws Exception
  578.  
  579. {
  580.  
  581. byte buffer[]=new byte[1024];
  582.  
  583. ds=new DatagramSocket(serverport);
  584.  
  585. BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
  586.  
  587. System.out.println(“server waiting”);
  588.  
  589. InetAddressia=InetAddress.getLocalHost();
  590.  
  591. while(true)
  592.  
  593. {
  594.  
  595. System.out.println(“Client:);
  596.  
  597. String str=dis.readLine();
  598.  
  599. if(str.equals(“end”))
  600.  
  601. break;
  602.  
  603. buffer=str.getBytes();
  604.  
  605. ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
  606.  
  607. DatagramPacket p=new DatagramPacket(buffer,buffer.length);
  608.  
  609. ds.receive(p);
  610.  
  611. String psx=new String(p.getData(),0,p.getLength());
  612.  
  613. System.out.println(“Server:+ psx);
  614.  
  615. }
  616.  
  617. }
  618.  
  619. }
  620.  
  621. UDP server
  622.  
  623. import java.io.*;
  624.  
  625. import java.net.*;
  626.  
  627. classUDPserver
  628.  
  629. {
  630.  
  631. public static DatagramSocket ds;
  632.  
  633. public static byte buffer[]=new byte[1024];
  634.  
  635. public static intclientport=789,serverport=790;
  636.  
  637. public static void main(String args[])throws Exception
  638.  
  639. {
  640.  
  641. ds=new DatagramSocket(clientport);
  642.  
  643. System.out.println(“press ctrl+c to quit the program”);
  644.  
  645. BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
  646.  
  647. InetAddressia=InetAddress.getLocalHost();
  648.  
  649. while(true)
  650.  
  651. {
  652.  
  653. DatagramPacket p=new DatagramPacket(buffer,buffer.length);
  654.  
  655. ds.receive(p);
  656.  
  657. String psx=new String(p.getData(),0,p.getLength());
  658.  
  659. System.out.println(“Client:+ psx);
  660.  
  661. InetAddressib=InetAddress.getByName(psx);
  662.  
  663. System.out.println(“Server output:+ib);
  664.  
  665. String str=dis.readLine();
  666.  
  667. if(str.equals(“end”))
  668.  
  669. break;
  670.  
  671. buffer=str.getBytes();
  672.  
  673. ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
  674.  
  675. }
  676.  
  677. }
  678.  
  679. }
  680.  
  681. OUTPUT:
  682.  
  683. UDPclient
  684.  
  685. C:\Program Files\Java\jdk1.6.0\bin>javac UDPclient.java
  686.  
  687. C:\Program Files\Java\jdk1.6.0\bin>java UDPclient
  688.  
  689. Server waiting
  690.  
  691. Client:www.yahoo.com
  692.  
  693. UDPserver
  694.  
  695. C:\Program Files\Java\jdk1.6.0\bin>javac UDPserver.java
  696.  
  697. C:\Program Files\Java\jdk1.6.0\bin>java UDPserver
  698.  
  699. Press ctrl+c to quit the program
  700.  
  701. Client:www.yahoo.com
  702.  
  703. Server output:www.yahoo.com/106.10.170.115
  704.  
  705. RESULT:
  706.  
  707. Thus client server program to Domain Name System using the UDP protocol client server has been executed and verified successfully.
  708.  
  709.  
  710.  
  711.  
  712.  
  713. QUESTION 5------------------------------------------------
  714. UDP DATE SERVER
  715.  
  716. Server Program >>>>> Server.java
  717.  
  718.  
  719. import java.net.*;
  720. import java.io.*;
  721. import java.util.*;
  722.  
  723. public class Server {
  724.  
  725. public static void main(String[] args) throws Exception{
  726.  
  727. DatagramSocket ss=new DatagramSocket(1234);
  728.  
  729. while(true){
  730.  
  731. System.out.println("Server is up....");
  732.  
  733. byte[] rd=new byte[100];
  734. byte[] sd=new byte[100];
  735.  
  736. DatagramPacket rp=new DatagramPacket(rd,rd.length);
  737.  
  738. ss.receive(rp);
  739.  
  740. InetAddress ip= rp.getAddress();
  741.  
  742. int port=rp.getPort();
  743.  
  744. Date d=new Date();   // getting system time
  745.  
  746. String time= d + "";  // converting it to String
  747.  
  748. sd=time.getBytes();   // converting that String to byte
  749.  
  750. DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port);
  751.  
  752. ss.send(sp);
  753.  
  754. rp=null;
  755.  
  756. System.out.println("Done !! ");
  757.  
  758. }
  759.  
  760. }
  761.  
  762. }
  763.  
  764.  
  765. Client program >>>>>>>>> Clientnew.java
  766. import java.net.*;
  767. import java.io.*;
  768.  
  769. public class Clientnew {
  770.  
  771. public static void main(String[] args) throws Exception{
  772.  
  773.     System.out.println("Server Time >>>>");
  774.  
  775.     DatagramSocket cs=new DatagramSocket();
  776.  
  777.     InetAddress ip=InetAddress.getByName("localhost");
  778.  
  779.     byte[] rd=new byte[100];
  780.     byte[] sd=new byte[100];
  781.  
  782.     DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,1234);
  783.  
  784.     DatagramPacket rp=new DatagramPacket(rd,rd.length);
  785.  
  786.     cs.send(sp);
  787.  
  788.     cs.receive(rp);
  789.  
  790.     String time=new String(rp.getData());
  791.  
  792.     System.out.println(time);
  793.  
  794.     cs.close();
  795.  
  796. }
  797.  
  798. }
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  809. SIR UPLOADED MATERIAL
  810.  
  811.  
  812. 1)Write a program to list all ports hosting a TCP server in a specified host.
  813.  
  814. import java.net.*;
  815. import java.io.*;
  816. public class ports{
  817. public static void main(String args[])
  818. {
  819. for(int i=1;i<1024;i++)
  820. {
  821. try{
  822. Socket s=new Socket("127.0.0.1",i);
  823. System.out.println("There is server on port"+i+"of 127.0.0.1");
  824. }
  825. catch(UnknownHostException e){
  826. System.err.println(e);
  827. break;
  828. }
  829. catch(IOException e){
  830. //must not be server on this port
  831. }
  832. }
  833. }
  834. }
  835.  
  836. 2)Write a program to display the server’s date and time details at the client end.
  837.  
  838. import java.io.IOException;
  839. import java.io.PrintWriter;
  840. import java.net.ServerSocket;
  841. import java.net.Socket;
  842. import java.util.Date;
  843.  
  844. public class dateserver {
  845.  
  846.     public static void main(String[] args) throws IOException {
  847.         ServerSocket listener = new ServerSocket(9090);
  848.         try {
  849.             while (true) {
  850.                 Socket socket = listener.accept();
  851.                 try {
  852.                     PrintWriter out =
  853.                         new PrintWriter(socket.getOutputStream(), true);
  854.                     out.println(new Date().toString());
  855.                 } finally {
  856.                     socket.close();
  857.                 }
  858.             }
  859.         }
  860.         finally {
  861.             listener.close();
  862.         }
  863.     }
  864. }
  865.  
  866. ------------------------------
  867. import java.io.*;
  868. import java.net.Socket;
  869.  
  870. public class dateClient {
  871.  
  872.     public static void main(String[] args) throws IOException {
  873.         Socket s = new Socket("127.0.0.1", 9090);
  874.         BufferedReader input =
  875.             new BufferedReader(new InputStreamReader(s.getInputStream()));
  876.         String answer = input.readLine();
  877.         System.out.println("The date and time details are "+answer  );
  878.    
  879.         System.exit(0);
  880.     }
  881. }
  882.  
  883.  
  884. 3)Write a program to display the client’s address at the server end.
  885.  
  886.  
  887. import java.io.*;
  888. import java.net.*;
  889.  
  890. public class ServerAddr
  891. {
  892. public static void main(String args[]) throws IOException
  893. {
  894. try
  895. {
  896. ServerSocket ss = new ServerSocket(6666);
  897. System.out.println("Waiting for client.....");
  898.  
  899. Socket s = ss.accept();
  900. System.out.println("Connected to client....");
  901.  
  902. DataInputStream in = new DataInputStream (s.getInputStream());
  903. String line = null;
  904. line = in.readUTF();
  905. System.out.println("Client's IP adress: " + line);
  906. }
  907. catch (Exception e)
  908. {
  909. e.printStackTrace();
  910. }
  911. }
  912. }
  913.  
  914. --------------------------------
  915.  
  916. import java.io.*;
  917. import java.net.*;
  918.  
  919. public class ClientAddr
  920. {
  921. public static void main(String args[]) throws IOException
  922. {
  923. try
  924. {
  925. InetAddress ipaddress = InetAddress.getByName("");
  926. Socket s = new Socket(ipaddress,6666);
  927.  
  928. System.out.println("Connected to the server...");
  929. DataOutputStream out = new DataOutputStream(s.getOutputStream());
  930.  
  931. String line = null;
  932. System.out.println("Sending my IP Address to server...");
  933.  
  934. line=ipaddress.getHostAddress();
  935.        
  936. out.writeUTF(line);
  937. out.flush();}
  938. catch (Exception e)
  939. {
  940. e.printStackTrace();
  941. }
  942. }
  943. }
  944.  
  945. 4)Implement a simple message transfer from client to server process using TCP/IP.
  946.  
  947. import java.io.*;
  948. import java.net.*;
  949.  
  950. public class Tcpserver
  951. {
  952. public static void main(String args[]) throws IOException
  953. {
  954. try
  955. {
  956. ServerSocket ss = new ServerSocket(6666);
  957. System.out.println("Waiting for client.....");
  958.  
  959. Socket s = ss.accept();
  960. System.out.println("Connected to client....");
  961.  
  962. DataInputStream in = new DataInputStream (s.getInputStream());
  963. String line = null;
  964. line = in.readUTF();
  965. System.out.println("Mesage from Client:" + line);
  966. }
  967. catch (Exception e)
  968. {
  969. e.printStackTrace();
  970. }
  971. }
  972. }
  973.  
  974. --------------------------------------
  975. import java.io.*;
  976. import java.net.*;
  977.  
  978. public class TCPClient
  979. {
  980. public static void main(String args[]) throws IOException
  981. {
  982. try
  983. {
  984. InetAddress ipaddress = InetAddress.getByName("");
  985. Socket s = new Socket(ipaddress,6666);
  986. DataInputStream read = new DataInputStream(System.in);
  987. System.out.println("Connected to the server...");
  988. DataOutputStream out = new DataOutputStream(s.getOutputStream());
  989.  
  990. String line = null;
  991. System.out.println("Write a message to the server..");
  992.  
  993. line=read.readLine();
  994. out.writeUTF(line);
  995. out.flush();}
  996. catch (Exception e)
  997. {
  998. e.printStackTrace();
  999. }
  1000. }
  1001. }
  1002.  
  1003. 5)Develop a TCP client/server application for transferring a text file from client to server
  1004.  
  1005.  
  1006. CLIENt:
  1007. import java.io.*;
  1008.  import java.net.*;
  1009. import java.util.*;
  1010. public class FTPClient {
  1011. public static void main(String args[]) throws Exception
  1012.  
  1013. {
  1014.  Socket ss = new Socket("localhost",5000);
  1015. while(true)
  1016. {
  1017.  
  1018. Scanner pbn = new Scanner(System.in);
  1019. System.out.println("Enter the path of the file ");
  1020. String path = pbn.nextLine();
  1021. System.out.println(path);
  1022.  
  1023. File f = new File(path);
  1024. FileInputStream fis = new FileInputStream(f);
  1025.  
  1026. BufferedInputStream bis = new BufferedInputStream(fis);
  1027. System.out.println("Sending file...");
  1028.  
  1029. System.out.println("File sent");
  1030.  
  1031.     }
  1032.  
  1033.   }
  1034. }
  1035. server:
  1036.  
  1037. //////////////////
  1038. import java.io.*;
  1039. import java.net.*;
  1040. import java.util.*;
  1041. public class FTPServer {
  1042. public static void main(String args[]) throws IOException
  1043. {ServerSocket ss = new ServerSocket(5000);
  1044.    
  1045.     Scanner pbn = new Scanner(System.in);
  1046.     boolean flag = true;
  1047.     while(flag)
  1048.     {
  1049.     flag = false;
  1050.     try
  1051.     {System.out.println("waiting...");
  1052. Socket s = ss.accept();
  1053. System.out.println("Accepted connection "+s);
  1054.             System.out.println("Enter the path where you want to store the file");
  1055.             String path1 = pbn.nextLine();
  1056.             FileOutputStream fos = new FileOutputStream(path1);
  1057.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  1058.             InputStream is = s.getInputStream();
  1059.             byte[] b = new byte[600000];
  1060.             int n = 0;
  1061.             int o = 0;
  1062.             while((n=is.read(b,o,b.length-o))>=0)
  1063.             {
  1064.                 o+=n;
  1065.             }
  1066.             bos.write(b,0,o);
  1067.  
  1068.             bos.flush();
  1069.               System.out.println("File Received");
  1070.            }
  1071.  
  1072.     catch(FileNotFoundException f)
  1073.     {
  1074.             flag = true;
  1075.             String msg = f.getMessage();
  1076.             System.out.println("Error Message:"+msg);
  1077.             System.out.println("Please Enter a correct file path");
  1078.     }
  1079.      }
  1080.  
  1081.     }
  1082. }
  1083.  
  1084.  
  1085. 6. 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.
  1086.  
  1087. import java.io.*;
  1088. import java.net.*;
  1089.  
  1090. public class AuthServer
  1091. {
  1092.     public static void main(String args[]) throws IOException
  1093.     {
  1094.         try
  1095.         {
  1096.             ServerSocket ss = new ServerSocket(6666);
  1097.             System.out.println("Waiting for client.....");
  1098.             Socket s = ss.accept();
  1099.             System.out.println("Connected to client....");
  1100.  
  1101.             DataInputStream in = new DataInputStream (s.getInputStream());
  1102.             DataOutputStream out = new DataOutputStream (s.getOutputStream());
  1103.  
  1104.             String line = null;
  1105.             String line1 = null;
  1106.             String sendline = null;
  1107.  
  1108.  
  1109. line = in.readUTF();
  1110. line1 = in.readUTF();
  1111.                 if((line.equals("aid")|| line.equals("bid"))&&((line1.equals("apass")|| line1.equals("bpass"))))
  1112.                 {
  1113.                     sendline ="Valid user id and password.Successfully logged in !!!!";
  1114.                 out.writeUTF(sendline);
  1115.                 out.flush();
  1116.  
  1117.                 }
  1118.                 else
  1119.  
  1120.                 {
  1121.     sendline ="Invalid details";
  1122.                 out.writeUTF(sendline);
  1123.                 out.flush();
  1124.                 }
  1125.  
  1126.         }
  1127.         catch (Exception e)
  1128.         {
  1129.             e.printStackTrace();
  1130.         }
  1131.     }
  1132. }
  1133.  
  1134. import java.io.*;
  1135. import java.net.*;
  1136.  
  1137. public class AuthClient
  1138. {
  1139.     public static void main(String args[]) throws IOException
  1140.     {
  1141.         try
  1142.         {
  1143.             InetAddress ipaddress = InetAddress.getByName("127.0.0.1");
  1144.             Socket s = new Socket(ipaddress,6666);
  1145.  
  1146.             System.out.println("Connected to the server...");
  1147.  
  1148.             DataInputStream read = new DataInputStream(System.in);
  1149.  
  1150.             DataInputStream in = new DataInputStream(s.getInputStream());
  1151.             DataOutputStream out = new DataOutputStream(s.getOutputStream());
  1152.  
  1153.             String line = null;
  1154.             String line1 = null;
  1155.             String receiveline = null;
  1156.  
  1157.             System.out.println("Enter User id ");
  1158.             line = read.readLine();
  1159.                 out.writeUTF(line);
  1160.                 out.flush();
  1161.  
  1162. System.out.println("Enter Password ");
  1163. line1 = read.readLine();
  1164. out.writeUTF(line1);
  1165. out.flush();
  1166.  
  1167.  
  1168.                 receiveline = in.readUTF();
  1169.                 System.out.println("SERVER: " + receiveline);
  1170.  
  1171.  
  1172.         }
  1173.         catch (Exception e)
  1174.         {
  1175.             e.printStackTrace();
  1176.         }
  1177.     }
  1178. }
  1179.  
  1180. 7. Write a program to develop a simple (text based) Chat application using TCP/IP.
  1181. //chat
  1182. import java.io.*;
  1183. import java.net.*;
  1184.  
  1185. public class TCPserver
  1186. {
  1187.     public static void main(String args[]) throws IOException
  1188.     {
  1189.         try
  1190.         {
  1191.             ServerSocket ss = new ServerSocket(6666);
  1192.             System.out.println("Waiting for client.....");
  1193. DataInputStream read = new DataInputStream(System.in);
  1194.             Socket s = ss.accept();
  1195.             System.out.println("Connected to client....");
  1196.  
  1197.             DataInputStream in = new DataInputStream (s.getInputStream());
  1198.             DataOutputStream out = new DataOutputStream (s.getOutputStream());
  1199.  
  1200.             String line = null;
  1201.            
  1202.  
  1203.             do
  1204.             {
  1205.                 line = in.readUTF();
  1206.                 System.out.println("CLIENT: " + line);
  1207.                 line = read.readLine();
  1208.                 out.writeUTF(line);
  1209.                 out.flush();
  1210.  
  1211.                 System.out.println("Waiting for the next line.....");
  1212.             }while(!line.equals("bye"));
  1213.         }
  1214.         catch (Exception e)
  1215.         {
  1216.             e.printStackTrace();
  1217.         }
  1218.     }
  1219. }
  1220.  
  1221.  
  1222. //chat
  1223. import java.io.*;
  1224. import java.net.*;
  1225.  
  1226. public class TCPClient
  1227. {
  1228.     public static void main(String args[]) throws IOException
  1229.     {
  1230.         try
  1231.         {
  1232.             InetAddress ipaddress = InetAddress.getByName("127.0.0.1");
  1233.             Socket s = new Socket(ipaddress,6666);
  1234.  
  1235.             System.out.println("Connected to the server...");
  1236.  
  1237.             DataInputStream read = new DataInputStream(System.in);
  1238.  
  1239.             DataInputStream in = new DataInputStream(s.getInputStream());
  1240.             DataOutputStream out = new DataOutputStream(s.getOutputStream());
  1241.  
  1242.             String line = null;
  1243.             String receiveline = null;
  1244.  
  1245.             System.out.println("Enter data to send to the server: ");
  1246.  
  1247.             do
  1248.             {
  1249.                 System.out.print("CLIENT: ");
  1250.                 line = read.readLine();
  1251.                 out.writeUTF(line);
  1252.                 out.flush();
  1253.  
  1254.                 receiveline = in.readUTF();
  1255.                 System.out.println("SERVER: " + receiveline);
  1256.  
  1257.             }while(!line.equals("bye"));
  1258.         }
  1259.         catch (Exception e)
  1260.         {
  1261.             e.printStackTrace();
  1262.         }
  1263.     }
  1264. }
  1265.  
  1266. 8. Implement a simple message transfer from client to server process using UDP.
  1267. import java.io.*;
  1268. import java.net.*;
  1269. class UDPServerss {
  1270. public static void main(String args[]) throws Exception       {
  1271.      DatagramSocket serverSocket = new DatagramSocket(9876);
  1272.  
  1273.    byte[] receiveData = new byte[1024];
  1274.  byte[] sendData = new byte[1024];
  1275.     while(true)                {
  1276.  
  1277.               DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1278.               serverSocket.receive(receivePacket);
  1279.                   String sentence = new String( receivePacket.getData());
  1280.                   System.out.println("RECEIVED: " + sentence);
  1281.       InetAddress IPAddress = receivePacket.getAddress();
  1282.             int port = receivePacket.getPort();
  1283.   String capitalizedSentence = sentence.toUpperCase();
  1284.          sendData = capitalizedSentence.getBytes();
  1285.       DatagramPacket sendPacket =                   new DatagramPacket(sendData, sendData.length, IPAddress, port);
  1286.         serverSocket.send(sendPacket);
  1287.            }
  1288. }
  1289. }
  1290. import java.io.*;
  1291. import java.net.*;
  1292.  class UDPClientssss {
  1293.    public static void main(String args[]) throws Exception    {
  1294.       BufferedReader inFromUser =          new BufferedReader(new InputStreamReader(System.in));
  1295.       DatagramSocket clientSocket = new DatagramSocket();
  1296.  InetAddress IPAddress = InetAddress.getByName("localhost");
  1297.       byte[] sendData = new byte[1024];
  1298.     byte[] receiveData = new byte[1024];
  1299.      String sentence = inFromUser.readLine();
  1300.      sendData = sentence.getBytes();
  1301.   DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
  1302.   clientSocket.send(sendPacket);
  1303.   DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  1304.   clientSocket.receive(receivePacket);
  1305.  String modifiedSentence = new String(receivePacket.getData());
  1306.       System.out.println("FROM SERVER:" + modifiedSentence);
  1307.   clientSocket.close();
  1308.    }
  1309.  }
  1310.  
  1311. 9. Write a program to implement an Echo UDP server. Test the working of the server by writing a client application.
  1312. import java.net.*;
  1313.  import java.util.*;
  1314.  public class EchoServer
  1315. {
  1316.      public static void main( String args[]) throws Exception
  1317.  {
  1318.          DatagramSocket dsock = new DatagramSocket(7);
  1319. byte arr1[] = new byte[150];
  1320. DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
  1321. while(true)
  1322. { dsock.receive(dpack);
  1323. byte arr2[] = dpack.getData();
  1324.  int packSize = dpack.getLength();
  1325. String s2 = new String(arr2, 0, packSize);
  1326. System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
  1327. dsock.send(dpack);
  1328. }
  1329. }
  1330. }----------------------------------
  1331. import java.net.*;
  1332. import java.util.*;
  1333.  public class EchoClient
  1334. { public static void main( String args[] ) throws Exception {
  1335. InetAddress add = InetAddress.getByName("127.0.0.1");
  1336. DatagramSocket dsock = new DatagramSocket( );
  1337.  String message1 = "This is client calling";
  1338.  byte arr[] = message1.getBytes( );
  1339.  DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
  1340.  dsock.send(dpack); // send the packet
  1341. Date sendTime = new Date( ); // note the time of sending the message
  1342. dsock.receive(dpack); // receive the packet
  1343. String message2 = new String(dpack.getData( ));
  1344.  Date receiveTime = new Date( ); // note the time of receiving the message
  1345.  System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
  1346.   }
  1347.  }
  1348.  
  1349. 10. Write a program to implement the DISCARD server using UDP. Test the working of the server by writing a client application. Let the server log the client details on its standard output.
  1350. import java.io.*;
  1351. import java.net.*;
  1352. public class UDPdiscardServer {
  1353.     public final static int DEFAULT_PORT=9;
  1354.     public final static int MAX_PACKET_SIZE=65507;
  1355. public static void main(String args[])
  1356. {
  1357.     int port=DEFAULT_PORT;
  1358.     byte[] buffer=new byte[MAX_PACKET_SIZE];
  1359.  
  1360.     try{
  1361.     port=9;
  1362.     }
  1363. catch(Exception ex)
  1364. {}
  1365.     try{
  1366.     DatagramSocket server=new DatagramSocket(port);
  1367. DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
  1368.  
  1369. while(true)
  1370. {
  1371.     try{
  1372.     server.receive(packet);
  1373.     String s=new String(packet.getData(),0,packet.getLength(),"UTF-8");
  1374.     System.out.println(packet.getAddress()+" at port "+packet.getPort()+" says "+s);
  1375.     packet.setLength(buffer.length);
  1376.     }
  1377.     catch(IOException ex){ System.err.println(ex);}
  1378. }
  1379.  
  1380.     }
  1381.     catch(SocketException ex)
  1382.     {System.err.println(ex);}
  1383. }
  1384. }
  1385. import java.io.*;
  1386. import java.net.*;
  1387. public class UDPdiscardClient {
  1388.     public final static int DEFAULT_PORT=9;
  1389.  
  1390. public static void main(String args[])
  1391. {String hostname="localhost";
  1392.     int port=DEFAULT_PORT;
  1393.  
  1394.  
  1395.    try{
  1396.    InetAddress server=InetAddress.getByName(hostname);
  1397.    BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
  1398.    DatagramSocket theSocket=new DatagramSocket();
  1399.  
  1400.    while(true)
  1401.    {
  1402.   String theLine=userInput.readLine();
  1403.   if(theLine.equals(".")) break;
  1404.   byte[] data=theLine.getBytes("UTF-8");
  1405.   DatagramPacket theOutput=new DatagramPacket(data,data.length,server,port);
  1406.   theSocket.send(theOutput);
  1407.    }
  1408.    }
  1409.    catch(UnknownHostException ex)
  1410.     {System.err.println(ex);}
  1411.     catch(SocketException ex)
  1412.     {System.err.println(ex);}
  1413.     catch(IOException ioex)
  1414.     {System.err.println(ioex);}
  1415. }
  1416. }
  1417. Find the physical address of a host when its logical address is known (ARP protocol) using TCP/IP.
  1418.  
  1419. import java.net.InetAddress;
  1420. import java.net.NetworkInterface;
  1421. import java.net.SocketException;
  1422. import java.net.UnknownHostException;
  1423. import java.util.Scanner;
  1424.  
  1425. public class MacAddress {
  1426.     public static void main(String[] args)
  1427.     {
  1428.         try
  1429.         {
  1430.             Scanner console = new Scanner(System.in);
  1431.             System.out.println("Enter System Name: ");
  1432.             String ipaddr = console.nextLine();
  1433.             InetAddress address = InetAddress.getByName(ipaddr);
  1434.             System.out.println("address = "+address);
  1435.             NetworkInterface ni = NetworkInterface.getByInetAddress(address);
  1436.             if (ni!=null)
  1437.             {
  1438.                 byte[] mac = ni.getHardwareAddress();
  1439.                 if (mac != null)
  1440.                 {
  1441.                     System.out.print("MAC Address : ");
  1442.                     for (int i=0; i<mac.length; i++)
  1443.                     {
  1444.                         System.out.format("%02X%s", mac[i], (i<mac.length - 1) ? "-" :"");
  1445.                     }
  1446.                 }
  1447.                 else
  1448.                 {
  1449.                     System.out.println("Address doesn't exist or is not accessible/");
  1450.  
  1451.                     }
  1452.                 }
  1453.             else
  1454.             {
  1455.                 System.out.println("Network Interface for the specified address is not found");
  1456.             }
  1457.         }
  1458.         catch(UnknownHostException he)
  1459.         {
  1460.         }
  1461.     catch(SocketException e)
  1462.     {
  1463.     }
  1464.     }    
  1465. }
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1477. basics
  1478. Basic network commands
  1479. ping
  1480. The ping command (named after the sound of an active sonar system) sends echo requests to the host
  1481. specified on the command line, and lists the responses received.
  1482. $ ping ipAddress or hostname
  1483. e.g
  1484. $ ping www.vit.ac.in
  1485. • ping - sends an ICMP ECHO_REQUEST packet to the specified host. If the host responds, an
  1486. ICMP packet is received.
  1487. • One can “ping” an IP address to see if a machine is alive.
  1488. • It provides a very quick way to see if a machine is up and connected to the network.
  1489. netstat
  1490. • It works with the LINUX Network Subsystem, it will tell you what the status of ports are ie. open,
  1491. closed, waiting connections. It is used to display the TCP/IP network protocol statistics and
  1492. information.
  1493. tcpdump
  1494. This is a sniffer, a program that captures packets off a network interface and interprets them.
  1495. hostname
  1496. Tells the user the host name of the computer they are logged into.
  1497. traceroute
  1498. traceroute will show the route of a packet. It attempts to list the series of hosts through which your
  1499. packets travel on their way to a given destination.
  1500. Command syntax:
  1501. traceroute machine_name_or_ip
  1502. e.g traceroute www.vit.ac.in
  1503. Each host will be displayed, along with the response times at each host.
  1504. finger
  1505. Retrieves information about the specified user.
  1506. e.g finger bit50001
  1507. ifconfig ( In Windows use ipconfig )
  1508. This command is used to configure network interfaces, or to display their current configuration.
  1509. dig
  1510. The "domain information groper" tool. If you give a hostname as an argument to output information
  1511. about that host, including it's IP address, hostname and various other information.
  1512. e.g dig vitlinux
  1513. telnet
  1514. telnet allows you to log in to a computer, just as if you were sitting at the terminal. Once your
  1515. username and password are verified, you are given a shell prompt. From here, you can do anything
  1516. requiring a text console.
  1517. ftp
  1518. To connect to an FTP server use
  1519. ftp ipaddress
  1520. netstat
  1521. Displays contents of /proc/net files. It works with the LINUX Network Subsystem, it will tell
  1522. you what the status of ports are ie. open, closed, waiting, masquerade connections. It will also
  1523. display various other things. It has many different options.
  1524. tcpdump
  1525. This is a sniffer, a program that captures packets off a network interface and interprets them
  1526. for you. It understands all basic internet protocols, and can be used to save entire packets for
  1527. later inspection.
  1528. ping
  1529. The ping command (named after the sound of an active sonar system) sends echo requests to
  1530. the host you specify on the command line, and lists the responses received their round trip
  1531. time.
  1532. You simply use ping as:
  1533. ping ip_or_host_name
  1534. hostname
  1535. Tells the user the host name of the computer they are logged into. Note: may be called host.
  1536. traceroute
  1537. traceroute will show the route of a packet. It attempts to list the series of hosts through which
  1538. your packets travel on their way to a given destination. Also have a look at xtraceroute (one
  1539. of several graphical equivalents of this program).
  1540. Command syntax:
  1541. traceroute machine_name_or_ip
  1542. tracepath
  1543. tracepath performs a very simlar function to traceroute the main difference is that tracepath
  1544. doesn't take complicated options.
  1545. Command syntax:
  1546. tracepath machine_name_or_ip
  1547. findsmb
  1548. findsmb is used to list info about machines that respond to SMB name queries (for example
  1549. windows based machines sharing their hard disk's).
  1550. Command syntax:
  1551. Findsmb
  1552. This would find all machines possible, you may need to specify a particular subnet to query
  1553. those machines only...
  1554. nmap
  1555. “ network exploration tool and security scanner”. nmap is a very advanced network tool used
  1556. to query machines (local or remote) as to whether they are up and what ports are open on
  1557. these machines.
  1558. A simple usage example:
  1559. nmap machine_name
  1560. This would query your own machine as to what ports it keeps open. nmap is a very powerful
  1561. tool, documentation is available on the nmap site as well as the information in the manual
  1562. page.
  1563. telnet
  1564. Someone once stated that telnet(1) was the coolest thing he had ever seen on computers. The ability
  1565. to remotely log in and do stuff on another computer is what separates Unix and Unix-like operating
  1566. systems from other operating systems.
  1567. telnet allows you to log in to a computer, just as if you were sitting at the terminal. Once your
  1568. username and password are verified, you are given a shell prompt. From here, you can do anything
  1569. requiring a text console. Compose email, read newsgroups, move files around, and so on. If you are
  1570. running X and you telnet to another machine, you can run X programs on the remote computer and
  1571. display them on yours.
  1572. To login to a remote machine, use this syntax:
  1573. % telnet <hostname>
  1574. If the host responds, you will receive a login prompt. Give it your username and password. That's it.
  1575. You are now at a shell. To quit your telnet session, use either the exit command or the logout
  1576. command.
  1577. telnet does not encrypt the information it sends. Everything is sent in plain text, even passwords.
  1578. It is not advisable to use telnet over the Internet. Instead, consider the Secure Shell. It encrypts
  1579. all traffic and is available for free.
  1580. The other use of telnet
  1581. Now that we have convinced you not to use the telnet protocol anymore to log into a remote machine,
  1582. we'll show you a couple of useful ways to use telnet.
  1583. You can also use the telnet command to connect to a host on a certain port.
  1584. % telnet <hostname> [port]
  1585. This can be quite handy when you quickly need to test a certain service, and you need full control
  1586. over the commands, and you need to see what exactly is going on. You can interactively test or use
  1587. an SMTP server, a POP3 server, an HTTP server, etc. this way.
  1588. In the next figure you'll see how you can telnet to a HTTP server on port 80, and get some basic
  1589. information from it.
  1590. Figure 13-1. Telnetting to a webserver
  1591. % telnet store.slackware.com 80
  1592. Trying 69.50.233.153...
  1593. Connected to store.slackware.com.
  1594. Escape character is '^]'.
  1595. HEAD / HTTP/1.0
  1596. HTTP/1.1 200 OK
  1597. Date: Mon, 25 Apr 2005 20:47:01 GMT
  1598. Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d
  1599. Last-Modified: Fri, 18 Apr 2003 10:58:54 GMT
  1600. ETag: "193424-c0-3e9fda6e"
  1601. Accept-Ranges: bytes
  1602. Content-Length: 192
  1603. Connection: close
  1604. Content-Type: text/html
  1605. Connection closed by foreign host.
  1606. %
  1607. 1-)arp :
  1608. When we need an Ethernet (MAC) address we can use arp(address resolution protocol).
  1609. In other words it shows the physical address of an host.
  1610. Example:
  1611. C:\Documents and Settings\sysadm>arp -a
  1612. Interface: 169.254.195.199 --- 0x2
  1613. Internet Address Physical Address Type
  1614. 216.109.127.60 00-53-45-00-00-00 static
  1615. 2-)nslookup:
  1616. Displays information from Domain Name System (DNS) name servers.
  1617. Example:
  1618. C:\Documents and Settings\sysadm>nslookup itu.dk
  1619. Server: ns3.inet.tele.dk
  1620. Address: 193.162.153.164
  1621. Non-authoritative answer:
  1622. Name: itu.dk
  1623. Address: 130.226.133.2
  1624. NOTE :If you write the command as above it shows as default your pc's server name firstly.
  1625. C:\Documents and Settings\sysadm>nslookup mail.yahoo.com itu.dk
  1626. Server: superman.itu.dk
  1627. Address: 130.226.133.2
  1628. Non-authoritative answer:
  1629. Name: login.yahoo.akadns.net
  1630. Address: 216.109.127.60
  1631. Aliases: mail.yahoo.com, login.yahoo.com
  1632. NOTE:Remark that in the second example we do not see the default server name.
  1633. There are many nslookup with optional commands.To read them type nslookup and enter
  1634. then type help and enter.
  1635. 3-)finger:
  1636. Displays the information about a user on the system.
  1637. Example:
  1638. NOTE :I could not find out the name of the server that we log on (windows) at the school.
  1639. Sysadmin does not know that either:o)
  1640. But as an example I tried it on the our unix server.
  1641. [hilmiolgun@ssh hilmiolgun]$ finger
  1642. Login Name Tty Idle Login Time Office Office Phone
  1643. adel Adel Abu-Sharkh pts/1 7 Sep 10 00:11 (cpe.atm2-0-
  1644. 1091080.0x50a0bcb2.albnxx13.customer.tele.dk)
  1645. adel Adel Abu-Sharkh pts/2 9 Sep 9 23:56 (cpe.atm2-0-
  1646. 1091080.0x50a0bcb2.albnxx13.customer.tele.dk)
  1647. hilmiolgun Hilmi Olgun pts/9 Sep 10 00:20 (0x3ef3e2fe.albnxx8.adsl.tele.dk)
  1648. hm Hanne Munkholm pts/6 1:56 Sep 8 21:27 (off180.palombia.dk)
  1649. jcg Jens Christian Godsk pts/4 1d Sep 8 10:28 (toscana.itu.dk)
  1650. kaj Kenneth Ahn Jensen pts/7 Sep 10 00:11 (cpe.atm2-0-
  1651. 54493.0x50a4ad32.boanxx12.customer.tele.dk)
  1652. root root pts/8 1 Sep 10 00:12 (sysadm2.itu.dk)
  1653. troels Troels Arvin pts/5 3:49 Sep 9 20:31 (62.79.119.132.adsl.vbr.worldonline.dk)
  1654. webclaus Claus Bech Rasmussen pts/0 6 Sep 10 00:11 (port967.ds1-khk.adsl.cybercity.dk)
  1655. NOTE :What I did is :I first check the online users,and get a list of them(above).
  1656. Then i just choosed one user to get information about him(below)
  1657. [hilmiolgun@ssh hilmiolgun]$ finger hm
  1658. Login: hm Name: Hanne Munkholm
  1659. Directory: /import/home/hm Shell: /bin/bash
  1660. On since Mon Sep 8 21:27 (CEST) on pts/6 from off180.palombia.dk
  1661. 1 hour 56 minutes idle
  1662. Last login Tue Sep 9 11:05 (CEST) on pts/12 from stud127.itu.dk
  1663. New mail received Mon Nov 11 23:01 2002 (CET)
  1664. Unread since Sat Oct 5 00:00 2002 (CEST)
  1665. Plan:
  1666. World Domination... fast.
  1667. [hilmiolgun@ssh hilmiolgun]$
  1668. 4-)ping:
  1669. Simpy shows if the remote machine is available or not....
  1670. Example:
  1671. C:\Documents and Settings\sysadm>ping webmail.itu.dk
  1672. Pinging tarzan.itu.dk [130.226.133.3] with 32 bytes of data:
  1673. Reply from 130.226.133.3: bytes=32 time=29ms TTL=55
  1674. Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
  1675. Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
  1676. Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
  1677. Ping statistics for 130.226.133.3:
  1678. Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  1679. Approximate round trip times in milli-seconds:
  1680. Minimum = 29ms, Maximum = 30ms, Average = 29ms
  1681. NOTE :Remark that the remote machine is replying.Otherwise the output will be "Request time out"
  1682. which means the
  1683. remote machine is not working well.(Not answering)
  1684. 5-)tracert:
  1685. It simply shows the path between source and destination address.
  1686. Example:
  1687. C:\Documents and Settings\sysadm>tracert webmail.itu.dk
  1688. Tracing route to tarzan.itu.dk [130.226.133.3]
  1689. over a maximum of 30 hops:
  1690. 1 * * * Request timed out.
  1691. 2 29 ms 19 ms 29 ms ge-0-2-1-2.1000M.albnxu1.ip.tele.dk [195.249.1.2 9]
  1692. 3 29 ms 29 ms 19 ms pos1-0.622M.lynxg1.ip.tele.dk [195.249.2.46]
  1693. 4 29 ms 19 ms 29 ms herman.fsknet.lyngby.forskningsnettet.dk [192.38 .7.1]
  1694. 5 29 ms 29 ms 19 ms 130.225.244.214
  1695. 6 29 ms 29 ms 29 ms 1.ku.forskningsnettet.dk [130.225.245.90]
  1696. 7 29 ms 29 ms 29 ms rk.itu.forskningsnettet.dk [130.226.249.30]
  1697. 8 29 ms 29 ms 29 ms 130.225.245.86
  1698. 9 29 ms 29 ms 29 ms tarzan.itu.dk [130.226.133.3]
  1699. Trace complete.
  1700. 6-)ftp:
  1701. For file transferring..(File transfer protocol)
  1702. Example:Lets you dont have an ftp software and you want to get a file from your school harddisk.
  1703. So to do that:
  1704. C:\Documents and Settings\sysadm>ftp
  1705. ftp> open
  1706. To ftp.itu.dk
  1707. Connected to ssh.itu.dk.
  1708. 220 ProFTPD 1.2.8rc2 Server (ProFTPD Default Installation) [ssh.it-c.dk]
  1709. NOTE:What am I doing is simply:typing them one-by-one(after each typing remember to enter)
  1710. ftp,open,ftp.itu.dk
  1711. User (ssh.itu.dk:(none)): hilmiolgun
  1712. 331 Password required for hilmiolgun.
  1713. Password:
  1714. 230 User hilmiolgun logged in.
  1715. NOTE:The server will require username and password..
  1716. ftp> help
  1717. Commands may be abbreviated. Commands are:
  1718. ! delete literal prompt send
  1719. ? debug ls put status
  1720. append dir mdelete pwd trace
  1721. ascii disconnect mdir quit type
  1722. bell get mget quote user
  1723. binary glob mkdir recv verbose
  1724. bye hash mls remotehelp
  1725. cd help mput rename
  1726. close lcd open rmdir
  1727. ftp> help dir
  1728. dir List contents of remote directory
  1729. NOTE: If it is your first time to those commands just type help and get the commands.If you dont
  1730. know how to use
  1731. them type help commandname..
  1732. ftp> dir
  1733. 200 PORT command successful
  1734. 150 Opening ASCII mode data connection for file list
  1735. drwx------ 4 hilmiolgun hilmiolgun 155 Jul 1 14:02 Desktop
  1736. drwx------ 2 hilmiolgun hilmiolgun 4096 May 30 10:21 Mail
  1737. drwxr-xr-x 5 hilmiolgun hilmiolgun 90 Sep 2 02:59 MobilePositionSDK
  1738. drwx------ 7 hilmiolgun hilmiolgun 4096 Aug 8 2002 NTnetscape
  1739. drwxr--r-- 13 hilmiolgun hilmiolgun 4096 Sep 4 01:56 New Folder
  1740. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 TTI409B
  1741. drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 cgi-bin
  1742. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 geu
  1743. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 hilmiolgun
  1744. drwxr-xr-x 6 hilmiolgun hilmiolgun 4096 Aug 14 15:59 image
  1745. drwxr-xr-x 3 hilmiolgun hilmiolgun 4096 Jul 29 16:03 jmf20-apidocs
  1746. drwxr-xr-x 4 hilmiolgun hilmiolgun 4096 Sep 9 14:10 NOTEsieee
  1747. drwx------ 2 hilmiolgun hilmiolgun 6 Feb 21 2002 nsmail
  1748. drwx------ 3 hilmiolgun hilmiolgun 103 Feb 21 2002 office52
  1749. drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 private
  1750. drwxr--rwx 2 hilmiolgun hilmiolgun 4096 Aug 23 12:02 public_html
  1751. drwxr-xr-x 5 hilmiolgun hilmiolgun 4096 Sep 6 03:30 speech
  1752. -rw-rw-r-- 1 hilmiolgun hilmiolgun 2630 Sep 9 13:58 test.txt
  1753. -rw-rw-r-- 1 hilmiolgun hilmiolgun 148 Sep 9 14:03 testing.txt
  1754. 226 Transfer complete.
  1755. ftp: 1318 bytes received in 0,24Seconds 5,49Kbytes/sec.
  1756. ftp> get testing.txt
  1757. 200 PORT command successful
  1758. 150 Opening ASCII mode data connection for testing.txt (148 bytes)
  1759. 226 Transfer complete.
  1760. ftp: 161 bytes received in 0,02Seconds 8,05Kbytes/sec.
  1761. NOTE :After taking a look to the school harddisk ,I copied a file "testing.txt" to my local harddisk....
  1762. ftp> !dir
  1763. Volume in drive C has no label.
  1764. Volume Serial Number is 0868-D52D
  1765. Directory of C:\Documents and Settings\sysadm
  1766. 10-09-2003 00:21 <DIR> .
  1767. 10-09-2003 00:21 <DIR> ..
  1768. 31-08-2003 07:28 <DIR> .java
  1769. 25-04-2003 12:18 <DIR> .javaws
  1770. 23-04-2003 15:26 <DIR> .jpi_cache
  1771. 26-08-2003 04:59 <DIR> .Nokia
  1772. 07-09-2003 01:46 12.546 .plugin140_03.trace
  1773. 07-09-2003 04:46 693 .plugin141_02.trace
  1774. 07-09-2003 01:20 164 .saves-3824-IBMR31IMAGE
  1775. 07-09-2003 01:20 <DIR> Desktop
  1776. 07-09-2003 08:05 <DIR> Favorites
  1777. 06-09-2003 05:29 80.140 love.wav
  1778. 09-09-2003 23:45 <DIR> mindterm
  1779. 09-09-2003 11:02 <DIR> My Documents
  1780. 10-09-2003 00:21 2.903 plugin131_08.trace
  1781. 25-04-2003 11:44 <DIR> Start Menu
  1782. 06-09-2003 21:21 <DIR> studio5se_user
  1783. 06-09-2003 05:32 18 test.txt
  1784. 06-09-2003 05:20 70 testing
  1785. 10-09-2003 00:37 161 testing.txt
  1786. 26-08-2003 03:46 <DIR> WINDOWS
  1787. 8 File(s) 96.695 bytes
  1788. 13 Dir(s) 3.842.056.192 bytes free
  1789. ftp> send love.wav
  1790. 200 PORT command successful
  1791. 150 Opening ASCII mode data connection for love.wav
  1792. 226 Transfer complete.
  1793. ftp: 80140 bytes sent in 3,97Seconds 20,21Kbytes/sec.
  1794. ftp> dir
  1795. 200 PORT command successful
  1796. 150 Opening ASCII mode data connection for file list
  1797. drwx------ 4 hilmiolgun hilmiolgun 155 Jul 1 14:02 Desktop
  1798. drwx------ 2 hilmiolgun hilmiolgun 4096 May 30 10:21 Mail
  1799. drwxr-xr-x 5 hilmiolgun hilmiolgun 90 Sep 2 02:59 MobilePositionSDK
  1800. drwx------ 7 hilmiolgun hilmiolgun 4096 Aug 8 2002 NTnetscape
  1801. drwxr--r-- 13 hilmiolgun hilmiolgun 4096 Sep 4 01:56 New Folder
  1802. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 TTI409B
  1803. drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 cgi-bin
  1804. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 geu
  1805. -rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 hilmiolgun
  1806. drwxr-xr-x 6 hilmiolgun hilmiolgun 4096 Aug 14 15:59 image
  1807. drwxr-xr-x 3 hilmiolgun hilmiolgun 4096 Jul 29 16:03 jmf20-apidocs
  1808. -rw-rw-r-- 1 hilmiolgun hilmiolgun 80137 Sep 9 22:36 love.wav
  1809. drwxr-xr-x 4 hilmiolgun hilmiolgun 4096 Sep 9 14:10 NOTEsieee
  1810. drwx------ 2 hilmiolgun hilmiolgun 6 Feb 21 2002 nsmail
  1811. drwx------ 3 hilmiolgun hilmiolgun 103 Feb 21 2002 office52
  1812. drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 private
  1813. drwxr--rwx 2 hilmiolgun hilmiolgun 4096 Aug 23 12:02 public_html
  1814. drwxr-xr-x 5 hilmiolgun hilmiolgun 4096 Sep 6 03:30 speech
  1815. -rw-rw-r-- 1 hilmiolgun hilmiolgun 2630 Sep 9 13:58 test.txt
  1816. -rw-rw-r-- 1 hilmiolgun hilmiolgun 148 Sep 9 14:03 testing.txt
  1817. 226 Transfer complete.
  1818. ftp: 1387 bytes received in 0,07Seconds 19,81Kbytes/sec.
  1819. ftp>
  1820. NOTE:At the end first looking at the local working directory and sending a file "love.wav" to the
  1821. school harddisk.
  1822. 7-)net:
  1823. It has many options,which are for checking/starting/stopping nt
  1824. services,users,messaging,configuration and so on...
  1825. Some of those options require administration privileges..
  1826. Example:
  1827. NOTE: To have an overview of commands options....
  1828. C:\Documents and Settings\sysadm>net
  1829. The syntax of this command is:
  1830. NET COMMANDS
  1831. NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
  1832. HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
  1833. SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
  1834. NOTE: And furthermore to get an overview of a specific option ...
  1835. C:\Documents and Settings\sysadm>net help print
  1836. The syntax of this command is:
  1837. NET PRINT
  1838. \\computername\sharename
  1839. [\\computername] job# [/HOLD | /RELEASE | /DELETE]
  1840. NET PRINT displays print jobs and shared queues.
  1841. For each queue, the display lists jobs, showing the size
  1842. and status of each job, and the status of the queue.
  1843. \\computername Is the name of the computer sharing the printer
  1844. queue(s).
  1845. sharename Is the name of the shared printer queue.
  1846. job# Is the identification number assigned to a print
  1847. job. A computer with one or more printer queues
  1848. assigns each print job a unique number.
  1849. /HOLD Prevents a job in a queue from printing.
  1850. The job stays in the printer queue, and other
  1851. jobs bypass it until it is released.
  1852. /RELEASE Reactivates a job that is held.
  1853. /DELETE Removes a job from a queue.
  1854. NET HELP command | MORE displays Help one screen at a time.
  1855. Finally in addition to above there are also those commands: hostname ,lpq, lpr ,rsh ,tftp ,nbstat
  1856. ,netstat.
  1857. To get familiar with those commands simply type commandname /? at the command line.
  1858. C:\>net
  1859. The syntax of this command is:
  1860. NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
  1861. HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
  1862. SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
  1863. C:\>net use
  1864. New connections will not be remembered.
  1865. Status Local Remote Network
  1866. -------------------------------------------------------------------------------
  1867. OK F: \\cse-sec\fac Microsoft Windows Network
  1868. C:\>net user
  1869. User accounts for \\CSE-DEPT-05
  1870. -------------------------------------------------------------------------------
  1871. Administrator Guest
  1872. C:\>net statistics
  1873. Statistics are available for the following running services:
  1874. Server
  1875. Workstation
  1876. Displays protocol statistics and current TCP/IP network connections.
  1877. NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]
  1878. -a Displays all connections and listening ports.
  1879. -e Displays Ethernet statistics. This may be combined with the
  1880. -s option.
  1881. -n Displays addresses and port numbers in numerical form.
  1882. -p proto Shows connections for the protocol specified by proto; proto
  1883. may be TCP or UDP. If used with the -s option to display
  1884. per-protocol statistics, proto may be TCP, UDP, or IP.
  1885. -r Displays the routing table.
  1886. -s Displays per-protocol statistics. By default, statistics
  1887. are shown for TCP, UDP and IP; the -p option may be used
  1888. to specify a subset of the default.
  1889. interval Redisplays selected statistics, pausing interval seconds
  1890. between each display. Press CTRL+C to stop redisplaying
  1891. statistics. If omitted, netstat will print the current
  1892. configuration information once.
  1893. C:\>net name
  1894. Name
  1895. -------------------------------------------------------------------------------
  1896. CSE-DEPT-05
  1897. C:\>net session
  1898. Computer User name Client Type Opens Idle time
  1899. -------------------------------------------------------------------------------
  1900. \\ENGLISH-03 Windows NT 1381 0 00:10:47
  1901. \\ENGLISHBDC Windows NT 1381 0 00:02:01
  1902. C:\>net accounts
  1903. Force user logoff how long after time expires?: Never
  1904. Minimum password age (days): 0
  1905. Maximum password age (days): 42
  1906. Minimum password length: 0
  1907. Length of password history maintained: None
  1908. Lockout threshold: Never
  1909. Lockout duration (minutes): 30
  1910. Lockout observation window (minutes): 30
  1911. Computer role: WORKSTATION
  1912. C:\>net localgroup
  1913. Aliases for \\CSE-DEPT-05
  1914. -------------------------------------------------------------------------------
  1915. *Administrators *Backup Operators *Guests
  1916. *Power Users *Replicator *Users
  1917. C:\>net config server
  1918. Server Name \\CSE-DEPT-05
  1919. Server Comment
  1920. Software version Windows NT 4.0
  1921. Server is active on NetBT_DLKRTS1 (0050ba8b326b) NetBT_DLKRTS1
  1922. (0050ba8b326b) NwlnkIpx (0050ba8b326b) NwlnkNb (0050ba8b326b) Nbf_DLKRTS1 (0050
  1923. ba8b326b)
  1924. Server hidden No
  1925. Maximum Logged On Users 10
  1926. Maximum open files per session 2048
  1927. Idle session time (min) 15
  1928. C:\>net config workstation
  1929. Computer name \\CSE-DEPT-05
  1930. User name Administrator
  1931. Workstation active on NwlnkNb (0050BA8B326B) NetBT_DLKRTS1 (0050B
  1932. A8B326B) Nbf_DLKRTS1 (0050BA8B326B)
  1933. Software version Windows NT 4.0
  1934. Workstation domain WORKGROUP
  1935. Logon domain CSE-DEPT-05
  1936. COM Open Timeout (sec) 3600
  1937. COM Send Count (byte) 16
  1938. COM Send Timeout (msec) 250
  1939. C:\>net share
  1940. Share name Resource Remark
  1941. -------------------------------------------------------------------------------
  1942. D$ D:\ Default share
  1943. IPC$ Remote IPC
  1944. C$ C:\ Default share
  1945. ADMIN$ C:\WINNT Remote Admin
  1946. E$ E:\ Default share
  1947. abishek E:\abishek
  1948. akshu E:\akshu
  1949. HARSHINI D:\ HARSHINI
  1950. DHARSHINI E:\ DHARSHINI
  1951. C:\>net stop messenger
  1952. The Messenger service is stopping.
  1953. The Messenger service was stopped successfully.
  1954. C:\>net start messenger
  1955. The Messenger service is starting...
  1956. The Messenger service was started successfully.
  1957. Network Configuration commands
  1958. ifconfig
  1959. This command is used to configure network interfaces, or to display their current
  1960. configuration. In addition to activating and deactivating interfaces with the “up” and “down”
  1961. settings, this command is necessary for setting an interface's address information if you don't
  1962. have the ifcfg script.
  1963. Use ifconfig as either:
  1964. ifconfig
  1965. This will simply list all information on all network devices currently up.
  1966. ifconfig eth0 down
  1967. This will take eth0 (assuming the device exists) down, it won't be able to receive or send
  1968. anything until you put the device back “up” again.
  1969. Clearly there are a lot more options for this tool, you will need to read the manual/info page to
  1970. learn more about them.
  1971. ifup
  1972. Use ifup device-name to bring an interface up by following a script (which will contain your
  1973. default networking settings). Simply type ifup and you will get help on using the script.
  1974. For example typing:
  1975. ifup eth0
  1976. Will bring eth0 up if it is currently down.
  1977. ifdown
  1978. Use ifdown device-name to bring an interface down using a script (which will contain your
  1979. default network settings). Simply type ifdown and you will get help on using the script.
  1980. For example typing:
  1981. ifdown eth0
  1982. Will bring eth0 down if it is currently up.
  1983. ifcfg
  1984. Use ifcfg to configure a particular interface. Simply type ifcfg to get help on using this script.
  1985. For example, to change eth0 from 192.168.0.1 to 192.168.0.2 you could do:
  1986. ifcfg eth0 del 192.168.0.1
  1987. ifcfg eth0 add 192.168.0.2
  1988. The first command takes eth0 down and removes that stored IP address and the second one
  1989. brings it back up with the new address.
  1990. route
  1991. The route command is the tool used to display or modify the routing table. To add a gateway
  1992. as the default you would type:
  1993. route add default gw some_computer
  1994. INTERNET SPECIFIC COMMANDS
  1995. host
  1996. Performs a simple lookup of an internet address (using the Domain Name System, DNS).
  1997. Simply type:
  1998. host ip_address
  1999. or
  2000. host domain_name
  2001. dig
  2002. The "domain information groper" tool. More advanced then host... If you give a hostname as
  2003. an argument to output information about that host, including it's IP address, hostname and
  2004. various other information.
  2005. For example, to look up information about “www.amazon.com” type:
  2006. dig www.amazon.com
  2007. To find the host name for a given IP address (ie a reverse lookup), use dig with the `-x' option.
  2008. dig -x 100.42.30.95
  2009. This will look up the address (which may or may not exist) and returns the address of the
  2010. host, for example if that was the address of “http://slashdot.org” then it would return
  2011. “http://slashdot.org”.
  2012. dig takes a huge number of options (at the point of being too many), refer to the manual page
  2013. for more information.
  2014. whois
  2015. (now BW whois) is used to look up the contact information from the “whois” databases, the
  2016. servers are only likely to hold major sites. Note that contact information is likely to be hidden
  2017. or restricted as it is often abused by crackers and others looking for a way to cause malicious
  2018. damage to organisation's.
  2019. wget
  2020. (GNU Web get) used to download files from the World Wide Web.
  2021. To archive a single web-site, use the -m or --mirror (mirror) option.
  2022. Use the -nc (no clobber) option to stop wget from overwriting a file if you already have it.
  2023. Use the -c or --continue option to continue a file that was unfinished by wget or another
  2024. program.
  2025. Simple usage example:
  2026. wget url_for_file
  2027. This would simply get a file from a site.
  2028. wget can also retrieve multiple files using standard wildcards, the same as the type used in
  2029. bash, like *, [ ], ?. Simply use wget as per normal but use single quotation marks (' ') on the
  2030. URL to prevent bash from expanding the wildcards. There are complications if you are
  2031. retrieving from a http site (see below...).
  2032. Advanced usage example, (used from wget manual page):
  2033. wget --spider --force-html -i bookmarks.html
  2034. This will parse the file bookmarks.html and check that all the links exist.
  2035. Advanced usage: this is how you can download multiple files using http (using a wildcard...).
  2036. Notes: http doesn't support downloading using standard wildcards, ftp does so you may use
  2037. wildcards with ftp and it will work fine. A work-around for this http limitation is shown
  2038. below:
  2039. wget -r -l1 --no-parent -A.gif http://www.website.com[1]
  2040. This will download (recursively), to a depth of one, in other words in the current directory and
  2041. not below that. This command will ignore references to the parent directory, and downloads
  2042. anything that ends in “.gif”. If you wanted to download say, anything that ends with “.pdf” as
  2043. well than add a -A.pdf before the website address. Simply change the website address and the
  2044. type of file being downloaded to download something else. Note that doing -A.gif is the same
  2045. as doing -A “*.gif(double quotes only, single quotes will not work).
  2046. wget has many more options refer to the examples section of the manual page, this tool is very
  2047. well documented.
  2048. Alternative website downloaders: You may like to try alternatives like httrack. A full GUI
  2049. website downloader written in python and available for GNU/Linux
  2050. curl
  2051. curl is another remote downloader. This remote downloader is designed to work without user
  2052. interaction and supports a variety of protocols, can upload/download and has a large number
  2053. of tricks/work-arounds for various things. It can access dictionary servers (dict), ldap servers,
  2054. ftp, http, gopher, see the manual page for full details.
  2055. To access the full manual (which is huge) for this command type:
  2056. curl -M
  2057. For general usage you can use it like wget. You can also login using a user name by using the
  2058. -u option and typing your username and password like this:
  2059. curl -u username:password http://www.placetodownload/file
  2060. To upload using ftp you the -T option:
  2061. curl -T file_name ftp://ftp.uploadsite.com
  2062. To continue a file use the -C option:
  2063. curl -C - -o file http://www.site.com
  2064. View and modify network interfaces
  2065. ifconfig -a Show information about all network interfaces
  2066. ifconfig eth0 Show information only about the interface eth0
  2067. ifconfig eth0 up Bring up the interface eth0
  2068. ifconfig eth0 down Take down the interface eth0
  2069. Simple network diagnostic commands
  2070. ping hostname Send ICMP echo requests to the host hostname
  2071. traceroute hostname Trace the network path to hostname
  2072. View open network connections
  2073. netstat -a Show information about all open network connections
  2074. netstat -a | grep LISTEN Show information about all open network ports
  2075. Set/view routing information
  2076. netstat -r View system routing tables
  2077. route View system routing tables
  2078. The command route can also be used to add or delete routes. Examples:
  2079. route add -host 192.168.3.4 gw 192.168.3.1 netmask 255.255.0.0
  2080. route del -host 192.168.3.4
  2081. NETSTAT.exe TCP/IP Network Statistics
  2082. Displays protocol statistics and current TCP/IP network connections.
  2083. NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]
  2084. -a Displays all connections and listening ports.
  2085. -e Displays Ethernet statistics. This may be combined with the -s option.
  2086. -n Displays addresses and port numbers in numerical form.
  2087. -p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP.
  2088. If used with the -s option to display per-protocol statistics, proto may be TCP, UDP,
  2089. or IP.
  2090. -r Displays the routing table.
  2091. -s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP;
  2092. the -p option may be used to specify a subset of the default.
  2093. interval Redisplays selected statistics, pausing interval seconds between each display. Press
  2094. CTRL+C to stop redisplaying statistics. If omitted, netstat will print the current
  2095. configuration information once.
  2096. C:\WINDOWS>netstat -a
  2097. Active Connections
  2098. Proto Local Address Foreign Address State
  2099. TCP My_Comp:ftp localhost:0 LISTENING
  2100. TCP My_Comp:80 localhost:0 LISTENING
  2101. Or with the "-an" parameters:
  2102. C:\WINDOWS>netstat -an
  2103. Active Connections
  2104. Proto Local Address Foreign Address State
  2105. TCP 0.0.0.0:21 0.0.0.0:0 LISTENING
  2106. TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
  2107. By simply opening a browser connection to both the HTTP (port 80) and FTP (port 21) servers
  2108. (while still offline!), I saw the following:
  2109. C:\WINDOWS>netstat -a
  2110. Active Connections
  2111. Proto Local Address Foreign Address State
  2112. TCP My_Comp:ftp localhost:0 LISTENING
  2113. TCP My_Comp:80 localhost:0 LISTENING
  2114. TCP My_Comp:1104 localhost:0 LISTENING
  2115. TCP My_Comp:ftp localhost:1104 ESTABLISHED
  2116. TCP My_Comp:1102 localhost:0 LISTENING
  2117. TCP My_Comp:1103 localhost:0 LISTENING
  2118. TCP My_Comp:80 localhost:1111 TIME_WAIT
  2119. TCP My_Comp:1104 localhost:ftp ESTABLISHED
  2120. TCP My_Comp:1107 localhost:0 LISTENING
  2121. TCP My_Comp:1112 localhost:80 TIME_WAIT
  2122. UDP My_Comp:1102 *:*
  2123. UDP My_Comp:1103 *:*
  2124. UDP My_Comp:1107 *:*
  2125. This may be a bit confusing to some people, but remember I'm running BOTH the servers and clients
  2126. on the same machine in these examples. A little later (using both 'a' and 'n') I got this:
  2127. C:\WINDOWS>netstat -an
  2128. Active Connections
  2129. Proto Local Address Foreign Address State
  2130. TCP 0.0.0.0:21 0.0.0.0:0 LISTENING
  2131. TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
  2132. TCP 0.0.0.0:1104 0.0.0.0:0 LISTENING
  2133. TCP 127.0.0.1:21 127.0.0.1:1104 FIN_WAIT_2
  2134. TCP 127.0.0.1:1102 0.0.0.0:0 LISTENING
  2135. TCP 127.0.0.1:1103 0.0.0.0:0 LISTENING
  2136. TCP 127.0.0.1:1104 127.0.0.1:21 CLOSE_WAIT
  2137. TCP 127.0.0.1:1107 0.0.0.0:0 LISTENING
  2138. UDP 127.0.0.1:1102 *:*
  2139. UDP 127.0.0.1:1103 *:*
  2140. UDP 127.0.0.1:1107 *:*
  2141. After turning off my server, I ended up with this for a while:
  2142. C:\WINDOWS>netstat -an
  2143. Active Connections
  2144. Proto Local Address Foreign Address State
  2145. TCP 127.0.0.1:80 127.0.0.1:1150 TIME_WAIT
  2146. TCP 127.0.0.1:80 127.0.0.1:1151 TIME_WAIT
  2147. PING.exe
  2148. Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
  2149. [-r count] [-s count] [[-j host-list] | [-k host-list]]
  2150. [-w timeout] destination-list
  2151. Options:
  2152. -t Ping the specifed host until interrupted.
  2153. -a Resolve addresses to hostnames.
  2154. -n count Number of echo requests to send.
  2155. -l size Send buffer size.
  2156. -f Set "Don't Fragment" flag in packet.
  2157. -i TTL Time To Live.
  2158. -v TOS Type Of Service.
  2159. -r count Record route for count hops.
  2160. -s count Timestamp for count hops.
  2161. -j host-list Loose source route along host-list.
  2162. -k host-list Strict source route along host-list.
  2163. -w timeout Timeout in milliseconds to wait for each reply.
  2164. There's one special IP number everyone should know about:
  2165. 127.0.0.1 - localhost (or loopback).
  2166. This is used to connect ( through a browser, for example) to a Web server on your own computer.
  2167. (127 being reserved for this purpose.) You can use this IP number at all times. It doesn't matter if
  2168. you're connected to the Internet or not.
  2169. It's also called the loopback address because you can ping it and get returns even when you're
  2170. offline (not connected to any network). If you don't get any valid replies, then there's a problem with
  2171. the computer's Network settings. Here's a typical response to the 'ping' command:
  2172. Here's another recent example using the name of my computer which I have tied to the IP number
  2173. 127.0.0.1 in my C:\WINDOWS\HOSTS file:
  2174. C:\WINDOWS>ping My_Comp
  2175. Pinging My_Comp [127.0.0.1] with 32 bytes of data:
  2176. Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
  2177. Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
  2178. Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
  2179. Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
  2180. Ping statistics for 127.0.0.1:
  2181. Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  2182. Approximate round trip times in milli-seconds:
  2183. Minimum = 0ms, Maximum = 1ms, Average = 0ms
  2184. TRACERT.exe Trace Route
  2185. Usage:
  2186. tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name
  2187. Options:
  2188. -d Do not resolve addresses to hostnames.
  2189. -h maximum_hops Maximum number of hops to search for target.
  2190. -j host-list Loose source route along host-list.
  2191. -w timeout Wait timeout milliseconds for each reply.
  2192. Here's an example which traces the route from some ISP in Los Angeles to the main server at UCLA
  2193. in California ( note how two computers relatively close to each other may be routed way round
  2194. about! ):
  2195. C:\WINDOWS>tracert www.ucla.edu
  2196. Tracing route to www.ucla.edu [169.232.33.129]
  2197. over a maximum of 30 hops:
  2198. 1 141 ms 132 ms 140 ms wla-ca-pm6.icg.net [165.236.29.85]
  2199. 2 134 ms 131 ms 139 ms whv-ca-gw1.icg.net [165.236.29.65]
  2200. 3 157 ms 132 ms 143 ms f3-1-0.lai-ca-gw1.icg.net [165.236.24.89]
  2201. 4 194 ms 193 ms 188 ms a0-0-0-1.dai-tx-gw1.icg.net [163.179.235.61]
  2202. 5 300 ms 211 ms 214 ms a1-1-0-1.ati-ga-gw1.icg.net [163.179.235.186]
  2203. 6 236 ms 237 ms 247 ms a5-0-0-1.was-dc-gw1.icg.net [163.179.235.129]
  2204. 7 258 ms 236 ms 244 ms 163.179.243.205
  2205. 8 231 ms 233 ms 230 ms wdc-brdr-03.inet.qwest.net [205.171.4.153]
  2206. 9 240 ms 230 ms 236 ms wdc-core-03.inet.qwest.net [205.171.24.69]
  2207. 10 262 ms 264 ms 263 ms hou-core-01.inet.qwest.net [205.171.5.187]
  2208. 11 281 ms 263 ms 259 ms hou-core-03.inet.qwest.net [205.171.23.9]
  2209. 12 272 ms 229 ms 222 ms lax-core-02.inet.qwest.net [205.171.5.163]
  2210. 13 230 ms 217 ms 230 ms lax-edge-07.inet.qwest.net [205.171.19.58]
  2211. 14 228 ms 219 ms 220 ms 63-145-160-42.cust.qwest.net [63.145.160.42]
  2212. 15 218 ms 222 ms 218 ms ISI-7507--ISI.POS.calren2.net [198.32.248.21]
  2213. 16 232 ms 222 ms 214 ms UCLA--ISI.POS.calren2.net [198.32.248.30]
  2214. 17 234 ms 226 ms 226 ms cbn5-gsr.calren2.ucla.edu [169.232.1.18]
  2215. 18 245 ms 227 ms 235 ms www.ucla.edu [169.232.33.129]
  2216. Trace complete.
  2217. Net Bios Stats
  2218. NBTSTAT.exe
  2219. Displays protocol statistics and current TCP/IP connections using NBT
  2220. (NetBIOS over TCP/IP).
  2221. NBTSTAT [-a RemoteName] [-A IP address] [-c] [-n] [-r] [-R] [-s] [S] [interval]
  2222. -a (adapter status) Lists the remote machine's name table given its name.
  2223. -A (Adapter status) Lists the remote machine's name table given its IP address.
  2224. -c (cache) Lists the remote name cache including the IP addresses.
  2225. -n (names) Lists local NetBIOS names.
  2226. -r (resolved) Lists names resolved by broadcast and via WINS
  2227. -R (Reload) Purges and reloads the remote cache name table
  2228. -S (Sessions) Lists sessions table with the destination IP addresses.
  2229. -s (sessions) Lists sessions table converting destination IP addresses to host names via the
  2230. hosts file.
  2231. RemoteName Remote host machine name.
  2232. IP address Dotted decimal representation of the IP address.
  2233. interval Redisplays selected statistics, pausing interval seconds between each display. Press
  2234. Ctrl+C to stop redisplaying statistics.
  2235. ROUTE.exe
  2236. Manipulates network routing tables.
  2237. ROUTE [-f] [command [destination] [MASK netmask] [gateway]]
  2238. -f Clears the routing tables of all gateway entries. If this is used in conjunction
  2239. with one of the commands, the tables are cleared prior to running the command.
  2240. command Specifies one of four commands
  2241. PRINT Prints a route
  2242. ADD Adds a route
  2243. DELETE Deletes a route
  2244. CHANGE Modifies an existing route
  2245. destination Specifies the host to send command.
  2246. MASK If the MASK keyword is present, the next parameter is interpreted as the
  2247. netmask parameter.
  2248. netmask If provided, specifies a sub-net mask value to be associated with this route entry.
  2249. If not specified, if defaults to 255.255.255.255.
  2250. gateway Specifies gateway.
  2251. All symbolic names used for destination or gateway are looked up in the network and host
  2252. name database files NETWORKS and HOSTS, respectively.
  2253. If the command is print or delete, wildcards may be used for the destination and gateway, or
  2254. the gateway argument may be omitted.
  2255. ARP.exe Address Resolution Protocol
  2256. ARP -s inet_addr eth_addr [if_addr]
  2257. ARP -d inet_addr [if_addr]
  2258. ARP -a [inet_addr] [-N if_addr]
  2259. -a Displays current ARP entries by interrogating the current protocol data. If inet_addr
  2260. is specified, the IP and Physical addresses for only the specified computer are
  2261. displayed. If more than one network interface uses ARP, entries for each ARP
  2262. table are displayed.
  2263. -g (Same as -a)
  2264. inet_addr Specifies an internet address.
  2265. -N if_addr Displays the ARP entries for the network interface specified by if_addr.
  2266. -d Deletes the host specified by inet_addr.
  2267. -s Adds the host and associates the Internet address inet_addr with the Physical address
  2268. eth_addr. The Physical address is given as 6 hexadecimal bytes separated by hyphens.
  2269. The entry is permanent.
  2270. eth_addr Specifies a physical address.
  2271. if_addr If present, this specifies the Internet address of the interface
  2272. whose address translation table should be modified. If not present, the first
  2273. applicable interface will be used.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement