• 网络编程-UDP2


     1 package com.yyq;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.InputStreamReader;
     5 import java.net.DatagramPacket;
     6 import java.net.DatagramSocket;
     7 import java.net.InetAddress;
     8 import java.net.SocketException;
     9 
    10 public class UdpSend2 {
    11     public static void main(String[] args) throws Exception {
    12         //1. 创建服务
    13         DatagramSocket ds = new DatagramSocket(5555);
    14         //2.从键盘中录入数据
    15         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    16         String line = null;
    17         while((line = bufr.readLine())!=null){
    18             if("88".equals(line)){
    19                 break;
    20             }
    21             byte[] buf = line.getBytes();
    22             DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),10001);
    23             ds.send(dp);
    24             //ds.close();
    25         }
    26     }
    27 }
    28 
    29 
    30 
    31 
    32 package com.yyq;
    33 
    34 import java.io.IOException;
    35 import java.net.DatagramPacket;
    36 import java.net.DatagramSocket;
    37 import java.net.SocketException;
    38 
    39 public class UdpRece2 {
    40     public static void main(String[] args) throws Exception {
    41         DatagramSocket ds = new DatagramSocket(10001);
    42         while(true){
    43             byte[] buf = new byte[1024];
    44             DatagramPacket dp = new DatagramPacket(buf,buf.length);
    45             ds.receive(dp);
    46             String ip = dp.getAddress().getHostAddress();
    47             String data = new String(dp.getData(),0,dp.getLength());
    48             System.out.println(ip + ":" + data +":"+dp.getPort());
    49             System.out.println("haode");
    50         }
    51     }
    52 }
  • 相关阅读:
    Python嵌入C/C++ (Python核心编程)
    c++ 降低文件间类的耦合度及关联度
    Python嵌入C++
    Windows编程学习笔记(十一)
    C++STL算法分析之:非变易算法
    背包九讲
    随机数 srand() & rand()
    C++ explicit 关键字解析
    [抄书]贪心策略的理论基础——拟阵
    WP7之LongListSelector控件
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/5142948.html
Copyright © 2020-2023  润新知