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
数据加载中……

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 大石头 阅读(346) 评论(0)  编辑  收藏 所属分类: 基础


只有注册用户登录后才能发表评论。


网站导航: