KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UDPEcho


1 import java.net.*;
2 import java.io.*;
3 /**
4  Plain SOCKS unaware UDP echo server and client.
5 */

6 public class UDPEcho implements Runnable JavaDoc{
7   
8    private int port;
9    private InetAddress hostIP;
10    private DatagramSocket sock;
11
12    private static final int BUF_SIZE = 1024;
13
14
15    public UDPEcho(String JavaDoc host,int port)
16       throws IOException,UnknownHostException{
17       this.hostIP = InetAddress.getByName(host);
18       this.port = port;
19       sock = new DatagramSocket();
20       System.out.println("UDP: "+sock.getLocalAddress()+":"+
21                                  sock.getLocalPort());
22       //sock.connect(hostIP,port);
23
}
24
25    public void send(String JavaDoc s) throws IOException{
26       System.out.println("Sending:"+s);
27       DatagramPacket packet = new DatagramPacket(s.getBytes(),
28                                                  s.length(),
29                          hostIP,
30                          port);
31       sock.send(packet);
32    }
33
34    public void run(){
35       byte[] buf = new byte[BUF_SIZE];
36       DatagramPacket incomingData = new DatagramPacket(buf,buf.length);
37       try{
38       while(true){
39          sock.receive(incomingData);
40          System.out.println("UDP From:"+
41                   incomingData.getAddress().getHostAddress()+":"+
42                   incomingData.getPort());
43          System.out.println(new String JavaDoc(incomingData.getData(),
44                                 0,incomingData.getLength()));
45              System.out.flush();
46          incomingData.setLength(buf.length);
47       }
48       }catch(IOException io_ex){
49      io_ex.printStackTrace();
50       }
51    }
52
53    public static void usage(){
54       System.err.print(
55       "Usage: java UDPEcho host port\n"+
56       "OR\n"+
57       "Usage: java UDPEcho port\n");
58    }
59
60    public static void doEcho(int port)throws IOException{
61       byte[] buf = new byte[BUF_SIZE];
62       DatagramPacket packet = new DatagramPacket(buf,buf.length);
63       DatagramSocket sock = new DatagramSocket(port);
64       
65       System.out.println("Starting UDP echo on"+
66                           sock.getLocalAddress().getHostAddress()+
67                           ":"+sock.getLocalPort());
68       while(true){
69      try{
70        sock.receive(packet);
71        sock.send(packet);
72            System.out.print(
73            "UDP From: "+packet.getAddress().getHostAddress()+":"+
74                         packet.getPort()+
75        "\n"+
76        new String JavaDoc(packet.getData(),0,packet.getLength())+
77        "\n"
78        );
79            System.out.flush();
80            
81        packet.setLength(buf.length);
82            //packet = new DatagramPacket(buf,buf.length);
83
}catch(IOException io_ex){
84      }
85       }
86    }
87
88    public static void main(String JavaDoc args[]){
89       int port;
90       String JavaDoc host;
91
92       if(args.length == 1){
93      try{
94        port = Integer.parseInt(args[0]);
95
96        doEcho(port);
97
98      }catch(IOException io_ex){
99        io_ex.printStackTrace();
100        System.exit(1);
101
102      }catch(NumberFormatException JavaDoc num_ex){
103        num_ex.printStackTrace();
104        System.exit(1);
105      }
106       }else if(args.length == 2){
107      try{
108          host = args[0];
109          port = Integer.parseInt(args[1]);
110
111          UDPEcho ut = new UDPEcho(host,port);
112          Thread JavaDoc thread = new Thread JavaDoc(ut);
113          thread.start();
114
115          BufferedReader in = new BufferedReader(
116                  new InputStreamReader(System.in));
117              String JavaDoc s;
118              System.out.print("Enter datagram:");
119              s = in.readLine();
120          while(s != null){
121         ut.send(s);
122         try{
123            Thread.currentThread().sleep(100);
124         }catch(InterruptedException JavaDoc i_ex){
125         }
126                 System.out.print("Enter datagram:");
127                 s = in.readLine();
128          }
129          System.exit(1);
130
131      }catch(IOException io_ex){
132        io_ex.printStackTrace();
133        System.exit(1);
134      }catch(NumberFormatException JavaDoc num_ex){
135        num_ex.printStackTrace();
136        System.exit(1);
137      }
138
139       }else{
140     usage();
141       }
142    }
143
144 }//End of class
145
Popular Tags