· 9 years ago · Mar 19, 2017, 05:56 PM
11.WAJP to display server date and time in client
2
3Server:
4
5import java.io.*;
6import java.net.*;
7import java.util.Date;
8class server1 {
9 public static void main(String argv[]) throws Exception
10 {
11 String capitalizedSentence;
12 ServerSocket welcomeSocket = new ServerSocket(6789);
13 while(true) {
14 Socket connectionSocket = welcomeSocket.accept();
15DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
16 capitalizedSentence = new Date().toString() + '\n';
17 outToClient.writeBytes(capitalizedSentence);
18 }
19 }
20}
21
22
23Client:
24
25import java.io.*;
26import java.net.*;
27class client1 {
28 public static void main(String argv[]) throws Exception
29 {
30 String modifiedSentence;
31 Socket clientSocket = new Socket("localhost", 6789);
32BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
33 modifiedSentence = inFromServer.readLine();
34 System.out.println("FROM SERVER: " + modifiedSentence);
35 clientSocket.close();
36 }
37}
38
39
40
41
422.WAJP to display client address in server
43Server:
44
45import java.io.*;
46import java.net.*;
47
48class server1 {
49 public static void main(String argv[]) throws Exception
50 {
51 ServerSocket welcomeSocket = new ServerSocket(6789);
52 while(true) {
53 Socket connectionSocket = welcomeSocket.accept();
54 BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
55System.out.println("Address Received from client : "+ inFromClient.readLine());
56 }
57 }
58}
59
60
61Client:
62
63import java.io.*;
64import java.net.*;
65class client1 {
66 public static void main(String argv[]) throws Exception
67 {
68 Socket clientSocket = new Socket("localhost", 6789);
69DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
70 outToServer.writeBytes(InetAddress.getLocalHost().getHostAddress() + '\n');
71 clientSocket.close();
72
73 }
74}
75
76
77
784.WAJP for chat
79SERVER
80import java.io.*;
81import java.net.*;
82import java.util.*;
83class Server
84{
85public static void main(String[] args)throws IOException
86{
87ServerSocket sr=new ServerSocket(6789);
88while(true)
89{
90Socket cons=sr.accept();
91BufferedReader incl=new BufferedReader(new InputStreamReader(cons.getInputStream()));
92OutputStream outcl=cons.getOutputStream();
93PrintWriter pw=new PrintWriter(outcl,true);
94BufferedReader serin=new BufferedReader(new InputStreamReader(System.in));
95String str=" ",str1=" ";
96try
97{
98while( (str=incl.readLine())!=null)
99{
100System.out.println("Client:"+str);
101str1=serin.readLine();
102System.out.println("You:"+str1);
103pw.println(str1);
104}
105}
106catch(SocketException e)
107{
108System.out.println(e);
109}
110}
111}
112}
113
114CLIENT
115import java.io.*;
116import java.net.*;
117
118class Client
119{
120public static void main(String[] args)throws IOException
121{
122String str=" ",str1=" ";
123int i=0;
124Socket cls=new Socket("localhost",6789);
125BufferedReader inser=new BufferedReader(new InputStreamReader(cls.getInputStream()));
126OutputStream outser=cls.getOutputStream();
127PrintWriter pw=new PrintWriter(outser,true);
128BufferedReader clin=new BufferedReader(new InputStreamReader(System.in));
129System.out.println("Begin chat");
130str1=clin.readLine();
131System.out.println("You:"+str1);
132pw.println(str1);
133try
134{
135while( (str=inser.readLine())!=null)
136{
137System.out.println("Server:"+str);
138str1=clin.readLine();
139System.out.println("You:"+str1);
140pw.println(str1);
141}
142}
143catch(SocketException e)
144{
145System.out.println(e);
146}//catch
147
148}
149}
150
151
1525.TCP/IP
153server:
154
155import java.io.*;
156import java.net.*;
157
158class server1 {
159
160 public static void main(String argv[]) throws Exception {
161 String clientSentence;
162 ServerSocket welcomeSocket = new ServerSocket(6789);
163 while (true) {
164 Socket connectionSocket = welcomeSocket.accept();
165 BufferedReader inFromClient
166 = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
167 DataOutputStream outToClient
168 = new DataOutputStream(connectionSocket.getOutputStream());
169 clientSentence = inFromClient.readLine();
170 System.out.println("Received from client : " + clientSentence);
171 String a = clientSentence.toLowerCase();
172 String b = "";//abc
173 String x = "abcdefghijklmnopqrstuvwxyz";
174//code to encode
175 for (int i = 0; i < a.length(); i++) {
176 if (a.charAt(i) == 'z') {
177 b += 'a';
178 } else {
179 for (int j = 0; j < x.length(); j++) {
180 if (x.charAt(j) == a.charAt(i)) {
181 b += x.charAt(j + 1);
182 break;
183 }
184 }
185 }
186 }
187 outToClient.writeBytes(b+"\n");
188 }
189 }
190}
191
192client:
193
194import java.io.*;
195import java.net.*;
196class client1 {
197 public static void main(String argv[]) throws Exception {
198 String sentence;
199 String modifiedSentence;
200 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
201 Socket clientSocket = new Socket("localhost", 6789);
202 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
203 BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
204 sentence = inFromUser.readLine();
205 outToServer.writeBytes(sentence + '\n');
206
207 modifiedSentence = inFromServer.readLine();
208
209 System.out.println("FROM SERVER: " + modifiedSentence);
210
211 clientSocket.close();
212
213 }
214}
215
2167.WAJP to display name and address of computer currently working on
217import java.net.InetAddress;
218public class pro1 {
219 public static void main(String []args) throws Exception{
220 InetAddress a=InetAddress.getLocalHost();
221 System.out.println(a);
222 }
223}
224
2258.IPv4 header
226
227
2289.Threads
229import java.util.*;
230/**
231 *
232 * @author mock3
233 */
234class waste extends Thread {
235
236 int a;
237
238 public void run() {
239 try {
240 Random r = new Random();
241 int r1= r.nextInt(1000);
242 if(Thread.currentThread().isDaemon()){
243 for (a = 0; a < 4; a++) {
244 System.out.println(a + " daemon");
245 Thread.sleep(r1);
246 }
247 }
248 else{
249 for (a = 0; a < 4; a++) {
250 System.out.println(a + " haha");
251 Thread.sleep(r1);
252 }
253 }
254 } catch (Exception e) {
255
256 }
257 }
258}
259
260public class JavaApplication2 {
261
262 /**
263 * @param args the command line arguments
264 */
265 public static void main(String[] args) throws Exception {
266 // TODO code application logic here
267 waste w1 = new waste();
268 w1.setDaemon(true);
269 waste w2 = new waste();
270 waste w3 = new waste();
271 w1.start();
272 w2.start();
273 w3.start();
274 }
275
276}
277
27810.WAJP to run basic network commands
279import java.net.*;
280import java.util.*;
281import java.io.*;
282public class command
283{
284 public static void main(String args[])
285 throws Exception
286 {
287 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
288 System.out.println("enter command:");
289 String s=br.readLine();
290 Process p=Runtime.getRuntime().exec(s);
291 p.waitFor();
292 BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
293 String line=reader.readLine();
294 while(line!=null)
295 {
296 System.out.println(line);
297 line=reader.readLine();0
298 }
299 System.out.println("done!");
300
301}
302}
303
304
305
306
3071- Wap to display server's date and time details at the client end.
308
309DateClient.java
310import java.io.*;
311import java.net.Socket;
312public class dateClient {
313public static void main(String[] args) throws IOException {
314Socket s = new Socket("127.0.0.1",9090);
315BufferedReader input = new BufferedReader(new
316InputStreamReader(s.getInputStream()));
317String answer = input.readLine();
318System.out.println("The date and time details are"+answer);
319System.exit(0);
320}
321
322
323
324DATESERVER.java
325
326import java.io.IOException;
327import java.io.PrintWriter;
328import java.net.ServerSocket;
329import java.net.Socket;
330import java.util.Date;
331public class dateserver {
332public static void main(String[] args) throws IOException {
333ServerSocket listener = new ServerSocket(9090);
334try {
335while(true) {
336Socket socket = listener.accept();
337try {
338PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
339out.println(new Date().toString());
340}finally {
341socket.close();
342}
343}
344}
345finally {
346listener.close();
347}
348}
349}
350
351
352
3532-> Wap to display the client's address at the server end?
354
355import java.io.*;
356import java.net.*;
357public class ServerAddr {
358public static void main(String[] args) throws IOException {
359try {
360ServerSocket ss = new ServerSocket(6666);
361System.out.println("Waiting for client....");
362Socket s = ss.accept();
363System.out.println("Connected to client....");
364DataInputStream in = new DataInputStream(s.getInputStream());
365String line = null;
366line = in.readUTF();
367System.out.println("Client's IP address:" + line);
368}
369catch(Exception e) {
370e.printStackTrace();
371}}}
372
373
374import java.io.*;
375import java.net.*;
376public class ClientAddr {
377public static void main(String[] args) throws IOException {
378try {
379InetAddress ipaddress = InetAddress.getByName("");
380Socket s = new Socket(ipaddress, 6666);
381System.out.println("Connected to the server...");
382DataOutputStream out = new DataOutputStream(s.getOutputStream());
383String line = null;
384System.out.println("Sending IP Adrress to server...");
385line = ipaddress.getHostAddress();
386out.writeUTF(line);
387out.flush(); }
388catch(Exception e) {
389e.printStackTrace();
390}}}
391
392
393
3943-> A java program to make a chat application using UDP?
395
396
397import java.io.*;
398import java.net.*;
399class UDPServer
400{
401public static DatagramSocket serversocket;
402public static DatagramPacket dp;
403public static BufferedReader dis;
404public static InetAddress ia;
405public static byte buf[] = new byte[1024];
406public static int cport = 789,sport=790;
407public static void main(String[] a) throws IOException
408{
409serversocket = new DatagramSocket(sport);
410dp = new DatagramPacket(buf,buf.length);
411dis = new BufferedReader
412(new InputStreamReader(System.in));
413ia = InetAddress.getLocalHost();
414System.out.println("Server is Running...");
415while(true)
416{
417serversocket.receive(dp);
418String str = new String(dp.getData(), 0,
419dp.getLength());
420if(str.equals("STOP"))
421{
422System.out.println("Terminated...");
423break;
424}
425System.out.println("Client: " + str);
426String str1 = new String(dis.readLine());
427buf = str1.getBytes();
428serversocket.send(new
429DatagramPacket(buf,str1.length(), ia, cport));
430}
431}
432}
433
434
435
436import java.io.*;
437import java.net.*;
438class UDPClient
439{
440public static DatagramSocket clientsocket;
441public static DatagramPacket dp;
442public static BufferedReader dis;
443public static InetAddress ia;
444public static byte buf[] = new byte[1024];
445public static int cport = 789, sport = 790;
446public static void main(String[] a) throws IOException
447{
448clientsocket = new DatagramSocket(cport);
449dp = new DatagramPacket(buf, buf.length);
450dis = new BufferedReader(new
451InputStreamReader(System.in));
452ia = InetAddress.getLocalHost();
453System.out.println("Client is Running... Type \'STOP\'
454to Quit");
455while(true)
456{
457String str = new String(dis.readLine());
458buf = str.getBytes();
459if(str.equals("STOP"))
460{
461System.out.println("Terminated...");
462clientsocket.send(new
463DatagramPacket(buf,str.length(), ia,
464sport));
465break;
466}
467clientsocket.send(new DatagramPacket(buf,
468str.length(), ia, sport));
469clientsocket.receive(dp);
470String str2 = new String(dp.getData(), 0,
471dp.getLength());
472System.out.println("Server: " + str2);
473}
474}
475}
476
477
478
479
480
481
482
483
4844-> Write a java program to develop a simple chat application?
485
486import java.io.*;
487import java.net.*;
488public class EchoServer
489{
490public static void main(String args[]) throws Exception
491{
492try
493{
494int Port;
495BufferedReader Buf =new BufferedReader(new
496InputStreamReader(System.in));
497System.out.print(" Enter the Port Address : " );
498Port=Integer.parseInt(Buf.readLine());
499ServerSocket sok =new ServerSocket(Port);
500System.out.println(" Server is Ready To Receive a Message. ");
501System.out.println(" Waiting ..... ");
502Socket so=sok.accept();
503if(so.isConnected()==true)
504System.out.println(" Client Socket is Connected Succecfully. ");
505InputStream in=so.getInputStream();
506OutputStream ou=so.getOutputStream();
507PrintWriter pr=new PrintWriter(ou);
508BufferedReader buf=new BufferedReader(new
509InputStreamReader(in));
510String str=buf.readLine();
511System.out.println(" Message Received From Client : " + str);
512System.out.println(" This Message is Forwarded To Client. ");
513pr.println(str);
514pr.flush();
515}
516catch(Exception e)
517{
518System.out.println(" Error : " + e.getMessage());
519}
520}
521}
522
523
524
525import java.io.*;
526import java.net.*;
527public class EchoClient
528{
529public static void main(String args[]) throws Exception
530{
531try {
532int Port;
533BufferedReader Buf =new BufferedReader(new
534InputStreamReader(System.in));
535System.out.print(" Enter the Port Address : " );
536Port=Integer.parseInt(Buf.readLine());
537Socket sok=new Socket("localhost",Port);
538if(sok.isConnected()==true)
539System.out.println(" Server Socket is Connected Succecfully. ");
540InputStream in=sok.getInputStream();
541OutputStream ou=sok.getOutputStream();
542PrintWriter pr=new PrintWriter(ou);
543BufferedReader buf1=new BufferedReader(new
544InputStreamReader(System.in));
545BufferedReader buf2=new BufferedReader(new
546InputStreamReader(in));
547String str1,str2;
548System.out.print(" Enter the Message : ");
549str1=buf1.readLine();
550pr.println(str1);
551pr.flush();
552System.out.println(" Message Send Successfully. ");
553str2=buf2.readLine();
554System.out.println(" Message From Server : " + str2);
555}
556catch(Exception e)
557{
558System.out.println(" Error : " + e.getMessage());
559}
560}
561}
562
563
5645-> The message entered in the client is sent to the server and the server encodes the message and returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b, b as c …z as a.
565This process is done using the TCP/IP protocol. Write a Java program for the above?
566
567
568
569
570
5716-> The message entered in the client is sent to the server and the server encodes the message and returns it to the client.
572Encoding is done by replacing a character by the character next to it i.e. a as b, b as c …z as a. This process is done using UDP. Write a Java program for the above?
573
574
575
576
5777-> Write a Java program to display the name and address of the computer that we are currently working on?
578
579import java.net.*;
580import java.util.*;
581import java.lang.*;
582public class ip
583{
584public static void main(String args[])throws UnknownHostException
585{
586InetAddress Address=InetAddress.getLocalHost();
587System.out.println(Address);
588System.out.println(Address.getHostAddress());
589System.out.println(Address.getHostName());
590}
591}
592
593
594
595
596
5978-> The client accepts IP header information in hexadecimal and sends to the server. The server receives those details and examines its checksum field.
598Write a Java program for this scenario?
599
600
601
602
6039-> Using threading concepts, write a Java program to create a daemon process?
604
605import java.util.*;
606import java.net.*;
607public class TestDaemonThread1 extends Thread{
608 public void run(){
609 if(Thread.currentThread().isDaemon()){//checking for daemon thread
610 System.out.println("daemon thread work");
611 }
612 else{
613 System.out.println("user thread work");
614 }
615 }
616 public static void main(String[] args){
617 TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
618 TestDaemonThread1 t2=new TestDaemonThread1();
619 TestDaemonThread1 t3=new TestDaemonThread1();
620
621 t1.setDaemon(true);//now t1 is daemon thread
622
623 t1.start();//starting threads
624 t2.start();
625 t3.start();
626 }
627}
628
629
630
631
63210-> A server should run for 10 secs and generate numbers continuously. The client connecting to it
633should read data and find out the sum of the data thus read. Write a Java program to implement this scenario.?
634
635
636
637
638
639
640
64111-> Java Program to Send a Message from Client to Server and Receive a Response Back Using Socket Programming?
642
643
644import java.io.BufferedReader;
645import java.io.BufferedWriter;
646import java.io.InputStream;
647import java.io.InputStreamReader;
648import java.io.OutputStream;
649import java.io.OutputStreamWriter;
650import java.net.ServerSocket;
651import java.net.Socket;
652
653public class Server
654{
655
656 private static Socket socket;
657
658 public static void main(String[] args)
659 {
660 try
661 {
662
663 int port = 25000;
664 ServerSocket serverSocket = new ServerSocket(port);
665 System.out.println("Server Started and listening to the port 25000");
666
667 //Server is running always. This is done using this while(true) loop
668 while(true)
669 {
670 //Reading the message from the client
671 socket = serverSocket.accept();
672 InputStream is = socket.getInputStream();
673 InputStreamReader isr = new InputStreamReader(is);
674 BufferedReader br = new BufferedReader(isr);
675 String number = br.readLine();
676 System.out.println("Message received from client is "+number);
677
678 //Multiplying the number by 2 and forming the return message
679 String returnMessage;
680 try
681 {
682 int numberInIntFormat = Integer.parseInt(number);
683 int returnValue = numberInIntFormat*2;
684 returnMessage = String.valueOf(returnValue) + "\n";
685 }
686 catch(NumberFormatException e)
687 {
688 //Input was not a number. Sending proper message back to client.
689 returnMessage = "Please send a proper number\n";
690 }
691
692 //Sending the response back to the client.
693 OutputStream os = socket.getOutputStream();
694 OutputStreamWriter osw = new OutputStreamWriter(os);
695 BufferedWriter bw = new BufferedWriter(osw);
696 bw.write(returnMessage);
697 System.out.println("Message sent to the client is "+returnMessage);
698 bw.flush();
699 }
700 }
701 catch (Exception e)
702 {
703 e.printStackTrace();
704 }
705 finally
706 {
707 try
708 {
709 socket.close();
710 }
711 catch(Exception e){}
712 }
713 }
714}
715
716
717
718import java.io.BufferedReader;
719import java.io.BufferedWriter;
720import java.io.InputStream;
721import java.io.InputStreamReader;
722import java.io.OutputStream;
723import java.io.OutputStreamWriter;
724import java.net.InetAddress;
725import java.net.Socket;
726
727public class Client
728{
729
730 private static Socket socket;
731
732 public static void main(String args[])
733 {
734 try
735 {
736 String host = "localhost";
737 int port = 25000;
738 InetAddress address = InetAddress.getByName(host);
739 socket = new Socket(address, port);
740
741 //Send the message to the server
742 OutputStream os = socket.getOutputStream();
743 OutputStreamWriter osw = new OutputStreamWriter(os);
744 BufferedWriter bw = new BufferedWriter(osw);
745
746 String number = "2";
747
748 String sendMessage = number + "\n";
749 bw.write(sendMessage);
750 bw.flush();
751 System.out.println("Message sent to the server : "+sendMessage);
752
753 //Get the return message from the server
754 InputStream is = socket.getInputStream();
755 InputStreamReader isr = new InputStreamReader(is);
756 BufferedReader br = new BufferedReader(isr);
757 String message = br.readLine();
758 System.out.println("Message received from the server : " +message);
759 }
760 catch (Exception exception)
761 {
762 exception.printStackTrace();
763 }
764 finally
765 {
766 //Closing the socket
767 try
768 {
769 socket.close();
770 }
771 catch(Exception e)
772 {
773 e.printStackTrace();
774 }
775 }
776 }
777}
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
79611-> A java program to implement an echo UDP server?
797
798
799
800import java.net.*;
801import java.util.*;
802
803public class ClientEcho
804{
805 public static void main( String args[] ) throws Exception
806 {
807 InetAddress add = InetAddress.getByName("snrao");
808
809 DatagramSocket dsock = new DatagramSocket( );
810 String message1 = "This is client calling";
811 byte arr[] = message1.getBytes( );
812 DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
813 dsock.send(dpack); // send the packet
814 Date sendTime = new Date(); // note the time of sending the message
815
816 dsock.receive(dpack); // receive the packet
817 String message2 = new String(dpack.getData( ));
818 Date receiveTime = new Date( ); // note the time of receiving the message
819 System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
820 }
821}
822
823
824import java.net.*;
825import java.util.*;
826public class ServerEcho
827{
828 public static void main( String args[]) throws Exception
829 {
830 DatagramSocket dsock = new DatagramSocket(7);
831 byte arr1[] = new byte[150];
832 DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
833
834 while(true)
835 {
836 dsock.receive(dpack);
837
838 byte arr2[] = dpack.getData();
839 int packSize = dpack.getLength();
840 String s2 = new String(arr2, 0, packSize);
841
842 System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
843 dsock.send(dpack);
844 }
845 } }
846
847
848
849
85012-> Random number generation in java?
851
852import java.util.*;
853
854class RandomNumbers {
855 public static void main(String[] args) {
856 int c;
857 Random t = new Random();
858
859 // random integers in [0, 100]
860
861 for (c = 1; c <= 10; c++) {
862 System.out.println(t.nextInt(100));
863 }
864 }
865}
866
867
868
869
870
871Question-Client which sends a text and server recieves it??
872
873
874File: MyServer.java
875
876import java.io.*;
877import java.net.*;
878public class MyServer {
879public static void main(String[] args){
880try{
881ServerSocket ss=new ServerSocket(6666);
882Socket s=ss.accept();//establishes connection
883DataInputStream dis=new DataInputStream(s.getInputStream());
884String str=(String)dis.readUTF();
885System.out.println("message= "+str);
886ss.close();
887}catch(Exception e){System.out.println(e);}
888}
889}
890File: MyClient.java
891
892import java.io.*;
893import java.net.*;
894public class MyClient {
895public static void main(String[] args) {
896try{
897Socket s=new Socket("localhost",6666);
898DataOutputStream dout=new DataOutputStream(s.getOutputStream());
899dout.writeUTF("Hello Server");
900dout.flush();
901dout.close();
902s.close();
903}catch(Exception e){System.out.println(e);}
904}
905}
906
907
908Question-URL listing
909
910
911//URLDemo.java
912import java.io.*;
913import java.net.*;
914public class URLDemo{
915public static void main(String[] args){
916try{
917URL url=new URL("http://www.javatpoint.com/java-tutorial");
918
919System.out.println("Protocol: "+url.getProtocol());
920System.out.println("Host Name: "+url.getHost());
921System.out.println("Port Number: "+url.getPort());
922System.out.println("File Name: "+url.getFile());
923
924}catch(Exception e){System.out.println(e);}
925}
926}
927
928
929
930Question-Displaying all the contents of the web page???
931
932
933import java.io.*;
934import java.net.*;
935public class URLConnectionExample {
936public static void main(String[] args){
937try{
938URL url=new URL("http://www.javatpoint.com/java-tutorial");
939URLConnection urlcon=url.openConnection();
940InputStream stream=urlcon.getInputStream();
941int i;
942while((i=stream.read())!=-1){
943System.out.print((char)i);
944}
945}catch(Exception e){System.out.println(e);}
946}
947}
948
949
950
9514.Getting header of the URL?
952
953
954import java.io.*;
955import java.net.*;
956public class HttpURLConnectionDemo{
957public static void main(String[] args){
958try{
959URL url=new URL("http://www.javatpoint.com/java-tutorial");
960HttpURLConnection huc=(HttpURLConnection)url.openConnection();
961for(int i=1;i<=8;i++){
962System.out.println(huc.getHeaderFieldKey(i)+" = "+huc.getHeaderField(i));
963}
964huc.disconnect();
965}catch(Exception e){System.out.println(e);}
966}
967}
968
969
970
971
972Question-InetAddress class ?
973
974
975import java.io.*;
976import java.net.*;
977public class InetDemo{
978public static void main(String[] args){
979try{
980InetAddress ip=InetAddress.getByName("www.javatpoint.com");
981
982System.out.println("Host Name: "+ip.getHostName());
983System.out.println("IP Address: "+ip.getHostAddress());
984}catch(Exception e){System.out.println(e);}
985}
986}
987
988
989
990Question-Datagram socket class?
991
992
993import java.net.*;
994public class DSender{
995 public static void main(String[] args) throws Exception {
996 DatagramSocket ds = new DatagramSocket();
997 String str = "Welcome java";
998 InetAddress ip = InetAddress.getByName("127.0.0.1");
999
1000 DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
1001 ds.send(dp);
1002 ds.close();
1003 }
1004}
1005
1006Example of Receiving DatagramPacket by DatagramSocket
1007
1008//DReceiver.java
1009import java.net.*;
1010public class DReceiver{
1011 public static void main(String[] args) throws Exception {
1012 DatagramSocket ds = new DatagramSocket(3000);
1013 byte[] buf = new byte[1024];
1014 DatagramPacket dp = new DatagramPacket(buf, 1024);
1015 ds.receive(dp);
1016 String str = new String(dp.getData(), 0, dp.getLength());
1017 System.out.println(str);
1018 ds.close();
1019 }
1020
1021
1022Question-The following GreetingClient is a client program that connects to a server by using a socket and sends a greeting, and then waits for a response.
1023
1024
1025
1026
1027// File Name GreetingClient.java
1028
1029import java.net.*;
1030import java.io.*;
1031
1032public class GreetingClient
1033{
1034 public static void main(String [] args)
1035 {
1036 String serverName = args[0];
1037 int port = Integer.parseInt(args[1]);
1038 try
1039 {
1040 System.out.println("Connecting to " + serverName +
1041 " on port " + port);
1042 Socket client = new Socket(serverName, port);
1043 System.out.println("Just connected to "
1044 + client.getRemoteSocketAddress());
1045 OutputStream outToServer = client.getOutputStream();
1046 DataOutputStream out = new DataOutputStream(outToServer);
1047 out.writeUTF("Hello from "
1048 + client.getLocalSocketAddress());
1049 InputStream inFromServer = client.getInputStream();
1050 DataInputStream in =
1051 new DataInputStream(inFromServer);
1052 System.out.println("Server says " + in.readUTF());
1053 client.close();
1054 }catch(IOException e)
1055 {
1056 e.printStackTrace();
1057 }
1058 }
1059}
1060Socket Server Example:
1061The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument:
1062
1063// File Name GreetingServer.java
1064
1065import java.net.*;
1066import java.io.*;
1067
1068public class GreetingServer extends Thread
1069{
1070 private ServerSocket serverSocket;
1071
1072 public GreetingServer(int port) throws IOException
1073 {
1074 serverSocket = new ServerSocket(port);
1075 serverSocket.setSoTimeout(10000);
1076 }
1077
1078 public void run()
1079 {
1080 while(true)
1081 {
1082 try
1083 {
1084 System.out.println("Waiting for client on port " +
1085 serverSocket.getLocalPort() + "...");
1086 Socket server = serverSocket.accept();
1087 System.out.println("Just connected to "
1088 + server.getRemoteSocketAddress());
1089 DataInputStream in =
1090 new DataInputStream(server.getInputStream());
1091 System.out.println(in.readUTF());
1092 DataOutputStream out =
1093 new DataOutputStream(server.getOutputStream());
1094 out.writeUTF("Thank you for connecting to "
1095 + server.getLocalSocketAddress() + "\nGoodbye!");
1096 server.close();
1097 }catch(SocketTimeoutException s)
1098 {
1099 System.out.println("Socket timed out!");
1100 break;
1101 }catch(IOException e)
1102 {
1103 e.printStackTrace();
1104 break;
1105 }
1106 }
1107 }
1108 public static void main(String [] args)
1109 {
1110 int port = Integer.parseInt(args[0]);
1111 try
1112 {
1113 Thread t = new GreetingServer(port);
1114 t.start();
1115 }catch(IOException e)
1116 {
1117 e.printStackTrace();
1118 }
1119 }
1120}
1121Compile client and server and then start server as follows:
1122
1123
1124
1125
1126Question- Wap to check whether Ipv4 or Ipv6 address?
1127
1128
1129import java.net.*;
1130import java.util.*;
1131import java.lang.*;
1132public class ipvi
1133{
1134public static void main(String args[])throws UnknownHostException
1135{
1136InetAddress Address=InetAddress.getLocalHost();
1137System.out.println(Address.getHostAddress());
1138if(Address instanceof InetAddress)
1139{
1140System.out.println("......IPV4ADDRESS");
1141}
1142else
1143{
1144System.out.println(".....IPV6ADDRESS");
1145}
1146}
1147}
1148
1149
1150
1151Question-WAP to display parts of URL.
1152
1153import java.io.*;
1154import java.net.*;
1155import java.util.*;
1156
1157class UrlParts
1158{
1159 public static void main(String args[])
1160 {
1161 try
1162 {
1163 URL u = new URL("http://academics.vit.ac.in");
1164 System.out.println("Protocol="+u.getProtocol());
1165 System.out.println("Port="+u.getPort());
1166 System.out.println("Default port="+u.getDefaultPort());
1167 System.out.println("File="+u.getFile());
1168 System.out.println("Path="+u.getPath());
1169 System.out.println("Refference="+u.getRef());
1170 }
1171 catch(Exception e)
1172 {
1173 System.out.println("Error");
1174 }
1175 }
1176}
1177
1178
1179
1180Question- Write a program to list all ports hosting a TCP server in a specified host and identify
1181available servers in well-known ports?
1182
1183import java.net.*;
1184import java.io.*;
1185public class ports{
1186public static void main(String args[])
1187{
1188for(int i=1; i++)
1189{
1190try{
1191Socket s=new Socket("127.0.0.1",i);
1192System.out.println("There is server on port"+i+"of 127.0.0.1");
1193}
1194catch(UnknownHostException e){
1195System.err.println(e);
1196break;
1197}
1198catch(IOException e){
1199}}}}
1200
1201
1202Question- Find which out of the first 1024 ports seems to be hosted tcp srevers on a specified host?
1203(Local host can be default)
1204
1205
1206import java.net.*;
1207import java.io.*;
1208public class portsB{
1209public static void main(String args[])
1210{
1211for(int i=1; i<1024; i++)
1212{
1213try{
1214Socket s=new Socket("127.0.0.1",i);
1215System.out.println("There is server on port"+i+"of 127.0.0.1");
1216}
1217catch(UnknownHostException e){
1218System.err.println(e);
1219break;
1220}
1221catch(IOException e){
1222}}}}
1223
1224
1225
1226Question- Find out which of the ports are above 1024 that seems to be hosted tcp
1227servers on a specified host?
1228
1229import java.net.*;
1230import java.io.*;
1231public class portsB{
1232public static void main(String args[])
1233{
1234for(int i=1024; ; i++)
1235{
1236try{
1237Socket s=new Socket("127.0.0.1",i);
1238System.out.println("There is server on port"+i+"of 127.0.0.1");
1239}
1240catch(UnknownHostException e){
1241System.err.println(e);
1242break;
1243}
1244catch(IOException e){
1245}}}}
1246
1247
1248
1249Question- How to check whether a port is being used or not ?
1250
1251Solution
1252Following example shows how to check whether any port is being used as a server or not by creating a socket object.
1253
1254import java.net.*;
1255import java.io.*;
1256
1257public class Main {
1258 public static void main(String[] args) {
1259 Socket Skt;
1260 String host = "localhost";
1261
1262 if (args.length > 0) {
1263 host = args[0];
1264 }
1265 for (int i = 0; i < 1024; i++) {
1266 try {
1267 System.out.println("Looking for "+ i);
1268 Skt = new Socket(host, i);
1269 System.out.println("There is a server on port " + i + " of " + host);
1270 } catch (UnknownHostException e) {
1271 System.out.println("Exception occured"+ e);
1272 break;
1273 } catch (IOException e) {}
1274 }
1275 }
1276}
1277
1278
1279Cyclesheet 1
1280
1281
12822) Write a Java program to run the basic networking commands
1283
1284import java.net.*;
1285import java.util.*;
1286import java.io.*;
1287public class command
1288{
1289 public static void main(String args[])
1290 throws Exception
1291 {
1292 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
1293 System.out.println("enter command:");
1294 String s=br.readLine();
1295 Process p=Runtime.getRuntime().exec(s);
1296 p.waitFor();
1297 BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
1298 String line=reader.readLine();
1299 while(line!=null)
1300 {
1301 System.out.println(line);
1302 line=reader.readLine();0
1303 }
1304 System.out.println("done!");
1305
1306}
1307}
1308
13093) Write a program to display the name of the computer and its IP address that you are currently working on.
1310
1311import java.net.*;
1312import java.io.*;
1313public class cm
1314{
1315public static void main(String args[])
1316{
1317try
1318{
1319BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
1320System.out.println("Enter the url which you want to find ip address and host name");
1321String str=br.readLine();
1322InetAddress add=InetAddress.getByName(str);
1323System.out.println("Local Host Information");
1324System.out.println("Host Name:"+add.getHostName());
1325System.out.println("IP address:"+add.getHostAddress());
1326}
1327catch(Exception e)
1328{
1329System.out.println(e);
1330}
1331}
1332
1333
13344) Write a program to print the IP address of “www.google.com†all IP addresses of “www.microsoft.comâ€.
1335import java.net.*;
1336import java.io.*;
1337public class ip
1338{
1339public static void main(String args[])
1340throws UnknownHostException {
1341 System.out.println(InetAddress.getByName("www.google.com"));
1342 InetAddress[] inetAddresses=InetAddress.getAllByName("www.microsoft.com");
1343 for(InetAddress ipAddress:inetAddresses)
1344 {
1345 System.out.println(ipAddress);
1346 }
1347}
1348}
1349
13505) Write a program to print all Network Interfaces of “localhostâ€.
1351
1352import java.net.*;
1353import java.util.*;
1354import java.io.*;
1355public class ni
1356{
1357public static void main(String args[]) throws UnknownHostException,SocketException
1358{
1359
1360 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
1361if(interfaces==null)
1362{
1363System.out.println("No network interfaces found");
1364}
1365else
1366{
1367for(NetworkInterface netIf:Collections.list(interfaces))
1368{
1369System.out.println("Display Name:" + netIf.getDisplayName());
1370System.out.println("Name : " + netIf.getName());
1371System.out.println();
1372}
1373}
1374}
1375}
1376
1377
13786) Implement the simple version of “nslookup†utility.
1379
1380import java.net.*;
1381import java.io.*;
1382import java.util.*;
1383public class nslook
1384{
1385public static void main(String args[]) throws Exception
1386{
1387 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
1388 System.out.println("Enter the hostname");
1389 String cmd=br.readLine();
1390 Process p=Runtime.getRuntime().exec("nslookup "+cmd);
1391 p.waitFor();
1392 BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
1393 String line=reader.readLine();
1394 while(line!=null)
1395 {
1396 System.out.println(line);
1397 line=reader.readLine();
1398 }
1399 System.out.println("Done");
1400}
1401}
1402
14037) Write a program to download the contents associated with a HTTP URL and save it in a file.
1404
1405import java.io.*;
1406 import java.net.*;
1407 public class GetJavaUrl
1408 {
1409 public static void main(String[] args)
1410 {
1411 URL u;
1412 InputStream is=null;
1413 DataInputStream dis;
1414 String s;
1415 try
1416 {
1417 u=new URL("ftp://10.30.2.53");
1418 is=u.openStream();
1419 dis = new DataInputStream(new BufferedInputStream(is));
1420 while((s=dis.readLine())!=null)
1421 {
1422 System.out.println(s);
1423 }
1424 }
1425 catch(MalformedURLException mue)
1426 {
1427 System.out.println("404: Error NOT FOUND");
1428 mue.printStackTrace();
1429 System.exit(1);
1430 }
1431 catch(IOException ioe)
1432 {
1433 System.out.println("404: Error NOT FOUND");
1434 ioe.printStackTrace();
1435 System.exit(1);
1436 }
1437 finally
1438 {
1439 try
1440 {
1441 is.close();
1442 }
1443 catch(IOException ioe)
1444 {
1445 }
1446 }
1447 }
1448
1449}
1450
1451ITE 304 Computer Networks Lab
1452
1453Cycle sheet-2
1454
1455
1456
14571. Write a program to list all ports hosting a TCP server in a specified host and identify available servers in well-known ports?
1458
14592. Implement echo server and client in java using TCP sockets.
1460
14613. Implement date server and client in java using TCP sockets.
1462
14634. Write a program to implement a simple message transfer from client to server process using TCP/IP.
1464
14655. Develop a TCP client/server application for transferring a text file from client to server?
1466
14676. Implement a chat server and client in java using TCP sockets.
1468
14697. 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.
1470
14718. Find the physical address of a host when its logical address is known (ARP protocol) using TCP/IP.
1472
14739. Find the logical address of a host when its physical address is known (RARP protocol) using TCP/IP.
1474
147510. Implement Domain Name System (DNS) using TCP/IP
1476
147711. Implement a TCP/IP based Server program in Java to compute the factorial of a number (can contain arbitrary number of digits). Write a client program to test the working of the same.
1478
147912. Find the class of the IP address using TCP/IP.
1480
1481
1482Solutions
1483
1484Q1 Write a program to list all ports hosting a TCP server in a specified host and identify available servers in well-known ports?
1485
1486import java.net.Socket;
1487import java.net.*;
1488
1489public class A {
1490public static void main(String[] args) throws Exception{
1491 try {
1492 for(int i=0;i<1024;i++)
1493 {
1494 Socket s=new Socket("localhost",i);
1495 System.out.println(i);
1496 }
1497 }
1498 catch(Exception e)
1499 {
1500 System.out.println(e);
1501 }
1502}
1503}
1504
1505Q2 Implement echo server and client in java using TCP sockets.
1506
1507
1508server
1509
1510import java.io.*;
1511
1512import java.net.*;
1513
1514
1515
1516public class EchoServer
1517
1518{
1519
1520public static void main(String args[]) throws Exception
1521
1522{
1523
1524try
1525
1526{
1527
1528int Port;
1529
1530BufferedReader Buf =new BufferedReader(new
1531
1532InputStreamReader(System.in));
1533
1534System.out.print(" Enter the Port Address : " );
1535
1536Port=Integer.parseInt(Buf.readLine());
1537
1538ServerSocket sok =new ServerSocket(Port);
1539
1540System.out.println(" Server is Ready To Receive a Message. ");
1541
1542System.out.println(" Waiting ..... ");
1543
1544Socket so=sok.accept();
1545
1546if(so.isConnected()==true)
1547
1548 System.out.println(" Client Socket is Connected Succecfully. ");
1549
1550InputStream in=so.getInputStream();
1551
1552OutputStream ou=so.getOutputStream();
1553
1554PrintWriter pr=new PrintWriter(ou);
1555
1556BufferedReader buf=new BufferedReader(new
1557
1558InputStreamReader(in));
1559
1560String str=buf.readLine();
1561
1562System.out.println(" Message Received From Client : " + str);
1563
1564System.out.println(" This Message is Forwarded To Client. ");
1565
1566pr.println(str);
1567
1568pr.flush();
1569
1570}
1571
1572 catch(Exception e)
1573
1574 {
1575
1576 System.out.println(" Error : " + e.getMessage());
1577
1578 }
1579
1580}
1581
1582}
1583
1584
1585
1586client
1587
1588import java.io.*;
1589
1590import java.net.*;
1591
1592
1593
1594public class EchoClient
1595
1596{
1597
1598public static void main(String args[]) throws Exception
1599
1600{
1601
1602try {
1603
1604int Port;
1605
1606BufferedReader Buf =new BufferedReader(new
1607
1608InputStreamReader(System.in));
1609
1610System.out.print(" Enter the Port Address : " );
1611
1612Port=Integer.parseInt(Buf.readLine());
1613
1614Socket sok=new Socket("localhost",Port);
1615
1616if(sok.isConnected()==true)
1617
1618 System.out.println(" Server Socket is Connected Succecfully. ");
1619
1620InputStream in=sok.getInputStream();
1621
1622OutputStream ou=sok.getOutputStream();
1623
1624PrintWriter pr=new PrintWriter(ou);
1625
1626BufferedReader buf1=new BufferedReader(new
1627
1628InputStreamReader(System.in));
1629
1630
1631BufferedReader buf2=new BufferedReader(new
1632
1633InputStreamReader(in));
1634
1635String str1,str2;
1636
1637System.out.print(" Enter the Message : ");
1638
1639str1=buf1.readLine();
1640
1641pr.println(str1);
1642
1643pr.flush();
1644
1645System.out.println(" Message Send Successfully. ");
1646
1647str2=buf2.readLine();
1648
1649System.out.println(" Message From Server : " + str2);
1650
1651 }
1652
1653 catch(Exception e)
1654
1655 {
1656
1657 System.out.println(" Error : " + e.getMessage());
1658
1659 }
1660
1661}
1662
1663}
1664Q3 Implement date server and client in java using TCP sockets.
1665
1666client
1667
1668import java.io.*;
1669
1670import java.net.*;
1671
1672
1673
1674class DateClient
1675
1676{
1677
1678 public static void main(String args[]) throws Exception
1679
1680 {
1681
1682 Socket soc=new Socket(InetAddress.getLocalHost(),5217);
1683
1684 BufferedReader in=new BufferedReader(new InputStreamReader( soc.getInputStream() ) );
1685
1686 System.out.println(in.readLine());
1687
1688 }
1689
1690}
1691
1692
1693
1694server
1695
1696import java.net.*;
1697
1698import java.io.*;
1699
1700import java.util.*;
1701
1702
1703
1704class DateServer
1705
1706{
1707
1708 public static void main(String args[]) throws Exception
1709
1710 {
1711
1712 ServerSocket s=new ServerSocket(5217);
1713 while(true)
1714
1715 {
1716
1717 System.out.println("Waiting For Connection ...");
1718
1719 Socket soc=s.accept();
1720
1721 DataOutputStream out=new DataOutputStream(soc.getOutputStream());
1722
1723 out.writeBytes("Server Date" + (new Date()).toString() + "\n");
1724
1725 out.close();
1726
1727 soc.close();
1728
1729 }
1730
1731
1732
1733 }
1734
1735}
1736
1737
1738Q4 , Q6 Write a program to implement a simple message transfer from client to server process using TCP/IP.
1739SERVER
1740
1741import java.io.*;
1742import java.io.BufferedReader;
1743import java.io.InputStream;
1744import java.io.InputStreamReader;
1745import java.io.OutputStream;
1746import java.io.PrintWriter;
1747import java.net.*;
1748public class GossipServer
1749{
1750 public static void main(String[] args) throws Exception
1751 {
1752 ServerSocket sersock = new ServerSocket(3000);
1753 System.out.println("Server ready for chatting");
1754 Socket sock = sersock.accept( );
1755 // reading from keyboard (keyRead object)
1756 BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
1757 // sending to client (pwrite object)
1758 OutputStream ostream = sock.getOutputStream();
1759 PrintWriter pwrite = new PrintWriter(ostream, true);
1760
1761 // receiving from server ( receiveRead object)
1762 InputStream istream = sock.getInputStream();
1763 BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
1764
1765 String receiveMessage, sendMessage;
1766 while(true)
1767 {
1768 if((receiveMessage = receiveRead.readLine()) != null)
1769 {
1770 System.out.println(receiveMessage);
1771 }
1772 sendMessage = keyRead.readLine();
1773 pwrite.println(sendMessage);
1774 pwrite.flush();
1775 }
1776 }
1777}
1778
1779
17805 Develop a TCP client/server application for transferring a text file from client to server?
1781Server:
1782import java.io.BufferedInputStream;
1783import java.io.File;
1784import java.io.FileInputStream;
1785import java.io.OutputStream;
1786import java.net.InetAddress;
1787import java.net.ServerSocket;
1788import java.net.Socket;
1789
1790public class FileTransferServer {
1791
1792 public static void main(String[] args) throws Exception {
1793 //Initialize Sockets
1794 ServerSocket ssock = new ServerSocket(5000);
1795 Socket socket = ssock.accept();
1796
1797 //The InetAddress specification
1798 InetAddress IA = InetAddress.getByName("localhost");
1799
1800 //Specify the file
1801 File file = new File("/home/likewise-open/VITUNIVERSITY/14bit0297/DateServer.java");
1802 FileInputStream fis = new FileInputStream(file);
1803 BufferedInputStream bis = new BufferedInputStream(fis);
1804
1805 //Get socket's output stream
1806 OutputStream os = socket.getOutputStream();
1807
1808 //Read File Contents into contents array
1809 byte[] contents;
1810 long fileLength = file.length();
1811 long current = 0;
1812
1813 long start = System.nanoTime();
1814 while(current!=fileLength){
1815 int size = 10000;
1816 if(fileLength - current >= size)
1817 current += size;
1818 else{
1819 size = (int)(fileLength - current);
1820 current = fileLength;
1821 }
1822 contents = new byte[size];
1823 bis.read(contents, 0, size);
1824 os.write(contents);
1825 System.out.print("Sending file ... "+(current*100)/fileLength+"% complete!");
1826 }
1827
1828 os.flush();
1829 //File transfer done. Close the socket connection!
1830 socket.close();
1831 ssock.close();
1832 System.out.println("File sent succesfully!");
1833 }
1834}
1835
1836Client:
1837import java.io.BufferedOutputStream;
1838import java.io.FileOutputStream;
1839import java.io.InputStream;
1840import java.net.InetAddress;
1841import java.net.Socket;
1842
1843
1844public class FileTransferClient {
1845
1846 public static void main(String[] args) throws Exception{
1847
1848 //Initialize socket
1849 Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);
1850 byte[] contents = new byte[10000];
1851
1852 //Initialize the FileOutputStream to the output file's full path.
1853 FileOutputStream fos = new FileOutputStream("e:\\data2.bin");
1854 BufferedOutputStream bos = new BufferedOutputStream(fos);
1855 InputStream is = socket.getInputStream();
1856
1857 //No of bytes read in one read() call
1858 int bytesRead = 0;
1859
1860 while((bytesRead=is.read(contents))!=-1)
1861 bos.write(contents, 0, bytesRead);
1862
1863 bos.flush();
1864 socket.close();
1865
1866 System.out.println("File saved successfully!");
1867 }
1868}
1869OUTPUT
1870
1871
1872CLINET
1873
1874import java.io.*;
1875import java.net.*;
1876public class GossipClient
1877{
1878 public static void main(String[] args) throws Exception
1879 {
1880 Socket sock = new Socket("127.0.0.1", 3000);
1881 // reading from keyboard (keyRead object)
1882 BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
1883 // sending to client (pwrite object)
1884 OutputStream ostream = sock.getOutputStream();
1885 PrintWriter pwrite = new PrintWriter(ostream, true);
1886
1887 // receiving from server ( receiveRead object)
1888 InputStream istream = sock.getInputStream();
1889 BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
1890
1891 System.out.println("Start the chitchat, type and press Enter key");
1892
1893 String receiveMessage, sendMessage;
1894 while(true)
1895 {
1896 sendMessage = keyRead.readLine(); // keyboard reading
1897 pwrite.println(sendMessage); // sending to server
1898 pwrite.flush(); // flush the data
1899 if((receiveMessage = receiveRead.readLine()) != null) //receive from server
1900 {
1901 System.out.println(receiveMessage); // displaying at DOS prompt
1902 }
1903 }
1904 }
1905}
1906Q7 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.
1907
1908
1909CLIENT
1910
1911import java.io.*;
1912import java.net.*;
1913class TCPClientAuth {
1914
1915 public static void main(String argv[]) throws Exception
1916 {
1917 String username;
1918 String password;
1919 String auth;
1920
1921 BufferedReader inFromUser =
1922 new BufferedReader(new InputStreamReader(System.in));
1923
1924 Socket clientSocket = new Socket("localhost", 6789);
1925
1926 DataOutputStream outToServer =
1927 new DataOutputStream(clientSocket.getOutputStream());
1928
1929 BufferedReader inFromServer =
1930 new BufferedReader(new
1931 InputStreamReader(clientSocket.getInputStream()));
1932
1933 System.out.print("Username :");
1934 username = inFromUser.readLine();
1935
1936 System.out.print("Password :");
1937 password = inFromUser.readLine();
1938
1939
1940 outToServer.writeBytes(username + '\n' + password + '\n' );
1941
1942 auth = inFromServer.readLine();
1943
1944 System.out.println("FROM SERVER: " + auth);
1945
1946 clientSocket.close();
1947
1948 }
1949}
1950
1951SERVER
1952
1953
1954import java.io.*;
1955import java.net.*;
1956
1957class TCPServerAuth {
1958
1959 public static void main(String argv[]) throws Exception
1960 {
1961 String username;
1962 String password;
1963 String auth;
1964
1965 ServerSocket welcomeSocket = new ServerSocket(6789);
1966
1967 while(true) {
1968
1969 Socket connectionSocket = welcomeSocket.accept();
1970
1971 BufferedReader inFromClient =
1972 new BufferedReader(new
1973 InputStreamReader(connectionSocket.getInputStream()));
1974
1975
1976
1977 DataOutputStream outToClient =
1978 new DataOutputStream(connectionSocket.getOutputStream());
1979
1980 username = inFromClient.readLine();
1981 password = inFromClient.readLine();
1982
1983
1984 if (username.equals("Admin")){
1985 if (password.equals("root")){
1986 auth = "Login Successful!";
1987 }else{
1988 auth = "Wrong Password";
1989 }
1990 }else{
1991 auth = "Wrong Username";
1992 }
1993
1994 outToClient.writeBytes(auth);
1995 connectionSocket.close();
1996 }
1997 }
1998}
1999
2000Q8.Find the physical address of a host when its logical address is known (ARP protocol) using TCP/IP.
2001
2002CLIENT:
2003import java.io.*;
2004import java.util.*;
2005import java.net.*;
2006
2007public class q8client
2008{
2009public static void main(String[] args)
2010{
2011try
2012{
2013Socket s = new Socket("localhost",5555);
2014BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
2015DataOutputStream dout = new DataOutputStream(s.getOutputStream());
2016DataInputStream din = new DataInputStream(s.getInputStream());
2017System.out.println("Enter the logical address: ");
2018String str = b.readLine();
2019dout.writeBytes(str + "\n");
2020String str1 = din.readLine();
2021System.out.println("Physical Address of str: " + str1);
2022s.close();
2023}
2024catch (Exception e)
2025{
2026}
2027}}
2028
2029SERVER:
2030
2031import java.io.*;
2032import java.util.*;
2033import java.net.*;
2034
2035public class q8server
2036{
2037public static void main(String[] args)
2038{
2039try
2040{
2041ServerSocket ss = new ServerSocket(5555);
2042Socket s = ss.accept();
2043BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
2044DataOutputStream dout = new DataOutputStream(s.getOutputStream());
2045DataInputStream din = new DataInputStream(s.getInputStream());
2046String str = din.readLine();
2047String logical[] = {"172.16.27.34","195.24.54.204"};
2048String physical[] = {"8E:AE:08:AA","0A:78:6C:AD"};
2049for(int i=0;i<logical.length;i++)
2050{
2051if (str.equals(logical[i]))
2052{
2053dout.writeBytes(physical[i] + "\n");
2054break;
2055}
2056}
2057s.close();
2058}
2059catch (Exception e)
2060{
2061}
2062}
2063}
2064
2065
2066OUTPUT:
2067
2068
2069=====================================================
2070
2071
2072
2073
2074Q9.Find the logical address of a host when its physical address is known (RARP protocol) using TCP/IP.
2075
2076CLIENT:
2077import java.io.*;
2078import java.net.*;
2079class TCPLogClient {
2080
2081 public static void main(String argv[]) throws Exception
2082 {
2083 String logicAdd;
2084 String phyAdd;
2085
2086 BufferedReader inFromUser =
2087 new BufferedReader(new InputStreamReader(System.in));
2088
2089 Socket clientSocket = new Socket("localhost", 6789);
2090
2091 DataOutputStream outToServer =
2092 new DataOutputStream(clientSocket.getOutputStream());
2093
2094 BufferedReader inFromServer =
2095 new BufferedReader(new
2096 InputStreamReader(clientSocket.getInputStream()));
2097
2098 System.out.println("Enter Physical address of host");
2099 phyAdd = inFromUser.readLine();
2100
2101 outToServer.writeBytes(phyAdd + '\n');
2102
2103 logicAdd = inFromServer.readLine();
2104
2105 System.out.println("FROM SERVER: Logical Address " + logicAdd);
2106
2107 clientSocket.close();
2108
2109 }
2110}
2111
2112SERVER:
2113import java.io.*;
2114import java.net.*;
2115
2116class TCPLogServer {
2117
2118 public static void main(String argv[]) throws Exception
2119 {
2120 String phyAdd;
2121
2122
2123 ServerSocket welcomeSocket = new ServerSocket(6789);
2124
2125 while(true) {
2126
2127 Socket connectionSocket = welcomeSocket.accept();
2128
2129 BufferedReader inFromClient =
2130 new BufferedReader(new
2131 InputStreamReader(connectionSocket.getInputStream()));
2132
2133
2134 String logical[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
2135 String physical[] = {"8E:AE:08:AA","0A:78:6C:AD", "d4:6d:50:84:db:c8"};
2136
2137 DataOutputStream outToClient =
2138 new DataOutputStream(connectionSocket.getOutputStream());
2139
2140 phyAdd = inFromClient.readLine();
2141 for(int i = 0; i < physical.length; i++)
2142 {
2143 if (phyAdd.equals(physical[i]))
2144 {
2145 outToClient.writeBytes(logical[i] + "\n");
2146 connectionSocket.close();
2147 }
2148 }
2149
2150
2151 }
2152 }
2153}
2154
2155=======================================
2156
2157
215810.Implement Domain Name System (DNS) using TCP/IP
2159
2160SERVER:
2161import java.io.*;
2162import java.net.*;
2163class TCPLoServer {
2164 public static void main(String argv[]) throws Exception
2165 {
2166 String D;
2167 ServerSocket welcomeSocket = new ServerSocket(6789);
2168 while(true) {
2169 Socket connectionSocket = welcomeSocket.accept();
2170BufferedReader inFromClient =
2171 new BufferedReader(new
2172 InputStreamReader(connectionSocket.getInputStream()));
2173String hostaddr[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
2174 String DNS[] = {"www.google.com","www.facebook.com", "www.vit.ac.in"};
2175 DataOutputStream outToClient =
2176 new DataOutputStream(connectionSocket.getOutputStream());
2177 D = inFromClient.readLine();
2178for(int i = 0; i < hostaddr.length; i++)
2179 {
2180 if (D.equals(hostaddr[i]))
2181 {
2182 outToClient.writeBytes(DNS[i] + "\n");
2183 connectionSocket.close();
2184 }
2185 }
2186
2187
2188 }
2189 }
2190}
2191
2192
2193
2194CLIENT:
2195import java.io.*;
2196import java.net.*;
2197class TCPLoClient {
2198
2199 public static void main(String argv[]) throws Exception
2200 {
2201 String hostaddr;
2202 String DNS;
2203
2204 BufferedReader inFromUser =
2205 new BufferedReader(new InputStreamReader(System.in));
2206
2207 Socket clientSocket = new Socket("localhost", 6789);
2208
2209 DataOutputStream outToServer =
2210 new DataOutputStream(clientSocket.getOutputStream());
2211
2212 BufferedReader inFromServer =
2213 new BufferedReader(new
2214 InputStreamReader(clientSocket.getInputStream()));
2215
2216 System.out.println("Enter IP address of host");
2217 DNS = inFromUser.readLine();
2218
2219 outToServer.writeBytes(DNS + '\n');
2220
2221 hostaddr = inFromServer.readLine();
2222
2223 System.out.println("FROM SERVER: Logical Address " + hostaddr);
2224
2225 clientSocket.close();
2226
2227 }
2228}
2229
2230
2231
2232*************************************************
2233
223411.Implement a TCP/IP based Server program in Java to compute the factorial of a number (can contain arbitrary number of digits). Write a client program to test the working of the same.
2235
2236CLIENT:
2237import java.io.*;
2238import java.net.*;
2239class client {
2240 public static void main(String argv[]) throws Exception
2241 {
2242 String sentence;
2243 String modifiedSentence;
2244 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2245 Socket clientSocket = new Socket("localhost", 2222);
2246 DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
2247 BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
2248 System.out.println("Enter a number - ");
2249 sentence = inFromUser.readLine();
2250 outToServer.writeBytes(sentence + '\n');
2251 modifiedSentence = inFromServer.readLine();
2252 System.out.println ("Factorial: " + modifiedSentence );
2253 clientSocket.close();
2254 }
2255}
2256SREVER:
2257
2258import java.io.*;
2259import java.net.*;
2260class serv {
2261 public static void main(String argv[]) throws Exception
2262 {
2263 String clientSentence;
2264 String capitalizedSentence;
2265 ServerSocket welcomeSocket = new ServerSocket(2222);
2266 Socket connectionSocket = welcomeSocket.accept();
2267 BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
2268 DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream());
2269 clientSentence = inFromClient.readLine();
2270 int num = Integer.parseInt(clientSentence);
2271 int fact=1;
2272 for(int i=1;i<=num;i++)
2273 fact=fact*i;
2274 System.out.println("Factorial : "+fact);
2275 capitalizedSentence = String.valueOf(fact);
2276 //capitalizedSentence = clientSentence.toUpperCase() + '\n';
2277 outToClient.writeBytes(capitalizedSentence);
2278 }
2279}
2280
2281******************************************************
2282
2283Ques12.Find the class of the IP address using TCP/IP.
2284import java.io.*;
2285import java.net.*;
2286
2287class TCPIpclassServer {
2288
2289 public static void main(String argv[]) throws Exception
2290 {
2291 String ip;
2292 int fbyte;
2293 String ipclass = "Invalid";
2294
2295
2296 ServerSocket welcomeSocket = new ServerSocket(6789);
2297
2298 while(true) {
2299
2300 Socket connectionSocket = welcomeSocket.accept();
2301
2302 BufferedReader inFromClient =
2303 new BufferedReader(new
2304 InputStreamReader(connectionSocket.getInputStream()));
2305
2306
2307 DataOutputStream outToClient =
2308 new DataOutputStream(connectionSocket.getOutputStream());
2309
2310 ip = inFromClient.readLine();
2311 int index;
2312 index = ip.indexOf(".");
2313 ip = ip.substring(0, index);
2314
2315
2316 fbyte = Integer.parseInt(ip);
2317 if (fbyte >= 0 && fbyte <= 127)
2318 ipclass = "Class A";
2319 else if (fbyte >= 128 && fbyte <= 191)
2320 ipclass = "Class B";
2321 else if (fbyte >= 192 && fbyte <= 223)
2322 ipclass = "Class C";
2323 else if (fbyte >= 224 && fbyte <= 239)
2324 ipclass = "Class D";
2325 else if (fbyte >= 240 && fbyte <= 255)
2326 ipclass = "Class E";
2327
2328 outToClient.writeBytes(ipclass + "\n");
2329 connectionSocket.close();
2330
2331 }
2332 }
2333}
2334
2335//client
2336
2337import java.io.*;
2338import java.net.*;
2339class TCPIpclassClient {
2340
2341 public static void main(String argv[]) throws Exception
2342 {
2343 String ip;
2344 String ipclass;
2345
2346 BufferedReader inFromUser =
2347 new BufferedReader(new InputStreamReader(System.in));
2348
2349 Socket clientSocket = new Socket("localhost", 6789);
2350
2351 DataOutputStream outToServer =
2352 new DataOutputStream(clientSocket.getOutputStream());
2353
2354 BufferedReader inFromServer =
2355 new BufferedReader(new
2356 InputStreamReader(clientSocket.getInputStream()));
2357
2358 System.out.println("Enter an IP address: ");
2359 ip = inFromUser.readLine();
2360
2361 outToServer.writeBytes(ip + '\n');
2362
2363 ipclass = inFromServer.readLine();
2364
2365 System.out.println("FROM SERVER: Class for IP: "+ ip + " is : " + ipclass);
2366
2367 clientSocket.close();
2368
2369 }
2370}
2371
2372CYCLESHEET 3
2373
23741. Implement echo server and client in java using UDP sockets.
2375
23762. Write a program to implement a text based message transfer from client to server process using UDP.
2377
23783. Implement a chat server and client in java using UDP sockets.
2379
23804. Implement a DNS server and client in java using UDP sockets.
2381
23825. Find the logical address of a host when its physical address is known (RARP protocol) using UDP.
2383
23846. Find the physical address of a host when its logical address is known (ARP protocol) using UDP.
2385
23867. Implement Client - Server communication to access Date using UDP in Java.
2387
2388Solutions :
2389
23901. Implement echo server and client in java using UDP sockets.
2391Code:
2392Server:
2393import java.io.*;
2394import java.net.*;
2395class UDPServer {
2396public static void main(String args[]) throws Exception
2397{
2398DatagramSocket serverSocket = new DatagramSocket(9876);
2399byte[] receiveData = new byte[1024];
2400byte[] sendData = new byte[1024];
2401while(true)
2402{
2403DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2404serverSocket.receive(receivePacket);
2405String sentence = new String(receivePacket.getData());
2406InetAddress IPAddress = receivePacket.getAddress();
2407int port = receivePacket.getPort();
2408String capitalizedSentence = sentence.toUpperCase();
2409sendData = capitalizedSentence.getBytes();
2410DatagramPacket sendPacket =new DatagramPacket(sendData, sendData.length, IPAddress,port);
2411serverSocket.send(sendPacket);
2412}
2413}
2414}
2415
2416Client
2417import java.io.*;
2418import java.net.*;
2419
2420class UDPClient {
2421public static void main(String args[]) throws Exception
2422{
2423BufferedReader inFromUser =
2424new BufferedReader(new InputStreamReader(System.in));
2425DatagramSocket clientSocket = new DatagramSocket();
2426InetAddress IPAddress = InetAddress.getByName("");
2427byte[] sendData = new byte[1024];
2428byte[] receiveData = new byte[1024];
2429String sentence = inFromUser.readLine();
2430sendData = sentence.getBytes();
2431DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
2432clientSocket.send(sendPacket);
2433DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2434clientSocket.receive(receivePacket);
2435String modifiedSentence = new String(receivePacket.getData());
2436System.out.println("FROM SERVER:" + modifiedSentence);
2437clientSocket.close();
2438}
2439}
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465Q. Write a program to implement a text based message transfer from client to server process using UDP.
2466
2467Code:
2468Server:
2469import java.net.DatagramPacket;
2470import java.net.DatagramSocket;
2471public class UDPaServer
2472{
2473 public static void main(String args[])
2474 {
2475 int server_port = 1111;
2476 System.out.println("UDP Server Listening in " + server_port);
2477 try
2478 {
2479 // DatagramSocket created and listening in Port 1111
2480 DatagramSocket socket = new DatagramSocket(server_port);
2481 byte[] msgBuffer = new byte[1024];
2482
2483 // DatagramPacket for receiving the incoming data from UDP Client
2484 DatagramPacket packet = new DatagramPacket(msgBuffer, msgBuffer.length);
2485
2486 while (true)
2487 {
2488 socket.receive(packet);
2489 String message = new String(msgBuffer, 0, packet.getLength());
2490 System.out.println("UDPServer: Message received = " + message);
2491 packet.setLength(msgBuffer.length);
2492 }
2493 }
2494 catch (Exception e)
2495 {
2496 e.printStackTrace();
2497 System.out.println("Error in getting the Data from UDP Client");
2498 }
2499 }
2500}
2501
2502Client:
2503import java.net.DatagramPacket;
2504import java.net.DatagramSocket;
2505import java.net.InetAddress;
2506import java.io.*;
2507import java.net.*;
2508
2509public class UDPaClient
2510{
2511 public static void main(String args[])
2512 {
2513 try
2514 {
2515 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2516 String server_address = "localhost";
2517 int server_port = 1111;
2518 String message = inFromUser.readLine();
2519 InetAddress address = InetAddress.getByName(server_address);
2520 DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, address, server_port);
2521 DatagramSocket socket = new DatagramSocket();
2522 socket.send(packet);
2523
2524 System.out.println("UDPClient: Sent data to Server ; Message = " + message);
2525 socket.close();
2526 }
2527 catch (Exception e)
2528 {
2529 e.printStackTrace();
2530 System.out.println("Error in sending the Data to UDP Server");
2531 }
2532 }
2533}
2534
2535
2536**********************************************
2537
25383.Implement a chat server and client in java using UDP sockets.
2539
2540//server
2541import java.io.*;
2542import java.net.*;
2543
2544public class UDPChatServer {
2545 public static void main(String args[]) throws Exception
2546 {
2547 DatagramSocket serverSocket = new DatagramSocket(6789);
2548
2549 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2550 String reply;
2551 while(true)
2552 {
2553 byte[] receiveData = new byte[1024];
2554 byte[] sendData = new byte[1024];
2555 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2556 serverSocket.receive(receivePacket);
2557
2558 String msg = new String(receivePacket.getData());
2559 System.out.println("Message Received: " + msg);
2560
2561 InetAddress IPAddress = receivePacket.getAddress();
2562 int port = receivePacket.getPort();
2563
2564 System.out.printf("Enter reply: ");
2565 reply = inFromUser.readLine();
2566 if (reply.equals("bye"))
2567 {
2568 System.out.printf("Exiting Chat!");
2569 serverSocket.close();
2570 break;
2571 }
2572 sendData = reply.getBytes();
2573 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
2574 serverSocket.send(sendPacket);
2575 }
2576
2577 }
2578}
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589//client
2590import java.io.*;
2591import java.net.*;
2592
2593public class UDPChatClient {
2594 public static void main(String args[]) throws Exception
2595 {
2596 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2597
2598 InetAddress IPAddress = InetAddress.getByName("localhost");
2599
2600 String msg = "";
2601 while(true)
2602 {
2603 byte[] sendData = new byte[1024];
2604 byte[] receiveData = new byte[1024];
2605 DatagramSocket clientSocket = new DatagramSocket();
2606 System.out.printf("Send a Message: ");
2607 msg = inFromUser.readLine();
2608 if (msg.equals("bye"))
2609 {
2610 System.out.printf("Exiting Chat!");
2611 clientSocket.close();
2612 break;
2613 }
2614 sendData = msg.getBytes();
2615
2616 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
2617 clientSocket.send(sendPacket);
2618
2619 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2620 clientSocket.receive(receivePacket);
2621
2622 String reply = new String(receivePacket.getData());
2623 System.out.println("Reply: " + reply);
2624 clientSocket.close();
2625 }
2626 }
2627}
2628
2629
2630****************************************
2631
2632
2633
26344. Implement a DNS server and client in java using UDP sockets.
2635
2636//server
2637import java.io.*;
2638import java.net.*;
2639import java.util.*;
2640class Serverdns12
2641{
2642
2643 public static void main(String args[])
2644 {
2645 try
2646 {
2647 DatagramSocket server=new DatagramSocket(1309);
2648 while(true)
2649 {
2650 byte[] sendbyte=new byte[1024];
2651 byte[] receivebyte=new byte[1024];
2652 DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
2653 server.receive(receiver);
2654 String str=new String(receiver.getData());
2655 String s=str.trim();
2656 //System.out.println(s);
2657 InetAddress addr=receiver.getAddress();
2658 int port=receiver.getPort();
2659 String ip[]={"165.165.80.80","165.165.79.1"};
2660 String name[]={"www.aptitudeguru.com","www.downloadcyclone.blogspot.com"};
2661 for(int i=0;i<ip.length;i++)
2662 {
2663 if(s.equals(ip[i]))
2664 {
2665 sendbyte=name[i].getBytes();
2666 DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
2667 server.send(sender);
2668 break;
2669 }
2670 else if(s.equals(name[i]))
2671 {
2672 sendbyte=ip[i].getBytes();
2673 DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
2674 server.send(sender);
2675 break;
2676 }
2677 }
2678 break;
2679 }
2680 }
2681 catch(Exception e)
2682 {
2683 System.out.println(e);
2684 }
2685 }
2686}
2687
2688
2689
2690
2691//client
2692
2693import java.io.*;
2694import java.net.*;
2695import java.util.*;
2696class Clientdns12
2697{
2698 public static void main(String args[])
2699 {
2700 try
2701 {
2702 DatagramSocket client=new DatagramSocket();
2703 InetAddress addr=InetAddress.getByName("127.0.0.1");
2704
2705 byte[] sendbyte=new byte[1024];
2706 byte[] receivebyte=new byte[1024];
2707
2708 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
2709 System.out.println("Enter the DOMAIN NAME or IP adress:");
2710 String str=in.readLine();
2711 sendbyte=str.getBytes();
2712 DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
2713 client.send(sender);
2714 DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
2715 client.receive(receiver);
2716 String s=new String(receiver.getData());
2717 System.out.println("IP address or DOMAIN NAME: "+s.trim());
2718 client.close();
2719 }
2720 catch(Exception e)
2721 {
2722 System.out.println(e);
2723 }
2724 }
2725}
2726
2727***************************
2728
2729
2730
2731
2732
27335. Find the logical address of a host when its physical address is known (RARP protocol) using UDP.
2734
2735//SERVER
2736import java.io.*;
2737import java.net.*;
2738
2739public class UDPRARPServer {
2740 public static void main(String args[]) throws Exception
2741 {
2742 DatagramSocket serverSocket = new DatagramSocket(6789);
2743 byte[] receiveData = new byte[1024];
2744 byte[] sendData = new byte[1024];
2745
2746 String logical[] = {"172.16.27.34","195.24.54.204", "10.10.137.1"};
2747 String physical[] = {"8E:AE:08:AA","0A:78:6C:AD", "d4:6d:50:84:db:c8"};
2748 String reply = "";
2749
2750 while(true)
2751 {
2752 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2753 serverSocket.receive(receivePacket);
2754
2755 String phyaddr = new String(receivePacket.getData());
2756 System.out.println("Physical Address received: " + phyaddr);
2757 phyaddr = phyaddr.trim();
2758 InetAddress IPAddress = receivePacket.getAddress();
2759 int port = receivePacket.getPort();
2760
2761 for(int i = 0; i < physical.length; i++)
2762 {
2763 if (phyaddr.equals(physical[i]))
2764 {
2765 reply = logical[i];
2766 sendData = reply.getBytes();
2767 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
2768 serverSocket.send(sendPacket);
2769 }
2770 }
2771
2772 }
2773 }
2774}
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785//CLIENT
2786import java.io.*;
2787import java.net.*;
2788
2789public class UDPRARPClient {
2790 public static void main(String args[]) throws Exception
2791 {
2792 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2793 DatagramSocket clientSocket = new DatagramSocket();
2794 InetAddress IPAddress = InetAddress.getByName("localhost");
2795 byte[] sendData = new byte[1024];
2796 byte[] receiveData = new byte[1024];
2797
2798 System.out.printf("Enter Physical address: ");
2799 String phyaddr = inFromUser.readLine();
2800 sendData = phyaddr.getBytes();
2801
2802 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
2803 clientSocket.send(sendPacket);
2804
2805 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2806 clientSocket.receive(receivePacket);
2807
2808 String logaddr = new String(receivePacket.getData());
2809 System.out.println("Logical Address:" + logaddr);
2810 clientSocket.close();
2811 }
2812}
2813
2814
2815********************************
2816
2817
28186.Find the physical address of a host when its logical address is known (ARP protocol) using UDP.
2819
2820//server
2821import java.io.*;
2822import java.net.*;
2823import java.util.*;
2824class Serverarp12
2825{
2826 public static void main(String args[])
2827 {
2828 try
2829 {
2830 DatagramSocket server=new DatagramSocket(1309);
2831 while(true)
2832 {
2833 byte[] sendbyte=new byte[1024];
2834 byte[] receivebyte=new byte[1024];
2835 DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
2836 server.receive(receiver);
2837 String str=new String(receiver.getData());
2838 String s=str.trim();
2839 //System.out.println(s);
2840 InetAddress addr=receiver.getAddress();
2841 int port=receiver.getPort();
2842 String ip[]={"165.165.80.80","165.165.79.1"};
2843 String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
2844 for(int i=0;i<ip.length;i++)
2845 {
2846 if(s.equals(ip[i]))
2847 {
2848 sendbyte=mac[i].getBytes();
2849 DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
2850 server.send(sender);
2851 break;
2852 }
2853 }
2854 break;
2855
2856
2857 }
2858 }
2859 catch(Exception e)
2860 {
2861 System.out.println(e);
2862 }
2863 }
2864}
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878//client
2879import java.io.*;
2880import java.net.*;
2881import java.util.*;
2882class Clientarp12
2883{
2884 public static void main(String args[])
2885 {
2886 try
2887 {
2888 DatagramSocket client=new DatagramSocket();
2889 InetAddress addr=InetAddress.getByName("127.0.0.1");
2890
2891 byte[] sendbyte=new byte[1024];
2892 byte[] receivebyte=new byte[1024];
2893 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
2894 System.out.println("Enter the logical address (IP):");
2895 String str=in.readLine();
2896 sendbyte=str.getBytes();
2897 DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
2898 client.send(sender);
2899 DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
2900 client.receive(receiver);
2901 String s=new String(receiver.getData());
2902 System.out.println("The Physical Address is(MAC): "+s.trim());
2903 client.close();
2904 }
2905 catch(Exception e)
2906 {
2907 System.out.println(e);
2908 }
2909 }
2910}
2911
2912
2913
2914*********************************
2915
2916
29177.Implement Client - Server communication to access Date using UDP in Java.
2918
2919
2920//server
2921import java.io.*;
2922import java.net.*;
2923import java.util.*;
2924
2925public class UDPDateServer {
2926 public static void main(String args[]) throws Exception
2927 {
2928 DatagramSocket serverSocket = new DatagramSocket(6789);
2929 byte[] receiveData = new byte[1024];
2930 byte[] sendData = new byte[1024];
2931 String GetDate = "";
2932 while(true)
2933 {
2934 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2935 serverSocket.receive(receivePacket);
2936
2937 String msg = new String(receivePacket.getData());
2938 System.out.println("Message: " + msg);
2939
2940 InetAddress IPAddress = receivePacket.getAddress();
2941 int port = receivePacket.getPort();
2942 msg = msg.trim();
2943 if (msg.equals("date"))
2944 {
2945 Date d = new Date();
2946 GetDate = d.toString();
2947 }
2948
2949 sendData = GetDate.getBytes();
2950 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
2951 serverSocket.send(sendPacket);
2952 }
2953 }
2954}
2955
2956
2957
2958
2959
2960//client
2961
2962import java.io.*;
2963import java.net.*;
2964
2965
2966public class UDPDateClient {
2967 public static void main(String args[]) throws Exception
2968 {
2969 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
2970 DatagramSocket clientSocket = new DatagramSocket();
2971 InetAddress IPAddress = InetAddress.getByName("localhost");
2972 byte[] sendData = new byte[1024];
2973 byte[] receiveData = new byte[1024];
2974
2975 System.out.printf("Enter Request: ");
2976 String msg = inFromUser.readLine();
2977 sendData = msg.getBytes();
2978
2979 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789);
2980 clientSocket.send(sendPacket);
2981
2982 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
2983 clientSocket.receive(receivePacket);
2984
2985 String reply = new String(receivePacket.getData());
2986 System.out.println("FROM SERVER:" + reply);
2987 clientSocket.close();
2988 }
2989}
2990
2991
2992==========================================
2993
2994This class of java in "java.net" represents an Internet Protocol (IP) address. InetAddress class is used to create and manipulate IP objects. It does not have a public constructor. The object of this class can be obtained using its static methods getLocalHost(), getByName(), and getAllByName(). It has several useful methods for obtaining host names and IP addresses
2995Methods in java.net that return InetAddress
2996
2997 InetAddress
2998InterfaceAddress.getAddress()
2999 Returns an InetAddress for this address.
3000 InetAddress
3001InetSocketAddress.getAddress()
3002 Gets the InetAddress.
3003 InetAddress
3004DatagramPacket.getAddress()
3005 Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
3006static InetAddress[]
3007InetAddress.getAllByName(String host)
3008 Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
3009 InetAddress
3010InterfaceAddress.getBroadcast()
3011 Returns an InetAddress for the brodcast address for this InterfaceAddress.
3012static InetAddress
3013InetAddress.getByAddress(byte[] addr)
3014 Returns an InetAddress object given the raw IP address .
3015static InetAddress
3016InetAddress.getByAddress(String host, byte[] addr)
3017 Create an InetAddress based on the provided host name and IP address No name service is checked for the validity of the address.
3018static InetAddress
3019InetAddress.getByName(String host)
3020 Determines the IP address of a host, given the host's name.
3021protected InetAddress
3022URLStreamHandler.getHostAddress(URL u)
3023 Get the IP address of our host.
3024protected InetAddress
3025SocketImpl.getInetAddress()
3026 Returns the value of this socket's address field.
3027 InetAddress
3028Socket.getInetAddress()
3029 Returns the address to which the socket is connected.
3030 InetAddress
3031ServerSocket.getInetAddress()
3032 Returns the local address of this server socket.
3033 InetAddress
3034DatagramSocket.getInetAddress()
3035 Returns the address to which this socket is connected.
3036 InetAddress
3037MulticastSocket.getInterface()
3038 Retrieve the address of the network interface used for multicast packets.
3039 InetAddress
3040Socket.getLocalAddress()
3041 Gets the local address to which the socket is bound.
3042 InetAddress
3043DatagramSocket.getLocalAddress()
3044 Gets the local address to which the socket is bound.
3045static InetAddress
3046InetAddress.getLocalHost()
3047 Returns the local host.
3048protected InetAddress
3049Authenticator.getRequestingSite()
3050 Gets the InetAddress of the site requesting authorization, or null if not available.
3051
3052
3053\A Program That Prints the Address of 192.168.64.3\\
3054
3055import java.net.*;
3056public class Address {
3057public static void main (String[] args) {
3058try {
3059InetAddress address = inetAddress.getByName("192.168.64.3");
3060System.out.println(address);
3061}
3062catch (UnknownHostException e) {
3063System.out.println("Could not find 192.168.64.3 ");
3064}
3065}
3066}
3067
3068\\program find the all address of google\\
3069
3070import java.net.*;
3071public class AllAddressesOfgoogle {
3072public static void main (String[] args) {
3073try {
3074InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
3075for (int i = 0; i < addresses.length; i++) {
3076System.out.println(addresses[i]);
3077}
3078}
3079catch (UnknownHostException e) {
3080System.out.println("Could not find www.microsoft.com");
3081}
3082}
3083}
3084
3085
3086
3087\\prints the address of the machine it's run on.
3088
3089import java.net.*;
3090public class MyAddress {
3091public static void main (String[] args) {
3092try {
3093InetAddress address = InetAddress.getLocalHost( );
3094System.out.println(address);
3095}
3096catch (UnknownHostException e) {
3097System.out.println("Could not find this computer's address.");
3098}
3099}
3100}
3101
3102Given the Address, Find the Hostname
3103import java.net.*;
3104public class ReverseTest {
3105public static void main (String[] args) {
3106try {
3107InetAddress ia = InetAddress.getByName("192.168.64.3");
3108System.out.println(ia.getHostName( ));
3109}
3110catch (Exception e) {
3111System.err.println(e);
3112}
3113}
3114}
3115. Are www.oreilly.com and helio.ora.com the Same?
3116import java.net.*;
3117public class OReillyAliases {
3118public static void main (String args[]) {
3119try {
3120InetAddress oreilly = InetAddress.getByName("www.oreilly.com");
3121InetAddress helio = InetAddress.getByName("helio.ora.com");
3122if (oreilly.equals(helio)) {
3123System.out.println("www.oreilly.com is the same as helio.ora.com");
3124}
3125else {
3126System.out.println("www.oreilly.com is not the same as helio.ora.com");
3127}
3128}
3129catch (UnknownHostException e) {
3130System.out.println("Host lookup failed.");
3131}
3132}
3133}
3134
3135import java.net.*;
3136import java.io.*;
3137public class prog7
3138{
3139public static void main(String args[])
3140{
3141try
3142{
3143
3144BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
3145File f=new File("test.txt");
3146FileWriter fw=new FileWriter(f);
3147System.out.println("Enter the URL which u want to download");
3148String str=br.readLine();
3149URL u=new URL(str);
3150InputStream in=u.openStream();
3151BufferedReader br1=new BufferedReader(new InputStreamReader(in));
3152String str1;
3153while((str1=br1.readLine())!=null){
3154fw.write(str1+"\r\n");
3155}
3156System.out.println("the content of the url:"+str+" is download and saved in text .txt");
3157fw.close();
3158}
3159catch(Exception e)
3160{
3161System.out.println(e);
3162}
3163}
3164}
3165
3166
3167+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++11111111111111111111111111111111111111111111111111111111111111111111111111144444444444444444444444444444444444444444444444444444444444444444444444444444444444444bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiittttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111133333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
3168
3169
3170
3171
3172question one
3173Implement echo server and client in java using UDP sockets.
3174
3175Client program – ServerEcho.java
3176
3177import java.net.*;
3178import java.util.*;
3179
3180public class ServerEcho
3181{
3182 public static void main( String args[]) throws Exception
3183 {
3184 DatagramSocket dsock = new DatagramSocket(7);
3185 byte arr1[] = new byte[150];
3186 DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
3187
3188 while(true)
3189 {
3190 dsock.receive(dpack);
3191
3192 byte arr2[] = dpack.getData();
3193 int packSize = dpack.getLength();
3194 String s2 = new String(arr2, 0, packSize);
3195
3196 System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
3197 dsock.send(dpack);
3198 }
3199 }
3200}
3201
3202Client program – ClientEcho.java
3203import java.net.*;
3204import java.util.*;
3205
3206public class ClientEcho
3207{
3208 public static void main( String args[] ) throws Exception
3209 {
3210 InetAddress add = InetAddress.getByName("snrao");
3211
3212 DatagramSocket dsock = new DatagramSocket( );
3213 String message1 = "This is client calling";
3214 byte arr[] = message1.getBytes( );
3215 DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
3216 dsock.send(dpack); // send the packet
3217 Date sendTime = new Date(); // note the time of sending the message
3218
3219 dsock.receive(dpack); // receive the packet
3220 String message2 = new String(dpack.getData( ));
3221 Date receiveTime = new Date( ); // note the time of receiving the message
3222 System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
3223 }
3224}
3225
3226
3227 OR
3228
3229import java.net.*;
3230 import java.util.*;
3231 public class EchoServer
3232{
3233 public static void main( String args[]) throws Exception
3234 {
3235 DatagramSocket dsock = new DatagramSocket(7);
3236byte arr1[] = new byte[150];
3237DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
3238while(true)
3239{ dsock.receive(dpack);
3240byte arr2[] = dpack.getData();
3241 int packSize = dpack.getLength();
3242String s2 = new String(arr2, 0, packSize);
3243System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
3244dsock.send(dpack);
3245}
3246}
3247}----------------------------------
3248import java.net.*;
3249import java.util.*;
3250 public class EchoClient
3251{ public static void main( String args[] ) throws Exception {
3252InetAddress add = InetAddress.getByName("127.0.0.1");
3253DatagramSocket dsock = new DatagramSocket( );
3254 String message1 = "This is client calling";
3255 byte arr[] = message1.getBytes( );
3256 DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
3257 dsock.send(dpack); // send the packet
3258Date sendTime = new Date( ); // note the time of sending the message
3259dsock.receive(dpack); // receive the packet
3260String message2 = new String(dpack.getData( ));
3261 Date receiveTime = new Date( ); // note the time of receiving the message
3262 System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
3263 }
3264 }
3265
3266
3267
3268
3269
3270
3271
3272
3273question 2
3274Implement a simple message transfer from client to server process using UDP.
3275import java.io.*;
3276import java.net.*;
3277class UDPServerss {
3278public static void main(String args[]) throws Exception {
3279 DatagramSocket serverSocket = new DatagramSocket(9876);
3280
3281 byte[] receiveData = new byte[1024];
3282 byte[] sendData = new byte[1024];
3283 while(true) {
3284
3285 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
3286 serverSocket.receive(receivePacket);
3287 String sentence = new String( receivePacket.getData());
3288 System.out.println("RECEIVED: " + sentence);
3289 InetAddress IPAddress = receivePacket.getAddress();
3290 int port = receivePacket.getPort();
3291 String capitalizedSentence = sentence.toUpperCase();
3292 sendData = capitalizedSentence.getBytes();
3293 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
3294 serverSocket.send(sendPacket);
3295 }
3296}
3297}
3298import java.io.*;
3299import java.net.*;
3300 class UDPClientssss {
3301 public static void main(String args[]) throws Exception {
3302 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
3303 DatagramSocket clientSocket = new DatagramSocket();
3304 InetAddress IPAddress = InetAddress.getByName("localhost");
3305 byte[] sendData = new byte[1024];
3306 byte[] receiveData = new byte[1024];
3307 String sentence = inFromUser.readLine();
3308 sendData = sentence.getBytes();
3309 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
3310 clientSocket.send(sendPacket);
3311 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
3312 clientSocket.receive(receivePacket);
3313 String modifiedSentence = new String(receivePacket.getData());
3314 System.out.println("FROM SERVER:" + modifiedSentence);
3315 clientSocket.close();
3316 }
3317 }
3318
3319
3320
3321
3322
3323
3324question 3
3325 Simple chat application in java using datagram socket and datagram packet
3326Algorithm
3327Start the UDP chat program
3328Import the package java.net.*;
3329Declare the datagramsocket,datagrampacket,BufferedReader,InetAddress.
3330Start the main function
3331In the main function using while loop it perform the loop until str.equals is STOP
3332There important while loop function are
3333clientsocket = new DatagramSocket(cport);
3334dp = new DatagramPacket(buf, buf.length);
3335dis = new BufferedReader(new
3336InputStreamReader(System.in));
3337ia = InetAddress.getLocalHost(); f it is stop then break the while loop
3338Terminate the UDP client program
3339source code java programming UDP Chat server
3340import java.io.*;
3341import java.net.*;
3342class UDPServer
3343{
3344public static DatagramSocket serversocket;
3345public static DatagramPacket dp;
3346public static BufferedReader dis;
3347public static InetAddress ia;
3348public static byte buf[] = new byte[1024];
3349public static int cport = 789,sport=790;
3350public static void main(String[] a) throws IOException
3351{
3352serversocket = new DatagramSocket(sport);
3353dp = new DatagramPacket(buf,buf.length);
3354dis = new BufferedReader
3355(new InputStreamReader(System.in));
3356ia = InetAddress.getLocalHost();
3357System.out.println("Server is Running...");
3358while(true)
3359{
3360serversocket.receive(dp);
3361String str = new String(dp.getData(), 0,
3362dp.getLength());
3363if(str.equals("STOP"))
3364{
3365System.out.println("Terminated...");
3366break;
3367}
3368System.out.println("Client: " + str);
3369String str1 = new String(dis.readLine());
3370buf = str1.getBytes();
3371serversocket.send(new
3372DatagramPacket(buf,str1.length(), ia, cport));
3373}
3374}
3375}
3376
3377Output:-
3378C:\IPLAB>javac UDPServer.java
3379C:\IPLAB>java UDPServer
3380Server is Running...
3381Client: Hello
3382Welcome
3383Terminated...
3384
3385source code java programming UDP Chat Client
3386import java.io.*;
3387import java.net.*;
3388class UDPClient
3389{
3390public static DatagramSocket clientsocket;
3391public static DatagramPacket dp;
3392public static BufferedReader dis;
3393public static InetAddress ia;
3394public static byte buf[] = new byte[1024];
3395public static int cport = 789, sport = 790;
3396public static void main(String[] a) throws IOException
3397{
3398clientsocket = new DatagramSocket(cport);
3399dp = new DatagramPacket(buf, buf.length);
3400dis = new BufferedReader(new
3401InputStreamReader(System.in));
3402ia = InetAddress.getLocalHost();
3403System.out.println("Client is Running... Type 'STOP'
3404to Quit");
3405while(true)
3406{
3407String str = new String(dis.readLine());
3408buf = str.getBytes();
3409if(str.equals("STOP"))
3410{
3411System.out.println("Terminated...");
3412clientsocket.send(new
3413DatagramPacket(buf,str.length(), ia,
3414sport));
3415break;
3416}
3417clientsocket.send(new DatagramPacket(buf,
3418str.length(), ia, sport));
3419clientsocket.receive(dp);
3420String str2 = new String(dp.getData(), 0,
3421dp.getLength());
3422System.out.println("Server: " + str2);
3423}
3424}
3425}
3426
3427Output UDP Chat Client
3428C:\IPLAB>javac UDPClient.java
3429C:\IPLAB>java UDPClient
3430Client is Running... Type ‘STOP’ to Quit
3431Hello
3432Server: Welcome
3433STOP
3434Terminated...
3435BB / REC - 41
3436
3437
3438
3439 or
3440
3441
3442 Client interface:
3443
3444 import java.awt.*;
3445 import javax.swing.*;
3446 public class UDPClient extends JFrame
3447 {
3448 // Variables
3449 private JFrame frame;
3450 private JPanel panel;
3451 private JLabel label;
3452 private JButton sendbutton;
3453 private JTextField textfield;
3454 private JTextArea textarea;
3455 private JScrollPane scrollpane;
3456
3457 public static void main (String args[]) {
3458
3459 new UDPClient();
3460 }
3461
3462 // Constructor
3463 public UDPClient() {
3464
3465 frame = this;
3466 panel = new JPanel(new GridBagLayout());
3467 panel.setBackground(Color.cyan);
3468 frame.setTitle("Chat Applet Client");
3469 frame.getContentPane().add(panel, BorderLayout.NORTH);
3470 frame.setVisible(true);
3471 frame.setSize(430, 364);
3472 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3473 //frame.setResizable(false);
3474 GridBagConstraints c = new GridBagConstraints();
3475 c.insets = new Insets(5, 5, 5, 5);
3476
3477 // Server address label
3478 label = new JLabel("Server:");
3479 c.fill = GridBagConstraints.HORIZONTAL;
3480 c.gridx = 0;
3481 c.gridy = 0;
3482 panel.add(label, c);
3483
3484 // Server address textfield
3485 textfield = new JTextField(20);
3486 c.fill = GridBagConstraints.HORIZONTAL;
3487 c.gridx = 1;
3488 c.gridy = 0;
3489 panel.add(textfield, c);
3490
3491 // 'Port#:' label
3492 label = new JLabel("Port# :");
3493 c.fill = GridBagConstraints.HORIZONTAL;
3494 c.gridx = 2;
3495 c.gridy = 0;
3496 panel.add(label, c);
3497
3498 // Port# textfield
3499 textfield = new JTextField(6);
3500 c.fill = GridBagConstraints.HORIZONTAL;
3501 c.gridx = 3;
3502 c.gridy = 0;
3503 panel.add(textfield, c);
3504
3505 // 'Conversation:' label
3506 label = new JLabel("Conversation:");
3507 c.fill = GridBagConstraints.HORIZONTAL;
3508 c.gridx = 0;
3509 c.gridy = 1;
3510 c.gridwidth = 4;
3511 panel.add(label, c);
3512
3513 // Conversation Window
3514 textarea = new JTextArea(10, 2);
3515 scrollpane = new JScrollPane(textarea);
3516 textarea.setLineWrap(true);
3517 textarea.setWrapStyleWord(true);
3518 textarea.setEditable(false);
3519 c.fill = GridBagConstraints.HORIZONTAL;
3520 c.gridx = 0;
3521 c.gridy = 2;
3522 c.gridwidth = 4;
3523 panel.add(scrollpane, c);
3524
3525 // 'Message:' label
3526 label = new JLabel("Message to Send:");
3527 c.fill = GridBagConstraints.HORIZONTAL;
3528 c.gridx = 0;
3529 c.gridy = 3;
3530 panel.add(label, c);
3531
3532 // Message Window
3533 textarea = new JTextArea(2, 2);
3534 scrollpane = new JScrollPane(textarea);
3535 textarea.setLineWrap(true);
3536 textarea.setWrapStyleWord(true);
3537 c.fill = GridBagConstraints.HORIZONTAL;
3538 c.gridx = 0;
3539 c.gridy = 4;
3540 c.gridwidth = 4;
3541 panel.add(scrollpane, c);
3542
3543 // 'Send' button
3544 sendbutton = new JButton("Send");
3545 c.fill = GridBagConstraints.HORIZONTAL;
3546 c.gridx = 0;
3547 c.gridy = 5;
3548 c.gridwidth = 4;
3549 panel.add(sendbutton, c);
3550 }
3551 }
3552
3553
3554
3555
3556
3557Server interface:
3558
3559 import java.awt.*;
3560 import javax.swing.*;
3561 public class UDPServer extends JFrame {
3562 // Variables
3563 private JFrame frame;
3564 private JPanel panel;
3565 private JLabel label;
3566 private JButton startbutton;
3567 private JButton stopbutton;
3568 private JButton sendbutton;
3569 private JTextField textfield;
3570 private JTextArea textarea;
3571 private JScrollPane scrollpane;
3572
3573 //http://www.youtube.com/watch?v=IkEz5tW5bok
3574 public static void main (String args[]) {
3575
3576 new UDPServer();
3577 }
3578
3579 // Constructor
3580 public UDPServer() {
3581
3582 frame = this;
3583 panel = new JPanel(new GridBagLayout());
3584 panel.setBackground(Color.darkGray);
3585 frame.setTitle("Chat Applet Server");
3586 frame.getContentPane().add(panel, BorderLayout.NORTH);
3587 frame.setVisible(true);
3588 //frame.pack();
3589
3590 frame.setSize(430, 364);
3591 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3592 frame.setResizable(true);
3593 GridBagConstraints c = new GridBagConstraints();
3594 c.insets = new Insets(5, 5, 5, 5);
3595
3596 // 'Start Server' button
3597 startbutton = new JButton("Start Server");
3598 startbutton.setPreferredSize(new Dimension(130,20));
3599 c.fill = GridBagConstraints.HORIZONTAL;
3600 c.gridx = 0;
3601 c.gridy = 0;
3602 c.gridwidth = 1;
3603 panel.add(startbutton, c);
3604
3605 // 'Stop Server' button
3606 stopbutton = new JButton("Stop Server");
3607 stopbutton.setPreferredSize(new Dimension(130,20));
3608 c.fill = GridBagConstraints.HORIZONTAL;
3609 c.gridx = 1;
3610 c.gridy = 0;
3611 c.gridwidth = 1;
3612 panel.add(stopbutton, c);
3613
3614 // 'Port#:' label
3615 label = new JLabel("Port# :");
3616 label.setForeground(Color.white);
3617 c.fill = GridBagConstraints.HORIZONTAL;
3618 c.gridx = 2;
3619 c.gridy = 0;
3620 panel.add(label, c);
3621
3622 // Port# textfield
3623 textfield = new JTextField(6);
3624 c.fill = GridBagConstraints.HORIZONTAL;
3625 c.gridx = 3;
3626 c.gridy = 0;
3627 panel.add(textfield, c);
3628
3629 // 'Conversation:' label
3630 label = new JLabel("Conversation:");
3631 label.setForeground(Color.white);
3632 c.fill = GridBagConstraints.HORIZONTAL;
3633 c.gridx = 0;
3634 c.gridy = 1;
3635 c.gridwidth = 4;
3636 panel.add(label, c);
3637
3638 // Conversation Window
3639 textarea = new JTextArea("<Server not yet started!>", 10, 2);
3640 scrollpane = new JScrollPane(textarea);
3641 textarea.setLineWrap(true);
3642 textarea.setWrapStyleWord(true);
3643 textarea.setEditable(false);
3644 c.fill = GridBagConstraints.HORIZONTAL;
3645 c.gridx = 0;
3646 c.gridy = 2;
3647 c.gridwidth = 4;
3648 panel.add(scrollpane, c);
3649
3650 // 'Message:' label
3651 label = new JLabel("Message to Send:");
3652 label.setForeground(Color.white);
3653 c.fill = GridBagConstraints.HORIZONTAL;
3654 c.gridx = 0;
3655 c.gridy = 3;
3656 panel.add(label, c);
3657
3658 // Message Window
3659 textarea = new JTextArea(2, 2);
3660 scrollpane = new JScrollPane(textarea);
3661 textarea.setLineWrap(true);
3662 textarea.setWrapStyleWord(true);
3663 c.fill = GridBagConstraints.HORIZONTAL;
3664 c.gridx = 0;
3665 c.gridy = 4;
3666 c.gridwidth = 4;
3667 panel.add(scrollpane, c);
3668
3669 // 'Send' button
3670 sendbutton = new JButton("Send");
3671 c.fill = GridBagConstraints.HORIZONTAL;
3672 c.gridx = 0;
3673 c.gridy = 5;
3674 c.gridwidth = 4;
3675 panel.add(sendbutton, c);
3676 }
3677 }
3678
3679
3680
3681
3682
3683
3684question 4-----------------------------
3685UDP Program – DNS CLIENT-SERVER
3686
3687UDP Program (DNS CLIENT-SERVER)
3688
3689AIM : To create a client server program to Domain Name System using the UDP protocol client server.
3690
3691ALGORITHM
3692
3693Server
3694
3695 Declare the necessary arrays and variables.
3696 Set server port address using socket().
3697 Get the current message.
3698 Connect to the client.
3699 Stop the process.
3700
3701Client
3702
3703 Set the client machine address.
3704 Connect to the server.
3705 Read from the server the current message.
3706 Display the current message.
3707 Close the connection.
3708
3709PROGRAM:
3710
3711UDPclient
3712
3713import java .io.*;
3714
3715import java.net.*;
3716
3717classUDPclient
3718
3719{
3720
3721public static DatagramSocket ds;
3722
3723public static intclientport=789,serverport=790;
3724
3725public static void main(String args[])throws Exception
3726
3727{
3728
3729byte buffer[]=new byte[1024];
3730
3731ds=new DatagramSocket(serverport);
3732
3733BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
3734
3735System.out.println(“server waitingâ€);
3736
3737InetAddressia=InetAddress.getLocalHost();
3738
3739while(true)
3740
3741{
3742
3743System.out.println(“Client:â€);
3744
3745String str=dis.readLine();
3746
3747if(str.equals(“endâ€))
3748
3749break;
3750
3751buffer=str.getBytes();
3752
3753ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
3754
3755DatagramPacket p=new DatagramPacket(buffer,buffer.length);
3756
3757ds.receive(p);
3758
3759String psx=new String(p.getData(),0,p.getLength());
3760
3761System.out.println(“Server:†+ psx);
3762
3763}
3764
3765}
3766
3767}
3768
3769UDP server
3770
3771import java.io.*;
3772
3773import java.net.*;
3774
3775classUDPserver
3776
3777{
3778
3779public static DatagramSocket ds;
3780
3781public static byte buffer[]=new byte[1024];
3782
3783public static intclientport=789,serverport=790;
3784
3785public static void main(String args[])throws Exception
3786
3787{
3788
3789ds=new DatagramSocket(clientport);
3790
3791System.out.println(“press ctrl+c to quit the programâ€);
3792
3793BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
3794
3795InetAddressia=InetAddress.getLocalHost();
3796
3797while(true)
3798
3799{
3800
3801DatagramPacket p=new DatagramPacket(buffer,buffer.length);
3802
3803ds.receive(p);
3804
3805String psx=new String(p.getData(),0,p.getLength());
3806
3807System.out.println(“Client:†+ psx);
3808
3809InetAddressib=InetAddress.getByName(psx);
3810
3811System.out.println(“Server output:â€+ib);
3812
3813String str=dis.readLine();
3814
3815if(str.equals(“endâ€))
3816
3817break;
3818
3819buffer=str.getBytes();
3820
3821ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
3822
3823}
3824
3825}
3826
3827}
3828
3829OUTPUT:
3830
3831UDPclient
3832
3833C:\Program Files\Java\jdk1.6.0\bin>javac UDPclient.java
3834
3835C:\Program Files\Java\jdk1.6.0\bin>java UDPclient
3836
3837Server waiting
3838
3839Client:www.yahoo.com
3840
3841UDPserver
3842
3843C:\Program Files\Java\jdk1.6.0\bin>javac UDPserver.java
3844
3845C:\Program Files\Java\jdk1.6.0\bin>java UDPserver
3846
3847Press ctrl+c to quit the program
3848
3849Client:www.yahoo.com
3850
3851Server output:www.yahoo.com/106.10.170.115
3852
3853RESULT:
3854
3855Thus client server program to Domain Name System using the UDP protocol client server has been executed and verified successfully.
3856
3857
3858
3859
3860
3861QUESTION 5------------------------------------------------
3862UDP DATE SERVER
3863
3864Server Program >>>>> Server.java
3865
3866
3867import java.net.*;
3868import java.io.*;
3869import java.util.*;
3870
3871public class Server {
3872
3873public static void main(String[] args) throws Exception{
3874
3875DatagramSocket ss=new DatagramSocket(1234);
3876
3877while(true){
3878
3879System.out.println("Server is up....");
3880
3881byte[] rd=new byte[100];
3882byte[] sd=new byte[100];
3883
3884DatagramPacket rp=new DatagramPacket(rd,rd.length);
3885
3886ss.receive(rp);
3887
3888InetAddress ip= rp.getAddress();
3889
3890int port=rp.getPort();
3891
3892Date d=new Date(); // getting system time
3893
3894String time= d + ""; // converting it to String
3895
3896sd=time.getBytes(); // converting that String to byte
3897
3898DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port);
3899
3900ss.send(sp);
3901
3902rp=null;
3903
3904System.out.println("Done !! ");
3905
3906}
3907
3908}
3909
3910}
3911
3912
3913Client program >>>>>>>>> Clientnew.java
3914import java.net.*;
3915import java.io.*;
3916
3917public class Clientnew {
3918
3919public static void main(String[] args) throws Exception{
3920
3921 System.out.println("Server Time >>>>");
3922
3923 DatagramSocket cs=new DatagramSocket();
3924
3925 InetAddress ip=InetAddress.getByName("localhost");
3926
3927 byte[] rd=new byte[100];
3928 byte[] sd=new byte[100];
3929
3930 DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,1234);
3931
3932 DatagramPacket rp=new DatagramPacket(rd,rd.length);
3933
3934 cs.send(sp);
3935
3936 cs.receive(rp);
3937
3938 String time=new String(rp.getData());
3939
3940 System.out.println(time);
3941
3942 cs.close();
3943
3944}
3945
3946}
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3957SIR UPLOADED MATERIAL
3958
3959
39601)Write a program to list all ports hosting a TCP server in a specified host.
3961
3962import java.net.*;
3963import java.io.*;
3964public class ports{
3965public static void main(String args[])
3966{
3967for(int i=1;i<1024;i++)
3968{
3969try{
3970Socket s=new Socket("127.0.0.1",i);
3971System.out.println("There is server on port"+i+"of 127.0.0.1");
3972}
3973catch(UnknownHostException e){
3974System.err.println(e);
3975break;
3976}
3977catch(IOException e){
3978//must not be server on this port
3979}
3980}
3981}
3982}
3983
39842)Write a program to display the server’s date and time details at the client end.
3985
3986import java.io.IOException;
3987import java.io.PrintWriter;
3988import java.net.ServerSocket;
3989import java.net.Socket;
3990import java.util.Date;
3991
3992public class dateserver {
3993
3994 public static void main(String[] args) throws IOException {
3995 ServerSocket listener = new ServerSocket(9090);
3996 try {
3997 while (true) {
3998 Socket socket = listener.accept();
3999 try {
4000 PrintWriter out =
4001 new PrintWriter(socket.getOutputStream(), true);
4002 out.println(new Date().toString());
4003 } finally {
4004 socket.close();
4005 }
4006 }
4007 }
4008 finally {
4009 listener.close();
4010 }
4011 }
4012}
4013
4014------------------------------
4015import java.io.*;
4016import java.net.Socket;
4017
4018public class dateClient {
4019
4020 public static void main(String[] args) throws IOException {
4021 Socket s = new Socket("127.0.0.1", 9090);
4022 BufferedReader input =
4023 new BufferedReader(new InputStreamReader(s.getInputStream()));
4024 String answer = input.readLine();
4025 System.out.println("The date and time details are "+answer );
4026
4027 System.exit(0);
4028 }
4029}
4030
4031
40323)Write a program to display the client’s address at the server end.
4033
4034
4035import java.io.*;
4036import java.net.*;
4037
4038public class ServerAddr
4039{
4040public static void main(String args[]) throws IOException
4041{
4042try
4043{
4044ServerSocket ss = new ServerSocket(6666);
4045System.out.println("Waiting for client.....");
4046
4047Socket s = ss.accept();
4048System.out.println("Connected to client....");
4049
4050DataInputStream in = new DataInputStream (s.getInputStream());
4051String line = null;
4052line = in.readUTF();
4053System.out.println("Client's IP adress: " + line);
4054}
4055catch (Exception e)
4056{
4057e.printStackTrace();
4058}
4059}
4060}
4061
4062--------------------------------
4063
4064import java.io.*;
4065import java.net.*;
4066
4067public class ClientAddr
4068{
4069public static void main(String args[]) throws IOException
4070{
4071try
4072{
4073InetAddress ipaddress = InetAddress.getByName("");
4074Socket s = new Socket(ipaddress,6666);
4075
4076System.out.println("Connected to the server...");
4077DataOutputStream out = new DataOutputStream(s.getOutputStream());
4078
4079String line = null;
4080System.out.println("Sending my IP Address to server...");
4081
4082line=ipaddress.getHostAddress();
4083
4084out.writeUTF(line);
4085out.flush();}
4086catch (Exception e)
4087{
4088e.printStackTrace();
4089}
4090}
4091}
4092
40934)Implement a simple message transfer from client to server process using TCP/IP.
4094
4095import java.io.*;
4096import java.net.*;
4097
4098public class Tcpserver
4099{
4100public static void main(String args[]) throws IOException
4101{
4102try
4103{
4104ServerSocket ss = new ServerSocket(6666);
4105System.out.println("Waiting for client.....");
4106
4107Socket s = ss.accept();
4108System.out.println("Connected to client....");
4109
4110DataInputStream in = new DataInputStream (s.getInputStream());
4111String line = null;
4112line = in.readUTF();
4113System.out.println("Mesage from Client:" + line);
4114}
4115catch (Exception e)
4116{
4117e.printStackTrace();
4118}
4119}
4120}
4121
4122--------------------------------------
4123import java.io.*;
4124import java.net.*;
4125
4126public class TCPClient
4127{
4128public static void main(String args[]) throws IOException
4129{
4130try
4131{
4132InetAddress ipaddress = InetAddress.getByName("");
4133Socket s = new Socket(ipaddress,6666);
4134DataInputStream read = new DataInputStream(System.in);
4135System.out.println("Connected to the server...");
4136DataOutputStream out = new DataOutputStream(s.getOutputStream());
4137
4138String line = null;
4139System.out.println("Write a message to the server..");
4140
4141line=read.readLine();
4142out.writeUTF(line);
4143out.flush();}
4144catch (Exception e)
4145{
4146e.printStackTrace();
4147}
4148}
4149}
4150
41515)Develop a TCP client/server application for transferring a text file from client to server
4152
4153
4154CLIENt:
4155import java.io.*;
4156 import java.net.*;
4157import java.util.*;
4158public class FTPClient {
4159public static void main(String args[]) throws Exception
4160
4161{
4162 Socket ss = new Socket("localhost",5000);
4163while(true)
4164{
4165
4166Scanner pbn = new Scanner(System.in);
4167System.out.println("Enter the path of the file ");
4168String path = pbn.nextLine();
4169System.out.println(path);
4170
4171File f = new File(path);
4172FileInputStream fis = new FileInputStream(f);
4173
4174BufferedInputStream bis = new BufferedInputStream(fis);
4175System.out.println("Sending file...");
4176
4177System.out.println("File sent");
4178
4179 }
4180
4181 }
4182}
4183server:
4184
4185//////////////////
4186import java.io.*;
4187import java.net.*;
4188import java.util.*;
4189public class FTPServer {
4190public static void main(String args[]) throws IOException
4191{ServerSocket ss = new ServerSocket(5000);
4192
4193 Scanner pbn = new Scanner(System.in);
4194 boolean flag = true;
4195 while(flag)
4196 {
4197 flag = false;
4198 try
4199 {System.out.println("waiting...");
4200Socket s = ss.accept();
4201System.out.println("Accepted connection "+s);
4202 System.out.println("Enter the path where you want to store the file");
4203 String path1 = pbn.nextLine();
4204 FileOutputStream fos = new FileOutputStream(path1);
4205 BufferedOutputStream bos = new BufferedOutputStream(fos);
4206 InputStream is = s.getInputStream();
4207 byte[] b = new byte[600000];
4208 int n = 0;
4209 int o = 0;
4210 while((n=is.read(b,o,b.length-o))>=0)
4211 {
4212 o+=n;
4213 }
4214 bos.write(b,0,o);
4215
4216 bos.flush();
4217 System.out.println("File Received");
4218 }
4219
4220 catch(FileNotFoundException f)
4221 {
4222 flag = true;
4223 String msg = f.getMessage();
4224 System.out.println("Error Message:"+msg);
4225 System.out.println("Please Enter a correct file path");
4226 }
4227 }
4228
4229 }
4230}
4231
4232
42336. 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.
4234
4235import java.io.*;
4236import java.net.*;
4237
4238public class AuthServer
4239{
4240 public static void main(String args[]) throws IOException
4241 {
4242 try
4243 {
4244 ServerSocket ss = new ServerSocket(6666);
4245 System.out.println("Waiting for client.....");
4246 Socket s = ss.accept();
4247 System.out.println("Connected to client....");
4248
4249 DataInputStream in = new DataInputStream (s.getInputStream());
4250 DataOutputStream out = new DataOutputStream (s.getOutputStream());
4251
4252 String line = null;
4253 String line1 = null;
4254 String sendline = null;
4255
4256
4257line = in.readUTF();
4258line1 = in.readUTF();
4259 if((line.equals("aid")|| line.equals("bid"))&&((line1.equals("apass")|| line1.equals("bpass"))))
4260 {
4261 sendline ="Valid user id and password.Successfully logged in !!!!";
4262 out.writeUTF(sendline);
4263 out.flush();
4264
4265 }
4266 else
4267
4268 {
4269 sendline ="Invalid details";
4270 out.writeUTF(sendline);
4271 out.flush();
4272 }
4273
4274 }
4275 catch (Exception e)
4276 {
4277 e.printStackTrace();
4278 }
4279 }
4280}
4281
4282import java.io.*;
4283import java.net.*;
4284
4285public class AuthClient
4286{
4287 public static void main(String args[]) throws IOException
4288 {
4289 try
4290 {
4291 InetAddress ipaddress = InetAddress.getByName("127.0.0.1");
4292 Socket s = new Socket(ipaddress,6666);
4293
4294 System.out.println("Connected to the server...");
4295
4296 DataInputStream read = new DataInputStream(System.in);
4297
4298 DataInputStream in = new DataInputStream(s.getInputStream());
4299 DataOutputStream out = new DataOutputStream(s.getOutputStream());
4300
4301 String line = null;
4302 String line1 = null;
4303 String receiveline = null;
4304
4305 System.out.println("Enter User id ");
4306 line = read.readLine();
4307 out.writeUTF(line);
4308 out.flush();
4309
4310System.out.println("Enter Password ");
4311line1 = read.readLine();
4312out.writeUTF(line1);
4313out.flush();
4314
4315
4316 receiveline = in.readUTF();
4317 System.out.println("SERVER: " + receiveline);
4318
4319
4320 }
4321 catch (Exception e)
4322 {
4323 e.printStackTrace();
4324 }
4325 }
4326}
4327
43287. Write a program to develop a simple (text based) Chat application using TCP/IP.
4329//chat
4330import java.io.*;
4331import java.net.*;
4332
4333public class TCPserver
4334{
4335 public static void main(String args[]) throws IOException
4336 {
4337 try
4338 {
4339 ServerSocket ss = new ServerSocket(6666);
4340 System.out.println("Waiting for client.....");
4341DataInputStream read = new DataInputStream(System.in);
4342 Socket s = ss.accept();
4343 System.out.println("Connected to client....");
4344
4345 DataInputStream in = new DataInputStream (s.getInputStream());
4346 DataOutputStream out = new DataOutputStream (s.getOutputStream());
4347
4348 String line = null;
4349
4350
4351 do
4352 {
4353 line = in.readUTF();
4354 System.out.println("CLIENT: " + line);
4355 line = read.readLine();
4356 out.writeUTF(line);
4357 out.flush();
4358
4359 System.out.println("Waiting for the next line.....");
4360 }while(!line.equals("bye"));
4361 }
4362 catch (Exception e)
4363 {
4364 e.printStackTrace();
4365 }
4366 }
4367}
4368
4369
4370//chat
4371import java.io.*;
4372import java.net.*;
4373
4374public class TCPClient
4375{
4376 public static void main(String args[]) throws IOException
4377 {
4378 try
4379 {
4380 InetAddress ipaddress = InetAddress.getByName("127.0.0.1");
4381 Socket s = new Socket(ipaddress,6666);
4382
4383 System.out.println("Connected to the server...");
4384
4385 DataInputStream read = new DataInputStream(System.in);
4386
4387 DataInputStream in = new DataInputStream(s.getInputStream());
4388 DataOutputStream out = new DataOutputStream(s.getOutputStream());
4389
4390 String line = null;
4391 String receiveline = null;
4392
4393 System.out.println("Enter data to send to the server: ");
4394
4395 do
4396 {
4397 System.out.print("CLIENT: ");
4398 line = read.readLine();
4399 out.writeUTF(line);
4400 out.flush();
4401
4402 receiveline = in.readUTF();
4403 System.out.println("SERVER: " + receiveline);
4404
4405 }while(!line.equals("bye"));
4406 }
4407 catch (Exception e)
4408 {
4409 e.printStackTrace();
4410 }
4411 }
4412}
4413
44148. Implement a simple message transfer from client to server process using UDP.
4415import java.io.*;
4416import java.net.*;
4417class UDPServerss {
4418public static void main(String args[]) throws Exception {
4419 DatagramSocket serverSocket = new DatagramSocket(9876);
4420
4421 byte[] receiveData = new byte[1024];
4422 byte[] sendData = new byte[1024];
4423 while(true) {
4424
4425 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
4426 serverSocket.receive(receivePacket);
4427 String sentence = new String( receivePacket.getData());
4428 System.out.println("RECEIVED: " + sentence);
4429 InetAddress IPAddress = receivePacket.getAddress();
4430 int port = receivePacket.getPort();
4431 String capitalizedSentence = sentence.toUpperCase();
4432 sendData = capitalizedSentence.getBytes();
4433 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
4434 serverSocket.send(sendPacket);
4435 }
4436}
4437}
4438import java.io.*;
4439import java.net.*;
4440 class UDPClientssss {
4441 public static void main(String args[]) throws Exception {
4442 BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
4443 DatagramSocket clientSocket = new DatagramSocket();
4444 InetAddress IPAddress = InetAddress.getByName("localhost");
4445 byte[] sendData = new byte[1024];
4446 byte[] receiveData = new byte[1024];
4447 String sentence = inFromUser.readLine();
4448 sendData = sentence.getBytes();
4449 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
4450 clientSocket.send(sendPacket);
4451 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
4452 clientSocket.receive(receivePacket);
4453 String modifiedSentence = new String(receivePacket.getData());
4454 System.out.println("FROM SERVER:" + modifiedSentence);
4455 clientSocket.close();
4456 }
4457 }
4458
44599. Write a program to implement an Echo UDP server. Test the working of the server by writing a client application.
4460import java.net.*;
4461 import java.util.*;
4462 public class EchoServer
4463{
4464 public static void main( String args[]) throws Exception
4465 {
4466 DatagramSocket dsock = new DatagramSocket(7);
4467byte arr1[] = new byte[150];
4468DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
4469while(true)
4470{ dsock.receive(dpack);
4471byte arr2[] = dpack.getData();
4472 int packSize = dpack.getLength();
4473String s2 = new String(arr2, 0, packSize);
4474System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
4475dsock.send(dpack);
4476}
4477}
4478}----------------------------------
4479import java.net.*;
4480import java.util.*;
4481 public class EchoClient
4482{ public static void main( String args[] ) throws Exception {
4483InetAddress add = InetAddress.getByName("127.0.0.1");
4484DatagramSocket dsock = new DatagramSocket( );
4485 String message1 = "This is client calling";
4486 byte arr[] = message1.getBytes( );
4487 DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
4488 dsock.send(dpack); // send the packet
4489Date sendTime = new Date( ); // note the time of sending the message
4490dsock.receive(dpack); // receive the packet
4491String message2 = new String(dpack.getData( ));
4492 Date receiveTime = new Date( ); // note the time of receiving the message
4493 System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
4494 }
4495 }
4496
449710. 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.
4498import java.io.*;
4499import java.net.*;
4500public class UDPdiscardServer {
4501 public final static int DEFAULT_PORT=9;
4502 public final static int MAX_PACKET_SIZE=65507;
4503public static void main(String args[])
4504{
4505 int port=DEFAULT_PORT;
4506 byte[] buffer=new byte[MAX_PACKET_SIZE];
4507
4508 try{
4509 port=9;
4510 }
4511catch(Exception ex)
4512{}
4513 try{
4514 DatagramSocket server=new DatagramSocket(port);
4515DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
4516
4517while(true)
4518{
4519 try{
4520 server.receive(packet);
4521 String s=new String(packet.getData(),0,packet.getLength(),"UTF-8");
4522 System.out.println(packet.getAddress()+" at port "+packet.getPort()+" says "+s);
4523 packet.setLength(buffer.length);
4524 }
4525 catch(IOException ex){ System.err.println(ex);}
4526}
4527
4528 }
4529 catch(SocketException ex)
4530 {System.err.println(ex);}
4531}
4532}
4533import java.io.*;
4534import java.net.*;
4535public class UDPdiscardClient {
4536 public final static int DEFAULT_PORT=9;
4537
4538public static void main(String args[])
4539{String hostname="localhost";
4540 int port=DEFAULT_PORT;
4541
4542
4543 try{
4544 InetAddress server=InetAddress.getByName(hostname);
4545 BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
4546 DatagramSocket theSocket=new DatagramSocket();
4547
4548 while(true)
4549 {
4550 String theLine=userInput.readLine();
4551 if(theLine.equals(".")) break;
4552 byte[] data=theLine.getBytes("UTF-8");
4553 DatagramPacket theOutput=new DatagramPacket(data,data.length,server,port);
4554 theSocket.send(theOutput);
4555 }
4556 }
4557 catch(UnknownHostException ex)
4558 {System.err.println(ex);}
4559 catch(SocketException ex)
4560 {System.err.println(ex);}
4561 catch(IOException ioex)
4562 {System.err.println(ioex);}
4563}
4564}
4565Find the physical address of a host when its logical address is known (ARP protocol) using TCP/IP.
4566
4567import java.net.InetAddress;
4568import java.net.NetworkInterface;
4569import java.net.SocketException;
4570import java.net.UnknownHostException;
4571import java.util.Scanner;
4572
4573public class MacAddress {
4574 public static void main(String[] args)
4575 {
4576 try
4577 {
4578 Scanner console = new Scanner(System.in);
4579 System.out.println("Enter System Name: ");
4580 String ipaddr = console.nextLine();
4581 InetAddress address = InetAddress.getByName(ipaddr);
4582 System.out.println("address = "+address);
4583 NetworkInterface ni = NetworkInterface.getByInetAddress(address);
4584 if (ni!=null)
4585 {
4586 byte[] mac = ni.getHardwareAddress();
4587 if (mac != null)
4588 {
4589 System.out.print("MAC Address : ");
4590 for (int i=0; i<mac.length; i++)
4591 {
4592 System.out.format("%02X%s", mac[i], (i<mac.length - 1) ? "-" :"");
4593 }
4594 }
4595 else
4596 {
4597 System.out.println("Address doesn't exist or is not accessible/");
4598
4599 }
4600 }
4601 else
4602 {
4603 System.out.println("Network Interface for the specified address is not found");
4604 }
4605 }
4606 catch(UnknownHostException he)
4607 {
4608 }
4609 catch(SocketException e)
4610 {
4611 }
4612 }
4613}
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4625basics
4626Basic network commands
4627ping
4628The ping command (named after the sound of an active sonar system) sends echo requests to the host
4629specified on the command line, and lists the responses received.
4630$ ping ipAddress or hostname
4631e.g
4632$ ping www.vit.ac.in
4633• ping - sends an ICMP ECHO_REQUEST packet to the specified host. If the host responds, an
4634ICMP packet is received.
4635• One can “ping†an IP address to see if a machine is alive.
4636• It provides a very quick way to see if a machine is up and connected to the network.
4637netstat
4638• It works with the LINUX Network Subsystem, it will tell you what the status of ports are ie. open,
4639closed, waiting connections. It is used to display the TCP/IP network protocol statistics and
4640information.
4641tcpdump
4642This is a sniffer, a program that captures packets off a network interface and interprets them.
4643hostname
4644Tells the user the host name of the computer they are logged into.
4645traceroute
4646traceroute will show the route of a packet. It attempts to list the series of hosts through which your
4647packets travel on their way to a given destination.
4648Command syntax:
4649traceroute machine_name_or_ip
4650e.g traceroute www.vit.ac.in
4651Each host will be displayed, along with the response times at each host.
4652finger
4653Retrieves information about the specified user.
4654e.g finger bit50001
4655ifconfig ( In Windows use ipconfig )
4656This command is used to configure network interfaces, or to display their current configuration.
4657dig
4658The "domain information groper" tool. If you give a hostname as an argument to output information
4659about that host, including it's IP address, hostname and various other information.
4660e.g dig vitlinux
4661telnet
4662telnet allows you to log in to a computer, just as if you were sitting at the terminal. Once your
4663username and password are verified, you are given a shell prompt. From here, you can do anything
4664requiring a text console.
4665ftp
4666To connect to an FTP server use
4667ftp ipaddress
4668netstat
4669Displays contents of /proc/net files. It works with the LINUX Network Subsystem, it will tell
4670you what the status of ports are ie. open, closed, waiting, masquerade connections. It will also
4671display various other things. It has many different options.
4672tcpdump
4673This is a sniffer, a program that captures packets off a network interface and interprets them
4674for you. It understands all basic internet protocols, and can be used to save entire packets for
4675later inspection.
4676ping
4677The ping command (named after the sound of an active sonar system) sends echo requests to
4678the host you specify on the command line, and lists the responses received their round trip
4679time.
4680You simply use ping as:
4681ping ip_or_host_name
4682hostname
4683Tells the user the host name of the computer they are logged into. Note: may be called host.
4684traceroute
4685traceroute will show the route of a packet. It attempts to list the series of hosts through which
4686your packets travel on their way to a given destination. Also have a look at xtraceroute (one
4687of several graphical equivalents of this program).
4688Command syntax:
4689traceroute machine_name_or_ip
4690tracepath
4691tracepath performs a very simlar function to traceroute the main difference is that tracepath
4692doesn't take complicated options.
4693Command syntax:
4694tracepath machine_name_or_ip
4695findsmb
4696findsmb is used to list info about machines that respond to SMB name queries (for example
4697windows based machines sharing their hard disk's).
4698Command syntax:
4699Findsmb
4700This would find all machines possible, you may need to specify a particular subnet to query
4701those machines only...
4702nmap
4703“ network exploration tool and security scannerâ€. nmap is a very advanced network tool used
4704to query machines (local or remote) as to whether they are up and what ports are open on
4705these machines.
4706A simple usage example:
4707nmap machine_name
4708This would query your own machine as to what ports it keeps open. nmap is a very powerful
4709tool, documentation is available on the nmap site as well as the information in the manual
4710page.
4711telnet
4712Someone once stated that telnet(1) was the coolest thing he had ever seen on computers. The ability
4713to remotely log in and do stuff on another computer is what separates Unix and Unix-like operating
4714systems from other operating systems.
4715telnet allows you to log in to a computer, just as if you were sitting at the terminal. Once your
4716username and password are verified, you are given a shell prompt. From here, you can do anything
4717requiring a text console. Compose email, read newsgroups, move files around, and so on. If you are
4718running X and you telnet to another machine, you can run X programs on the remote computer and
4719display them on yours.
4720To login to a remote machine, use this syntax:
4721% telnet <hostname>
4722If the host responds, you will receive a login prompt. Give it your username and password. That's it.
4723You are now at a shell. To quit your telnet session, use either the exit command or the logout
4724command.
4725telnet does not encrypt the information it sends. Everything is sent in plain text, even passwords.
4726It is not advisable to use telnet over the Internet. Instead, consider the Secure Shell. It encrypts
4727all traffic and is available for free.
4728The other use of telnet
4729Now that we have convinced you not to use the telnet protocol anymore to log into a remote machine,
4730we'll show you a couple of useful ways to use telnet.
4731You can also use the telnet command to connect to a host on a certain port.
4732% telnet <hostname> [port]
4733This can be quite handy when you quickly need to test a certain service, and you need full control
4734over the commands, and you need to see what exactly is going on. You can interactively test or use
4735an SMTP server, a POP3 server, an HTTP server, etc. this way.
4736In the next figure you'll see how you can telnet to a HTTP server on port 80, and get some basic
4737information from it.
4738Figure 13-1. Telnetting to a webserver
4739% telnet store.slackware.com 80
4740Trying 69.50.233.153...
4741Connected to store.slackware.com.
4742Escape character is '^]'.
4743HEAD / HTTP/1.0
4744HTTP/1.1 200 OK
4745Date: Mon, 25 Apr 2005 20:47:01 GMT
4746Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d
4747Last-Modified: Fri, 18 Apr 2003 10:58:54 GMT
4748ETag: "193424-c0-3e9fda6e"
4749Accept-Ranges: bytes
4750Content-Length: 192
4751Connection: close
4752Content-Type: text/html
4753Connection closed by foreign host.
4754%
47551-)arp :
4756When we need an Ethernet (MAC) address we can use arp(address resolution protocol).
4757In other words it shows the physical address of an host.
4758Example:
4759C:\Documents and Settings\sysadm>arp -a
4760Interface: 169.254.195.199 --- 0x2
4761Internet Address Physical Address Type
4762216.109.127.60 00-53-45-00-00-00 static
47632-)nslookup:
4764Displays information from Domain Name System (DNS) name servers.
4765Example:
4766C:\Documents and Settings\sysadm>nslookup itu.dk
4767Server: ns3.inet.tele.dk
4768Address: 193.162.153.164
4769Non-authoritative answer:
4770Name: itu.dk
4771Address: 130.226.133.2
4772NOTE :If you write the command as above it shows as default your pc's server name firstly.
4773C:\Documents and Settings\sysadm>nslookup mail.yahoo.com itu.dk
4774Server: superman.itu.dk
4775Address: 130.226.133.2
4776Non-authoritative answer:
4777Name: login.yahoo.akadns.net
4778Address: 216.109.127.60
4779Aliases: mail.yahoo.com, login.yahoo.com
4780NOTE:Remark that in the second example we do not see the default server name.
4781There are many nslookup with optional commands.To read them type nslookup and enter
4782then type help and enter.
47833-)finger:
4784Displays the information about a user on the system.
4785Example:
4786NOTE :I could not find out the name of the server that we log on (windows) at the school.
4787Sysadmin does not know that either:o)
4788But as an example I tried it on the our unix server.
4789[hilmiolgun@ssh hilmiolgun]$ finger
4790Login Name Tty Idle Login Time Office Office Phone
4791adel Adel Abu-Sharkh pts/1 7 Sep 10 00:11 (cpe.atm2-0-
47921091080.0x50a0bcb2.albnxx13.customer.tele.dk)
4793adel Adel Abu-Sharkh pts/2 9 Sep 9 23:56 (cpe.atm2-0-
47941091080.0x50a0bcb2.albnxx13.customer.tele.dk)
4795hilmiolgun Hilmi Olgun pts/9 Sep 10 00:20 (0x3ef3e2fe.albnxx8.adsl.tele.dk)
4796hm Hanne Munkholm pts/6 1:56 Sep 8 21:27 (off180.palombia.dk)
4797jcg Jens Christian Godsk pts/4 1d Sep 8 10:28 (toscana.itu.dk)
4798kaj Kenneth Ahn Jensen pts/7 Sep 10 00:11 (cpe.atm2-0-
479954493.0x50a4ad32.boanxx12.customer.tele.dk)
4800root root pts/8 1 Sep 10 00:12 (sysadm2.itu.dk)
4801troels Troels Arvin pts/5 3:49 Sep 9 20:31 (62.79.119.132.adsl.vbr.worldonline.dk)
4802webclaus Claus Bech Rasmussen pts/0 6 Sep 10 00:11 (port967.ds1-khk.adsl.cybercity.dk)
4803NOTE :What I did is :I first check the online users,and get a list of them(above).
4804Then i just choosed one user to get information about him(below)
4805[hilmiolgun@ssh hilmiolgun]$ finger hm
4806Login: hm Name: Hanne Munkholm
4807Directory: /import/home/hm Shell: /bin/bash
4808On since Mon Sep 8 21:27 (CEST) on pts/6 from off180.palombia.dk
48091 hour 56 minutes idle
4810Last login Tue Sep 9 11:05 (CEST) on pts/12 from stud127.itu.dk
4811New mail received Mon Nov 11 23:01 2002 (CET)
4812Unread since Sat Oct 5 00:00 2002 (CEST)
4813Plan:
4814World Domination... fast.
4815[hilmiolgun@ssh hilmiolgun]$
48164-)ping:
4817Simpy shows if the remote machine is available or not....
4818Example:
4819C:\Documents and Settings\sysadm>ping webmail.itu.dk
4820Pinging tarzan.itu.dk [130.226.133.3] with 32 bytes of data:
4821Reply from 130.226.133.3: bytes=32 time=29ms TTL=55
4822Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
4823Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
4824Reply from 130.226.133.3: bytes=32 time=30ms TTL=55
4825Ping statistics for 130.226.133.3:
4826Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
4827Approximate round trip times in milli-seconds:
4828Minimum = 29ms, Maximum = 30ms, Average = 29ms
4829NOTE :Remark that the remote machine is replying.Otherwise the output will be "Request time out"
4830which means the
4831remote machine is not working well.(Not answering)
48325-)tracert:
4833It simply shows the path between source and destination address.
4834Example:
4835C:\Documents and Settings\sysadm>tracert webmail.itu.dk
4836Tracing route to tarzan.itu.dk [130.226.133.3]
4837over a maximum of 30 hops:
48381 * * * Request timed out.
48392 29 ms 19 ms 29 ms ge-0-2-1-2.1000M.albnxu1.ip.tele.dk [195.249.1.2 9]
48403 29 ms 29 ms 19 ms pos1-0.622M.lynxg1.ip.tele.dk [195.249.2.46]
48414 29 ms 19 ms 29 ms herman.fsknet.lyngby.forskningsnettet.dk [192.38 .7.1]
48425 29 ms 29 ms 19 ms 130.225.244.214
48436 29 ms 29 ms 29 ms 1.ku.forskningsnettet.dk [130.225.245.90]
48447 29 ms 29 ms 29 ms rk.itu.forskningsnettet.dk [130.226.249.30]
48458 29 ms 29 ms 29 ms 130.225.245.86
48469 29 ms 29 ms 29 ms tarzan.itu.dk [130.226.133.3]
4847Trace complete.
48486-)ftp:
4849For file transferring..(File transfer protocol)
4850Example:Lets you dont have an ftp software and you want to get a file from your school harddisk.
4851So to do that:
4852C:\Documents and Settings\sysadm>ftp
4853ftp> open
4854To ftp.itu.dk
4855Connected to ssh.itu.dk.
4856220 ProFTPD 1.2.8rc2 Server (ProFTPD Default Installation) [ssh.it-c.dk]
4857NOTE:What am I doing is simply:typing them one-by-one(after each typing remember to enter)
4858ftp,open,ftp.itu.dk
4859User (ssh.itu.dk:(none)): hilmiolgun
4860331 Password required for hilmiolgun.
4861Password:
4862230 User hilmiolgun logged in.
4863NOTE:The server will require username and password..
4864ftp> help
4865Commands may be abbreviated. Commands are:
4866! delete literal prompt send
4867? debug ls put status
4868append dir mdelete pwd trace
4869ascii disconnect mdir quit type
4870bell get mget quote user
4871binary glob mkdir recv verbose
4872bye hash mls remotehelp
4873cd help mput rename
4874close lcd open rmdir
4875ftp> help dir
4876dir List contents of remote directory
4877NOTE: If it is your first time to those commands just type help and get the commands.If you dont
4878know how to use
4879them type help commandname..
4880ftp> dir
4881200 PORT command successful
4882150 Opening ASCII mode data connection for file list
4883drwx------ 4 hilmiolgun hilmiolgun 155 Jul 1 14:02 Desktop
4884drwx------ 2 hilmiolgun hilmiolgun 4096 May 30 10:21 Mail
4885drwxr-xr-x 5 hilmiolgun hilmiolgun 90 Sep 2 02:59 MobilePositionSDK
4886drwx------ 7 hilmiolgun hilmiolgun 4096 Aug 8 2002 NTnetscape
4887drwxr--r-- 13 hilmiolgun hilmiolgun 4096 Sep 4 01:56 New Folder
4888-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 TTI409B
4889drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 cgi-bin
4890-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 geu
4891-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 hilmiolgun
4892drwxr-xr-x 6 hilmiolgun hilmiolgun 4096 Aug 14 15:59 image
4893drwxr-xr-x 3 hilmiolgun hilmiolgun 4096 Jul 29 16:03 jmf20-apidocs
4894drwxr-xr-x 4 hilmiolgun hilmiolgun 4096 Sep 9 14:10 NOTEsieee
4895drwx------ 2 hilmiolgun hilmiolgun 6 Feb 21 2002 nsmail
4896drwx------ 3 hilmiolgun hilmiolgun 103 Feb 21 2002 office52
4897drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 private
4898drwxr--rwx 2 hilmiolgun hilmiolgun 4096 Aug 23 12:02 public_html
4899drwxr-xr-x 5 hilmiolgun hilmiolgun 4096 Sep 6 03:30 speech
4900-rw-rw-r-- 1 hilmiolgun hilmiolgun 2630 Sep 9 13:58 test.txt
4901-rw-rw-r-- 1 hilmiolgun hilmiolgun 148 Sep 9 14:03 testing.txt
4902226 Transfer complete.
4903ftp: 1318 bytes received in 0,24Seconds 5,49Kbytes/sec.
4904ftp> get testing.txt
4905200 PORT command successful
4906150 Opening ASCII mode data connection for testing.txt (148 bytes)
4907226 Transfer complete.
4908ftp: 161 bytes received in 0,02Seconds 8,05Kbytes/sec.
4909NOTE :After taking a look to the school harddisk ,I copied a file "testing.txt" to my local harddisk....
4910ftp> !dir
4911Volume in drive C has no label.
4912Volume Serial Number is 0868-D52D
4913Directory of C:\Documents and Settings\sysadm
491410-09-2003 00:21 <DIR> .
491510-09-2003 00:21 <DIR> ..
491631-08-2003 07:28 <DIR> .java
491725-04-2003 12:18 <DIR> .javaws
491823-04-2003 15:26 <DIR> .jpi_cache
491926-08-2003 04:59 <DIR> .Nokia
492007-09-2003 01:46 12.546 .plugin140_03.trace
492107-09-2003 04:46 693 .plugin141_02.trace
492207-09-2003 01:20 164 .saves-3824-IBMR31IMAGE
492307-09-2003 01:20 <DIR> Desktop
492407-09-2003 08:05 <DIR> Favorites
492506-09-2003 05:29 80.140 love.wav
492609-09-2003 23:45 <DIR> mindterm
492709-09-2003 11:02 <DIR> My Documents
492810-09-2003 00:21 2.903 plugin131_08.trace
492925-04-2003 11:44 <DIR> Start Menu
493006-09-2003 21:21 <DIR> studio5se_user
493106-09-2003 05:32 18 test.txt
493206-09-2003 05:20 70 testing
493310-09-2003 00:37 161 testing.txt
493426-08-2003 03:46 <DIR> WINDOWS
49358 File(s) 96.695 bytes
493613 Dir(s) 3.842.056.192 bytes free
4937ftp> send love.wav
4938200 PORT command successful
4939150 Opening ASCII mode data connection for love.wav
4940226 Transfer complete.
4941ftp: 80140 bytes sent in 3,97Seconds 20,21Kbytes/sec.
4942ftp> dir
4943200 PORT command successful
4944150 Opening ASCII mode data connection for file list
4945drwx------ 4 hilmiolgun hilmiolgun 155 Jul 1 14:02 Desktop
4946drwx------ 2 hilmiolgun hilmiolgun 4096 May 30 10:21 Mail
4947drwxr-xr-x 5 hilmiolgun hilmiolgun 90 Sep 2 02:59 MobilePositionSDK
4948drwx------ 7 hilmiolgun hilmiolgun 4096 Aug 8 2002 NTnetscape
4949drwxr--r-- 13 hilmiolgun hilmiolgun 4096 Sep 4 01:56 New Folder
4950-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 TTI409B
4951drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 cgi-bin
4952-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 geu
4953-rw-rw-r-- 1 hilmiolgun hilmiolgun 74 Sep 9 12:56 hilmiolgun
4954drwxr-xr-x 6 hilmiolgun hilmiolgun 4096 Aug 14 15:59 image
4955drwxr-xr-x 3 hilmiolgun hilmiolgun 4096 Jul 29 16:03 jmf20-apidocs
4956-rw-rw-r-- 1 hilmiolgun hilmiolgun 80137 Sep 9 22:36 love.wav
4957drwxr-xr-x 4 hilmiolgun hilmiolgun 4096 Sep 9 14:10 NOTEsieee
4958drwx------ 2 hilmiolgun hilmiolgun 6 Feb 21 2002 nsmail
4959drwx------ 3 hilmiolgun hilmiolgun 103 Feb 21 2002 office52
4960drwx------ 2 hilmiolgun hilmiolgun 6 Jan 21 2002 private
4961drwxr--rwx 2 hilmiolgun hilmiolgun 4096 Aug 23 12:02 public_html
4962drwxr-xr-x 5 hilmiolgun hilmiolgun 4096 Sep 6 03:30 speech
4963-rw-rw-r-- 1 hilmiolgun hilmiolgun 2630 Sep 9 13:58 test.txt
4964-rw-rw-r-- 1 hilmiolgun hilmiolgun 148 Sep 9 14:03 testing.txt
4965226 Transfer complete.
4966ftp: 1387 bytes received in 0,07Seconds 19,81Kbytes/sec.
4967ftp>
4968NOTE:At the end first looking at the local working directory and sending a file "love.wav" to the
4969school harddisk.
49707-)net:
4971It has many options,which are for checking/starting/stopping nt
4972services,users,messaging,configuration and so on...
4973Some of those options require administration privileges..
4974Example:
4975NOTE: To have an overview of commands options....
4976C:\Documents and Settings\sysadm>net
4977The syntax of this command is:
4978NET COMMANDS
4979NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
4980HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
4981SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
4982NOTE: And furthermore to get an overview of a specific option ...
4983C:\Documents and Settings\sysadm>net help print
4984The syntax of this command is:
4985NET PRINT
4986\\computername\sharename
4987[\\computername] job# [/HOLD | /RELEASE | /DELETE]
4988NET PRINT displays print jobs and shared queues.
4989For each queue, the display lists jobs, showing the size
4990and status of each job, and the status of the queue.
4991\\computername Is the name of the computer sharing the printer
4992queue(s).
4993sharename Is the name of the shared printer queue.
4994job# Is the identification number assigned to a print
4995job. A computer with one or more printer queues
4996assigns each print job a unique number.
4997/HOLD Prevents a job in a queue from printing.
4998The job stays in the printer queue, and other
4999jobs bypass it until it is released.
5000/RELEASE Reactivates a job that is held.
5001/DELETE Removes a job from a queue.
5002NET HELP command | MORE displays Help one screen at a time.
5003Finally in addition to above there are also those commands: hostname ,lpq, lpr ,rsh ,tftp ,nbstat
5004,netstat.
5005To get familiar with those commands simply type commandname /? at the command line.
5006C:\>net
5007The syntax of this command is:
5008NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
5009HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
5010SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
5011C:\>net use
5012New connections will not be remembered.
5013Status Local Remote Network
5014-------------------------------------------------------------------------------
5015OK F: \\cse-sec\fac Microsoft Windows Network
5016C:\>net user
5017User accounts for \\CSE-DEPT-05
5018-------------------------------------------------------------------------------
5019Administrator Guest
5020C:\>net statistics
5021Statistics are available for the following running services:
5022Server
5023Workstation
5024Displays protocol statistics and current TCP/IP network connections.
5025NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]
5026-a Displays all connections and listening ports.
5027-e Displays Ethernet statistics. This may be combined with the
5028-s option.
5029-n Displays addresses and port numbers in numerical form.
5030-p proto Shows connections for the protocol specified by proto; proto
5031may be TCP or UDP. If used with the -s option to display
5032per-protocol statistics, proto may be TCP, UDP, or IP.
5033-r Displays the routing table.
5034-s Displays per-protocol statistics. By default, statistics
5035are shown for TCP, UDP and IP; the -p option may be used
5036to specify a subset of the default.
5037interval Redisplays selected statistics, pausing interval seconds
5038between each display. Press CTRL+C to stop redisplaying
5039statistics. If omitted, netstat will print the current
5040configuration information once.
5041C:\>net name
5042Name
5043-------------------------------------------------------------------------------
5044CSE-DEPT-05
5045C:\>net session
5046Computer User name Client Type Opens Idle time
5047-------------------------------------------------------------------------------
5048\\ENGLISH-03 Windows NT 1381 0 00:10:47
5049\\ENGLISHBDC Windows NT 1381 0 00:02:01
5050C:\>net accounts
5051Force user logoff how long after time expires?: Never
5052Minimum password age (days): 0
5053Maximum password age (days): 42
5054Minimum password length: 0
5055Length of password history maintained: None
5056Lockout threshold: Never
5057Lockout duration (minutes): 30
5058Lockout observation window (minutes): 30
5059Computer role: WORKSTATION
5060C:\>net localgroup
5061Aliases for \\CSE-DEPT-05
5062-------------------------------------------------------------------------------
5063*Administrators *Backup Operators *Guests
5064*Power Users *Replicator *Users
5065C:\>net config server
5066Server Name \\CSE-DEPT-05
5067Server Comment
5068Software version Windows NT 4.0
5069Server is active on NetBT_DLKRTS1 (0050ba8b326b) NetBT_DLKRTS1
5070(0050ba8b326b) NwlnkIpx (0050ba8b326b) NwlnkNb (0050ba8b326b) Nbf_DLKRTS1 (0050
5071ba8b326b)
5072Server hidden No
5073Maximum Logged On Users 10
5074Maximum open files per session 2048
5075Idle session time (min) 15
5076C:\>net config workstation
5077Computer name \\CSE-DEPT-05
5078User name Administrator
5079Workstation active on NwlnkNb (0050BA8B326B) NetBT_DLKRTS1 (0050B
5080A8B326B) Nbf_DLKRTS1 (0050BA8B326B)
5081Software version Windows NT 4.0
5082Workstation domain WORKGROUP
5083Logon domain CSE-DEPT-05
5084COM Open Timeout (sec) 3600
5085COM Send Count (byte) 16
5086COM Send Timeout (msec) 250
5087C:\>net share
5088Share name Resource Remark
5089-------------------------------------------------------------------------------
5090D$ D:\ Default share
5091IPC$ Remote IPC
5092C$ C:\ Default share
5093ADMIN$ C:\WINNT Remote Admin
5094E$ E:\ Default share
5095abishek E:\abishek
5096akshu E:\akshu
5097HARSHINI D:\ HARSHINI
5098DHARSHINI E:\ DHARSHINI
5099C:\>net stop messenger
5100The Messenger service is stopping.
5101The Messenger service was stopped successfully.
5102C:\>net start messenger
5103The Messenger service is starting...
5104The Messenger service was started successfully.
5105Network Configuration commands
5106ifconfig
5107This command is used to configure network interfaces, or to display their current
5108configuration. In addition to activating and deactivating interfaces with the “up†and “downâ€
5109settings, this command is necessary for setting an interface's address information if you don't
5110have the ifcfg script.
5111Use ifconfig as either:
5112ifconfig
5113This will simply list all information on all network devices currently up.
5114ifconfig eth0 down
5115This will take eth0 (assuming the device exists) down, it won't be able to receive or send
5116anything until you put the device back “up†again.
5117Clearly there are a lot more options for this tool, you will need to read the manual/info page to
5118learn more about them.
5119ifup
5120Use ifup device-name to bring an interface up by following a script (which will contain your
5121default networking settings). Simply type ifup and you will get help on using the script.
5122For example typing:
5123ifup eth0
5124Will bring eth0 up if it is currently down.
5125ifdown
5126Use ifdown device-name to bring an interface down using a script (which will contain your
5127default network settings). Simply type ifdown and you will get help on using the script.
5128For example typing:
5129ifdown eth0
5130Will bring eth0 down if it is currently up.
5131ifcfg
5132Use ifcfg to configure a particular interface. Simply type ifcfg to get help on using this script.
5133For example, to change eth0 from 192.168.0.1 to 192.168.0.2 you could do:
5134ifcfg eth0 del 192.168.0.1
5135ifcfg eth0 add 192.168.0.2
5136The first command takes eth0 down and removes that stored IP address and the second one
5137brings it back up with the new address.
5138route
5139The route command is the tool used to display or modify the routing table. To add a gateway
5140as the default you would type:
5141route add default gw some_computer
5142INTERNET SPECIFIC COMMANDS
5143host
5144Performs a simple lookup of an internet address (using the Domain Name System, DNS).
5145Simply type:
5146host ip_address
5147or
5148host domain_name
5149dig
5150The "domain information groper" tool. More advanced then host... If you give a hostname as
5151an argument to output information about that host, including it's IP address, hostname and
5152various other information.
5153For example, to look up information about “www.amazon.com†type:
5154dig www.amazon.com
5155To find the host name for a given IP address (ie a reverse lookup), use dig with the `-x' option.
5156dig -x 100.42.30.95
5157This will look up the address (which may or may not exist) and returns the address of the
5158host, for example if that was the address of “http://slashdot.org†then it would return
5159“http://slashdot.orgâ€.
5160dig takes a huge number of options (at the point of being too many), refer to the manual page
5161for more information.
5162whois
5163(now BW whois) is used to look up the contact information from the “whois†databases, the
5164servers are only likely to hold major sites. Note that contact information is likely to be hidden
5165or restricted as it is often abused by crackers and others looking for a way to cause malicious
5166damage to organisation's.
5167wget
5168(GNU Web get) used to download files from the World Wide Web.
5169To archive a single web-site, use the -m or --mirror (mirror) option.
5170Use the -nc (no clobber) option to stop wget from overwriting a file if you already have it.
5171Use the -c or --continue option to continue a file that was unfinished by wget or another
5172program.
5173Simple usage example:
5174wget url_for_file
5175This would simply get a file from a site.
5176wget can also retrieve multiple files using standard wildcards, the same as the type used in
5177bash, like *, [ ], ?. Simply use wget as per normal but use single quotation marks (' ') on the
5178URL to prevent bash from expanding the wildcards. There are complications if you are
5179retrieving from a http site (see below...).
5180Advanced usage example, (used from wget manual page):
5181wget --spider --force-html -i bookmarks.html
5182This will parse the file bookmarks.html and check that all the links exist.
5183Advanced usage: this is how you can download multiple files using http (using a wildcard...).
5184Notes: http doesn't support downloading using standard wildcards, ftp does so you may use
5185wildcards with ftp and it will work fine. A work-around for this http limitation is shown
5186below:
5187wget -r -l1 --no-parent -A.gif http://www.website.com[1]
5188This will download (recursively), to a depth of one, in other words in the current directory and
5189not below that. This command will ignore references to the parent directory, and downloads
5190anything that ends in “.gifâ€. If you wanted to download say, anything that ends with “.pdf†as
5191well than add a -A.pdf before the website address. Simply change the website address and the
5192type of file being downloaded to download something else. Note that doing -A.gif is the same
5193as doing -A “*.gif†(double quotes only, single quotes will not work).
5194wget has many more options refer to the examples section of the manual page, this tool is very
5195well documented.
5196Alternative website downloaders: You may like to try alternatives like httrack. A full GUI
5197website downloader written in python and available for GNU/Linux
5198curl
5199curl is another remote downloader. This remote downloader is designed to work without user
5200interaction and supports a variety of protocols, can upload/download and has a large number
5201of tricks/work-arounds for various things. It can access dictionary servers (dict), ldap servers,
5202ftp, http, gopher, see the manual page for full details.
5203To access the full manual (which is huge) for this command type:
5204curl -M
5205For general usage you can use it like wget. You can also login using a user name by using the
5206-u option and typing your username and password like this:
5207curl -u username:password http://www.placetodownload/file
5208To upload using ftp you the -T option:
5209curl -T file_name ftp://ftp.uploadsite.com
5210To continue a file use the -C option:
5211curl -C - -o file http://www.site.com
5212View and modify network interfaces
5213ifconfig -a Show information about all network interfaces
5214ifconfig eth0 Show information only about the interface eth0
5215ifconfig eth0 up Bring up the interface eth0
5216ifconfig eth0 down Take down the interface eth0
5217Simple network diagnostic commands
5218ping hostname Send ICMP echo requests to the host hostname
5219traceroute hostname Trace the network path to hostname
5220View open network connections
5221netstat -a Show information about all open network connections
5222netstat -a | grep LISTEN Show information about all open network ports
5223Set/view routing information
5224netstat -r View system routing tables
5225route View system routing tables
5226The command route can also be used to add or delete routes. Examples:
5227route add -host 192.168.3.4 gw 192.168.3.1 netmask 255.255.0.0
5228route del -host 192.168.3.4
5229NETSTAT.exe TCP/IP Network Statistics
5230Displays protocol statistics and current TCP/IP network connections.
5231NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]
5232-a Displays all connections and listening ports.
5233-e Displays Ethernet statistics. This may be combined with the -s option.
5234-n Displays addresses and port numbers in numerical form.
5235-p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP.
5236If used with the -s option to display per-protocol statistics, proto may be TCP, UDP,
5237or IP.
5238-r Displays the routing table.
5239-s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP;
5240the -p option may be used to specify a subset of the default.
5241interval Redisplays selected statistics, pausing interval seconds between each display. Press
5242CTRL+C to stop redisplaying statistics. If omitted, netstat will print the current
5243configuration information once.
5244C:\WINDOWS>netstat -a
5245Active Connections
5246Proto Local Address Foreign Address State
5247TCP My_Comp:ftp localhost:0 LISTENING
5248TCP My_Comp:80 localhost:0 LISTENING
5249Or with the "-an" parameters:
5250C:\WINDOWS>netstat -an
5251Active Connections
5252Proto Local Address Foreign Address State
5253TCP 0.0.0.0:21 0.0.0.0:0 LISTENING
5254TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
5255By simply opening a browser connection to both the HTTP (port 80) and FTP (port 21) servers
5256(while still offline!), I saw the following:
5257C:\WINDOWS>netstat -a
5258Active Connections
5259Proto Local Address Foreign Address State
5260TCP My_Comp:ftp localhost:0 LISTENING
5261TCP My_Comp:80 localhost:0 LISTENING
5262TCP My_Comp:1104 localhost:0 LISTENING
5263TCP My_Comp:ftp localhost:1104 ESTABLISHED
5264TCP My_Comp:1102 localhost:0 LISTENING
5265TCP My_Comp:1103 localhost:0 LISTENING
5266TCP My_Comp:80 localhost:1111 TIME_WAIT
5267TCP My_Comp:1104 localhost:ftp ESTABLISHED
5268TCP My_Comp:1107 localhost:0 LISTENING
5269TCP My_Comp:1112 localhost:80 TIME_WAIT
5270UDP My_Comp:1102 *:*
5271UDP My_Comp:1103 *:*
5272UDP My_Comp:1107 *:*
5273This may be a bit confusing to some people, but remember I'm running BOTH the servers and clients
5274on the same machine in these examples. A little later (using both 'a' and 'n') I got this:
5275C:\WINDOWS>netstat -an
5276Active Connections
5277Proto Local Address Foreign Address State
5278TCP 0.0.0.0:21 0.0.0.0:0 LISTENING
5279TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
5280TCP 0.0.0.0:1104 0.0.0.0:0 LISTENING
5281TCP 127.0.0.1:21 127.0.0.1:1104 FIN_WAIT_2
5282TCP 127.0.0.1:1102 0.0.0.0:0 LISTENING
5283TCP 127.0.0.1:1103 0.0.0.0:0 LISTENING
5284TCP 127.0.0.1:1104 127.0.0.1:21 CLOSE_WAIT
5285TCP 127.0.0.1:1107 0.0.0.0:0 LISTENING
5286UDP 127.0.0.1:1102 *:*
5287UDP 127.0.0.1:1103 *:*
5288UDP 127.0.0.1:1107 *:*
5289After turning off my server, I ended up with this for a while:
5290C:\WINDOWS>netstat -an
5291Active Connections
5292Proto Local Address Foreign Address State
5293TCP 127.0.0.1:80 127.0.0.1:1150 TIME_WAIT
5294TCP 127.0.0.1:80 127.0.0.1:1151 TIME_WAIT
5295PING.exe
5296Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
5297[-r count] [-s count] [[-j host-list] | [-k host-list]]
5298[-w timeout] destination-list
5299Options:
5300-t Ping the specifed host until interrupted.
5301-a Resolve addresses to hostnames.
5302-n count Number of echo requests to send.
5303-l size Send buffer size.
5304-f Set "Don't Fragment" flag in packet.
5305-i TTL Time To Live.
5306-v TOS Type Of Service.
5307-r count Record route for count hops.
5308-s count Timestamp for count hops.
5309-j host-list Loose source route along host-list.
5310-k host-list Strict source route along host-list.
5311-w timeout Timeout in milliseconds to wait for each reply.
5312There's one special IP number everyone should know about:
5313127.0.0.1 - localhost (or loopback).
5314This is used to connect ( through a browser, for example) to a Web server on your own computer.
5315(127 being reserved for this purpose.) You can use this IP number at all times. It doesn't matter if
5316you're connected to the Internet or not.
5317It's also called the loopback address because you can ping it and get returns even when you're
5318offline (not connected to any network). If you don't get any valid replies, then there's a problem with
5319the computer's Network settings. Here's a typical response to the 'ping' command:
5320Here's another recent example using the name of my computer which I have tied to the IP number
5321127.0.0.1 in my C:\WINDOWS\HOSTS file:
5322C:\WINDOWS>ping My_Comp
5323Pinging My_Comp [127.0.0.1] with 32 bytes of data:
5324Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
5325Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
5326Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
5327Reply from 127.0.0.1: bytes=32 time=1ms TTL=128
5328Ping statistics for 127.0.0.1:
5329Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
5330Approximate round trip times in milli-seconds:
5331Minimum = 0ms, Maximum = 1ms, Average = 0ms
5332TRACERT.exe Trace Route
5333Usage:
5334tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name
5335Options:
5336-d Do not resolve addresses to hostnames.
5337-h maximum_hops Maximum number of hops to search for target.
5338-j host-list Loose source route along host-list.
5339-w timeout Wait timeout milliseconds for each reply.
5340Here's an example which traces the route from some ISP in Los Angeles to the main server at UCLA
5341in California ( note how two computers relatively close to each other may be routed way round
5342about! ):
5343C:\WINDOWS>tracert www.ucla.edu
5344Tracing route to www.ucla.edu [169.232.33.129]
5345over a maximum of 30 hops:
53461 141 ms 132 ms 140 ms wla-ca-pm6.icg.net [165.236.29.85]
53472 134 ms 131 ms 139 ms whv-ca-gw1.icg.net [165.236.29.65]
53483 157 ms 132 ms 143 ms f3-1-0.lai-ca-gw1.icg.net [165.236.24.89]
53494 194 ms 193 ms 188 ms a0-0-0-1.dai-tx-gw1.icg.net [163.179.235.61]
53505 300 ms 211 ms 214 ms a1-1-0-1.ati-ga-gw1.icg.net [163.179.235.186]
53516 236 ms 237 ms 247 ms a5-0-0-1.was-dc-gw1.icg.net [163.179.235.129]
53527 258 ms 236 ms 244 ms 163.179.243.205
53538 231 ms 233 ms 230 ms wdc-brdr-03.inet.qwest.net [205.171.4.153]
53549 240 ms 230 ms 236 ms wdc-core-03.inet.qwest.net [205.171.24.69]
535510 262 ms 264 ms 263 ms hou-core-01.inet.qwest.net [205.171.5.187]
535611 281 ms 263 ms 259 ms hou-core-03.inet.qwest.net [205.171.23.9]
535712 272 ms 229 ms 222 ms lax-core-02.inet.qwest.net [205.171.5.163]
535813 230 ms 217 ms 230 ms lax-edge-07.inet.qwest.net [205.171.19.58]
535914 228 ms 219 ms 220 ms 63-145-160-42.cust.qwest.net [63.145.160.42]
536015 218 ms 222 ms 218 ms ISI-7507--ISI.POS.calren2.net [198.32.248.21]
536116 232 ms 222 ms 214 ms UCLA--ISI.POS.calren2.net [198.32.248.30]
536217 234 ms 226 ms 226 ms cbn5-gsr.calren2.ucla.edu [169.232.1.18]
536318 245 ms 227 ms 235 ms www.ucla.edu [169.232.33.129]
5364Trace complete.
5365Net Bios Stats
5366NBTSTAT.exe
5367Displays protocol statistics and current TCP/IP connections using NBT
5368(NetBIOS over TCP/IP).
5369NBTSTAT [-a RemoteName] [-A IP address] [-c] [-n] [-r] [-R] [-s] [S] [interval]
5370-a (adapter status) Lists the remote machine's name table given its name.
5371-A (Adapter status) Lists the remote machine's name table given its IP address.
5372-c (cache) Lists the remote name cache including the IP addresses.
5373-n (names) Lists local NetBIOS names.
5374-r (resolved) Lists names resolved by broadcast and via WINS
5375-R (Reload) Purges and reloads the remote cache name table
5376-S (Sessions) Lists sessions table with the destination IP addresses.
5377-s (sessions) Lists sessions table converting destination IP addresses to host names via the
5378hosts file.
5379RemoteName Remote host machine name.
5380IP address Dotted decimal representation of the IP address.
5381interval Redisplays selected statistics, pausing interval seconds between each display. Press
5382Ctrl+C to stop redisplaying statistics.
5383ROUTE.exe
5384Manipulates network routing tables.
5385ROUTE [-f] [command [destination] [MASK netmask] [gateway]]
5386-f Clears the routing tables of all gateway entries. If this is used in conjunction
5387with one of the commands, the tables are cleared prior to running the command.
5388command Specifies one of four commands
5389PRINT Prints a route
5390ADD Adds a route
5391DELETE Deletes a route
5392CHANGE Modifies an existing route
5393destination Specifies the host to send command.
5394MASK If the MASK keyword is present, the next parameter is interpreted as the
5395netmask parameter.
5396netmask If provided, specifies a sub-net mask value to be associated with this route entry.
5397If not specified, if defaults to 255.255.255.255.
5398gateway Specifies gateway.
5399All symbolic names used for destination or gateway are looked up in the network and host
5400name database files NETWORKS and HOSTS, respectively.
5401If the command is print or delete, wildcards may be used for the destination and gateway, or
5402the gateway argument may be omitted.
5403ARP.exe Address Resolution Protocol
5404ARP -s inet_addr eth_addr [if_addr]
5405ARP -d inet_addr [if_addr]
5406ARP -a [inet_addr] [-N if_addr]
5407-a Displays current ARP entries by interrogating the current protocol data. If inet_addr
5408is specified, the IP and Physical addresses for only the specified computer are
5409displayed. If more than one network interface uses ARP, entries for each ARP
5410table are displayed.
5411-g (Same as -a)
5412inet_addr Specifies an internet address.
5413-N if_addr Displays the ARP entries for the network interface specified by if_addr.
5414-d Deletes the host specified by inet_addr.
5415-s Adds the host and associates the Internet address inet_addr with the Physical address
5416eth_addr. The Physical address is given as 6 hexadecimal bytes separated by hyphens.
5417The entry is permanent.
5418eth_addr Specifies a physical address.
5419if_addr If present, this specifies the Internet address of the interface
5420whose address translation table should be modified. If not present, the first
5421applicable interface will be used.