<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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
    數(shù)據(jù)加載中……

    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發(fā)送數(shù)據(jù)報
            
            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)絡編程精解>>                       *
     * 技術支持網(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); //與本地的一個固定端口綁定
        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的數(shù)據(jù)報
            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回復一個數(shù)據(jù)報
          }
    catch (IOException e) {
             e.printStackTrace();
          }

        }

      }


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

    }




    /****************************************************
     * 作者:孫衛(wèi)琴                                     *
     * 來源:<<Java網(wǎng)絡編程精解>>                       *
     * 技術支持網(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(); // 與本地的任意一個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ù)報
            }
     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 大石頭 閱讀(363) 評論(0)  編輯  收藏 所屬分類: 基礎

    主站蜘蛛池模板: 中国精品一级毛片免费播放| 成人性生交视频免费观看| 亚洲欧洲国产综合| 精品久久久久国产免费| 免费精品视频在线| 自怕偷自怕亚洲精品| 最近免费中文字幕4| 国产精品永久免费视频| 亚洲精品国产第1页| 国产午夜鲁丝片AV无码免费| 免费一级毛片在线播放视频| 亚洲av无码片vr一区二区三区 | 亚洲理论电影在线观看| 两性刺激生活片免费视频| 一级毛片无遮挡免费全部| 亚洲精品国产福利片| 亚洲无线码一区二区三区| 成年女人午夜毛片免费看| 波多野结衣免费一区视频| 久久久久亚洲国产AV麻豆| 亚洲综合久久1区2区3区| AV在线亚洲男人的天堂| 久久精品女人天堂AV免费观看| 国产午夜无码片免费| 九九精品国产亚洲AV日韩| 亚洲国产美国国产综合一区二区 | 国产成人久久AV免费| 含羞草国产亚洲精品岁国产精品| 亚洲一二成人精品区| 亚洲国产V高清在线观看| 97在线观免费视频观看| 无码人妻一区二区三区免费n鬼沢| 免费看一级一级人妻片 | 久久久青草青青国产亚洲免观 | 亚洲色大成网站www永久一区| 女人18毛片水最多免费观看| 久久午夜羞羞影院免费观看| eeuss影院ss奇兵免费com| 亚洲精品天堂无码中文字幕| 亚洲人成在线免费观看| 久久久综合亚洲色一区二区三区 |