using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace UDPTest
{
/// <summary>
/// 接受
/// </summary>
public class LabelReceiveService
{
/// <summary>
/// 用于UDP发送的网络服务类
/// </summary>
static UdpClient udpcRecv = null; static IPEndPoint localIpep = null; /// <summary>
/// 开关:在监听UDP报文阶段为true,否则为false
/// </summary>
static bool IsUdpcRecvStart = false;
/// <summary>
/// 线程:不断监听UDP报文
/// </summary>
static Thread thrRecv; public static void StartReceive()
{
if (!IsUdpcRecvStart) // 未监听的情况,开始监听
{
localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), ); // 本机IP和监听端口号
udpcRecv = new UdpClient(localIpep);
thrRecv = new Thread(ReceiveMessage);
thrRecv.Start();
IsUdpcRecvStart = true;
Console.WriteLine("UDP监听器已成功启动");
}
} public static void StopReceive()
{
if (IsUdpcRecvStart)
{
thrRecv.Abort(); // 必须先关闭这个线程,否则会异常
udpcRecv.Close();
IsUdpcRecvStart = false;
Console.WriteLine("UDP监听器已成功关闭");
}
} /// <summary>
/// 接收数据
/// </summary>
/// <param name="obj"></param>
private static void ReceiveMessage(object obj)
{
while (IsUdpcRecvStart)
{
try
{
byte[] bytRecv = udpcRecv.Receive(ref localIpep);
string message = Encoding.UTF8.GetString(bytRecv, , bytRecv.Length);
Console.WriteLine(string.Format("{0}[{1}]", localIpep, message)); }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
break;
}
}
} }
}
    public static void Main(string[] args)
{
LabelReceiveService.StartReceive();
Console.ReadKey();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace UDPSendTest
{
class Program
{
/// <summary>
/// 用于UDP发送的网络服务类
/// </summary>
private static UdpClient udpcSend = null; static IPEndPoint localIpep = null; public static void Main(string[] args)
{
localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), ); // 本机IP和监听端口号
udpcSend = new UdpClient(localIpep); // 实名发送
string msg = null;
while ((msg = Console.ReadLine()) != null)
{
Thread thrSend = new Thread(SendMessage);
thrSend.Start(msg);
}
Console.ReadKey();
} /// <summary>
/// 发送信息
/// </summary>
/// <param name="obj"></param>
private static void SendMessage(object obj)
{
try
{
string message = (string)obj;
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), ); // 发送到的IP地址和端口号
udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
//udpcSend.Close();
}
catch { }
} }
}

C# UDP发送和接收的更多相关文章

  1. UDP发送和接收

    发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...

  2. Java UDP发送与接收

    IP地址?端口号?主机名? 什么是Socket? 什么是UDP? 什么是TCP? UDP和TCP区别? 以上问题请自行百度,有标准解释,此处不再赘述,直接上干货! 实例: 发送端: public cl ...

  3. Linux系统下UDP发送和接收广播消息小例子

    // 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...

  4. Linux系统下UDP发送和接收广播消息小样例

    [cpp] view plaincopy // 发送端 #include <iostream> #include <stdio.h> #include <sys/sock ...

  5. C# WinForm UDP 发送和接收消息

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; ...

  6. java下udp的DatagramSocket 发送与接收

    发送 package cn.stat.p4.ipdemo; import java.io.BufferedReader; import java.io.IOException; import java ...

  7. Udp实现消息的发送和接收、以及图片的上传

    //Udp实现消息的发送和接收 import java.io.IOException; import java.net.DatagramPacket; import java.net.Datagram ...

  8. C#中UDP数据的发送、接收

    Visual C# UDP数据的发送、接收包使用的主要类及其用法: 用Visual C# UDP协议的实现,最为常用,也是最为关键的类就是UdpClient,UdpClient位于命名空间System ...

  9. 项目总结22:Java UDP Socket数据的发送和接收

    项目总结22:Java UDP Socket数据的发送和接收 1-先上demo 客户端(发送数据) package com.hs.pretest.udp; import java.io.IOExcep ...

随机推荐

  1. Vue + GraphQL初试

    基本用法 GraphQL概述 GraphQL基本语法特性 GraphQL类型系统 GraphQL类型系统内置基础类型 GraphQL类型系统内置修饰符 GraphQL工作原理 GraphQL执行过程 ...

  2. 【数据库开发】is not allowed to connect to this MySQL server解决办法

    ERROR 1130: Host '192.168.1.3′ is not allowed to connect to this MySQL server这是告诉你没有权限连接指定IP的主机,下面我们 ...

  3. selenium之定位详篇

    一.Web定位方法(find_elements返回元素list) 1.id: find_element_by_id()  # find_element_by_id("id") fi ...

  4. SrpingBoot入门到入坟03-基于idea快速创建SpringBoot应用

    先前先创建Maven项目然后依照官方文档再然后编写主程序写业务逻辑代码才建立好SpringBoot项目,这样太过麻烦,IDE都支持快速创建,下面基于idea: 使用Spring Initializer ...

  5. Word 删除脚注尾注前边的编号

    1. 前言 一般我们插入尾注都是有编号的,怎么插入无编号的尾注? 2. 步骤 进入引用选项卡,然后注意不要点插入脚注,而要点脚注最右下方的小方框,出现脚注和尾注的对话框,点符号(Y)...,选第一个格 ...

  6. WEBservice的浏览器及元素的常用函数及变量整理总结 (selenium )

    由于网页自动化要操作浏览器以及浏览器页面元素,这里笔者就将浏览器及页面元素常用的函数及变量整理总结一下,以供读者在编写网页自动化测试时查阅. from selenium import webdrive ...

  7. 【闭包】Pants On Fire

    Pants On Fire 题目描述 Donald and Mike are the leaders of the free world and haven’t yet (after half a y ...

  8. 跟我一起学编程—《Scratch编程》第21课:打地鼠

    能够熟练创建并使用变量 能够熟练使用“广播”和侦测指令 能够熟练绘制角色和背景造型 能够熟练使用循环.选择等程序指令 任务描述: 绘制有6个地洞的背景:绘制锤子的两个造型:绘制地鼠的造型. 游戏开始后 ...

  9. Destination高级特性

    一.组合队列 Composite Destinations 组合队列允许用一个虚拟的destination代表多个destinations.这样就可以通过composite destinations在 ...

  10. sqlserver 聚集索引 非聚集索引

    聚集索引是一种对磁盘上实际数据重新组织以按指定的一列或者多列值排序.像我们用到的汉语字典,就是一个聚集索引.换句话说就是聚集索引会改变数据库表中数据的存放顺序.非聚集索引不会重新组织表中的数据,而是对 ...