现在的位置: 首页 > 综合 > 正文

网络编程 UDP TCP

2013年10月03日 ⁄ 综合 ⁄ 共 2404字 ⁄ 字号 评论关闭

  1. //UDP协议
  2. public class UdpDemo {
  3.   
  4.   public static void main(String[] args) throws Exception{
  5.     
  6.       DatagramSocket send=new DatagramSocket();
  7.       DatagramSocket receive=new DatagramSocket(10002);
  8.       
  9.       new Thread(new UdpSend(send)).start();
  10.       new Thread(new UdpReceive(receive)).start();    
  11.   }
  12. }
  13. class UdpSend implements Runnable {
  14.   private DatagramSocket ds;
  15.   // 1.创建udp对象
  16.   public UdpSend(DatagramSocket ds) {
  17.     this.ds = ds;
  18.   }
  19.   public void run() {
  20.     try {
  21.       // 2.确定数据,并封装成数据包
  22.       BufferedReader bufr = new BufferedReader(new InputStreamReader(
  23.           System.in));
  24.       String line = null;
  25.       while ((line = bufr.readLine()) != null) {
  26.         if ("886".equals(line)) {
  27.           break;
  28.         }
  29.         byte[] buf = line.getBytes();
  30.         DatagramPacket dp = new DatagramPacket(buf, buf.length,
  31.             InetAddress.getByName("127.0.0.1"), 10000);
  32.         // 3. 通过socket服务, 将已有的数据包发送出去。
  33.         ds.send(dp);
  34.       }
  35.     } catch (Exception e) {
  36.       throw new RuntimeException("发送端失败");
  37.     }
  38.   }
  39. }
  40. class UdpReceive implements Runnable {
  41.   private DatagramSocket ds;
  42.   // 1.创建udp对象
  43.   public UdpReceive(DatagramSocket ds) {
  44.     this.ds = ds;
  45.   }
  46.   public void run() {
  47.     try {
  48.       while (true) {
  49.         // 2.定义数据包,用于存储数据
  50.         byte[] buf = new byte[1024];
  51.         DatagramPacket dp = new DatagramPacket(buf, buf.length);
  52.         // 3. 通过服务的receive方法, 将收到的数据存到数据包中
  53.         ds.receive(dp);// 阻塞方法
  54.         // 4. 通过数据包的方法获取数据
  55.         String ip = dp.getAddress().getHostAddress();
  56.         String data = new String(dp.getData(), 0, dp.getLength());
  57.         int port = dp.getPort();
  58.         System.out.println(ip + data + port);
  59.       }
  60.     } catch (Exception e) {
  61.       throw new RuntimeException("接受端失败");
  62.     }
  63.     // ds.close();
  64.   }
  65. }

  1. //TCP协议
  2. class TcpClient {
  3.   public static void main(String[] args) throws Exception {
  4.     Socket s = new Socket("192.168.1.251", 10003);
  5.     // 为了发送数据,应该获取socket流中的输出流
  6.     OutputStream out = s.getOutputStream();
  7.     out.write("11".getBytes());
  8.     InputStream in = s.getInputStream();
  9.     byte[] buf = new byte[1024];
  10.     int len = in.read(buf);
  11.     System.out.println(new String(buf, 0, len));
  12.     s.close();
  13.   }
  14. }
  15. class TcpServer {
  16.   public static void main(String[] args) throws Exception {
  17.     // 建立服务端socket服务,并监听一个端口
  18.     ServerSocket ss = new ServerSocket(10003);
  19.     // 获取客户端对象
  20.     Socket s = ss.accept();
  21.     String ip = s.getInetAddress().getHostAddress();
  22.     // 获取客户端发送过来的数据,要使用客户端对象的读取流来读取数据

抱歉!评论已关闭.