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, 評論 - 3, 引用 - 0

          導航

          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          blog收藏

          文件共享

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          UDP學習之一,UDP的簡單例子

          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(); //與本地的任意一個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發送數據報
                  
                  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();
              }

            }

          }
            



          /****************************************************
           * 作者:孫衛琴                                     *
           * 來源:<<Java網絡編程精解>>                       *
           * 技術支持網址: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); //與本地的一個固定端口綁定
              System.out.println("服務器啟動");
            }


            
          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);  
          //接收來自任意一個EchoClient的數據報
                  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回復一個數據報
                }
          catch (IOException e) {
                   e.printStackTrace();
                }

              }

            }


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

          }




          /****************************************************
           * 作者:孫衛琴                                     *
           * 來源:<<Java網絡編程精解>>                       *
           * 技術支持網址: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(); // 與本地的任意一個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發送數據報
                  }
           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) 評論(0)  編輯  收藏 所屬分類: 基礎

          主站蜘蛛池模板: 桃江县| 屏边| 神农架林区| 南平市| 张北县| 太仆寺旗| 富阳市| 奎屯市| 宣汉县| 香港 | 高安市| 南乐县| 台江县| 阿勒泰市| 汪清县| 内黄县| 昌邑市| 揭西县| 若尔盖县| 永州市| 武穴市| 郧西县| 扬州市| 湛江市| 蕲春县| 张北县| 苏尼特左旗| 汤阴县| 赞皇县| 平谷区| 仙桃市| 金寨县| 酒泉市| 韶山市| 门头沟区| 平和县| 石阡县| 临泽县| 镇江市| 郸城县| 济源市|