keep moving!

          We must not cease from exploration. And the end of all our exploring will be to arrive where we began and to know the place for the first time.
          隨筆 - 37, 文章 - 2, 評(píng)論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          UDP學(xué)習(xí)之一,UDP的簡(jiǎn)單例子

          import java.net.*;
          import java.io.*;
          public class EchoClient {
            
          private String remoteHost="localhost";
            
          private int remotePort=8000;
            
          private DatagramSocket socket;

            
          public EchoClient()throws IOException{
               socket
          =new DatagramSocket(); //與本地的任意一個(gè)UDP端口綁定
            }

            
          public static void main(String args[])throws IOException{
              
          new EchoClient().talk();
            }

            
          public void talk()throws IOException {
              
          try{
                InetAddress remoteIP
          =InetAddress.getByName(remoteHost);

                BufferedReader localReader
          =new BufferedReader(new InputStreamReader(System.in));
                String msg
          =null;
                
          while((msg=localReader.readLine())!=null){
                  
          byte[] outputData=msg.getBytes();
                  DatagramPacket outputPacket
          =new DatagramPacket(outputData,
                                              outputData.length,remoteIP,remotePort);
                  socket.send(outputPacket);  
          //給EchoServer發(fā)送數(shù)據(jù)報(bào)
                  
                  DatagramPacket inputPacket
          =new DatagramPacket(new byte[512],512);
                  socket.receive(inputPacket);
                  System.out.println(
          new String(inputPacket.getData(),0,inputPacket.getLength()));  
                  
          if(msg.equals("bye"))
                    
          break;
                 }

              }
          catch(IOException e){
                 e.printStackTrace();
              }
          finally{
                 socket.close();
              }

            }

          }
            



          /****************************************************
           * 作者:孫衛(wèi)琴                                     *
           * 來源:<<Java網(wǎng)絡(luò)編程精解>>                       *
           * 技術(shù)支持網(wǎng)址:www.javathinker.org                *
           **************************************************
          */

          import java.io.*;
          import java.net.*;
          public class EchoServer {
            
          private int port=8000;
            
          private DatagramSocket socket;

            
          public EchoServer() throws IOException {
              socket
          =new DatagramSocket(port); //與本地的一個(gè)固定端口綁定
              System.out.println("服務(wù)器啟動(dòng)");
            }


            
          public String echo(String msg) {
              
          return "echo:" + msg;
            }


            
          public void service() {
              
          while (true{
                
          try {
                  DatagramPacket packet
          =new DatagramPacket(new byte[512],512);
                  socket.receive(packet);  
          //接收來自任意一個(gè)EchoClient的數(shù)據(jù)報(bào)
                  String msg=new String(packet.getData(),0,packet.getLength());         
                  System.out.println(packet.getAddress() 
          + ":" +packet.getPort()
                                      
          +">"+msg);
                  
                  packet.setData(echo(msg).getBytes());
                  socket.send(packet);  
          //給EchoClient回復(fù)一個(gè)數(shù)據(jù)報(bào)
                }
          catch (IOException e) {
                   e.printStackTrace();
                }

              }

            }


            
          public static void main(String args[])throws IOException {
              
          new EchoServer().service();
            }

          }




          /****************************************************
           * 作者:孫衛(wèi)琴                                     *
           * 來源:<<Java網(wǎng)絡(luò)編程精解>>                       *
           * 技術(shù)支持網(wǎng)址:www.javathinker.org                *
           **************************************************
          */

          import java.io.IOException;
          import java.net.DatagramPacket;
          import java.net.DatagramSocket;
          import java.net.InetAddress;
          import java.net.UnknownHostException;

          public class SendUdpData {
              
          private String remoteHost = "localhost";
              
          private int remotePort = 8000;
              
          private DatagramSocket socket;

              
          public SendUdpData() throws IOException {
                  socket 
          = new DatagramSocket(); // 與本地的任意一個(gè)UDP端口綁定
              }


              
          public void sendData() {
                  
          try {
                      InetAddress remoteIP 
          = InetAddress.getByName(remoteHost);
                      
          byte[] outputData = "Hello!".getBytes();
                      DatagramPacket outputPacket 
          = new DatagramPacket(outputData,
                              outputData.length, remoteIP, remotePort);
                      socket.send(outputPacket); 
          // 給EchoServer發(fā)送數(shù)據(jù)報(bào)
                  }
           catch (UnknownHostException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
           finally {
                      socket.close();
                  }

              }


              
          public static void main(String[] args) throws IOException {
                  
          new SendUdpData().sendData();
              }

          }

          posted on 2008-10-04 12:37 大石頭 閱讀(366) 評(píng)論(0)  編輯  收藏 所屬分類: 基礎(chǔ)

          主站蜘蛛池模板: 柳州市| 庆元县| 胶南市| 六安市| 马龙县| 高密市| 会理县| 兰考县| 大安市| 余干县| 冕宁县| 乌拉特后旗| 蕲春县| 定兴县| 邓州市| 雷波县| 峨山| 太保市| 信宜市| 白沙| 阿拉尔市| 昔阳县| 福州市| 新沂市| 库尔勒市| 会宁县| 本溪| 中山市| 临城县| 开江县| 西乌珠穆沁旗| 南平市| 岚皋县| 溧水县| 临武县| 故城县| 牡丹江市| 松滋市| 望谟县| 灵石县| 泰来县|