C# UDP发送和接收
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发送和接收的更多相关文章
- UDP发送和接收
发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...
- Java UDP发送与接收
IP地址?端口号?主机名? 什么是Socket? 什么是UDP? 什么是TCP? UDP和TCP区别? 以上问题请自行百度,有标准解释,此处不再赘述,直接上干货! 实例: 发送端: public cl ...
- Linux系统下UDP发送和接收广播消息小例子
// 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...
- Linux系统下UDP发送和接收广播消息小样例
[cpp] view plaincopy // 发送端 #include <iostream> #include <stdio.h> #include <sys/sock ...
- C# WinForm UDP 发送和接收消息
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; ...
- java下udp的DatagramSocket 发送与接收
发送 package cn.stat.p4.ipdemo; import java.io.BufferedReader; import java.io.IOException; import java ...
- Udp实现消息的发送和接收、以及图片的上传
//Udp实现消息的发送和接收 import java.io.IOException; import java.net.DatagramPacket; import java.net.Datagram ...
- C#中UDP数据的发送、接收
Visual C# UDP数据的发送、接收包使用的主要类及其用法: 用Visual C# UDP协议的实现,最为常用,也是最为关键的类就是UdpClient,UdpClient位于命名空间System ...
- 项目总结22:Java UDP Socket数据的发送和接收
项目总结22:Java UDP Socket数据的发送和接收 1-先上demo 客户端(发送数据) package com.hs.pretest.udp; import java.io.IOExcep ...
随机推荐
- Java读取Excel文件(包括xls和xlsx)的样例程序
样例程序如下所示,其中: parseXls()函数依赖于jxl,只能读取xls格式文件: parseExcel()函数依赖于apache poi,能够读取xls和xlsx两种格式的文件. jxl的依赖 ...
- CentOS 7中安装和配置Promethues
Prometheus 是什么? Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采用Prome ...
- 在C/C++中常用的符号
C++中&和*的用法一直是非常让人头疼的难点,课本博客上讲这些知识点一般都是分开讲其用法的,没有详细的总结,导致我在这方面的知识结构格外混乱,在网上找到了一篇英文文章简单总结了这两个符号的一些 ...
- 慕课零基础学java语言翁恺老师——第二周编程题
1.时间换算(5分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果 ...
- python安装第三方的包
转载自:http://blog.csdn.net/xyqzki/article/details/38414433 第一种基本安装方法 1 下载第三方包,解压 2 在命令提示符里输入cmd,然后用cd进 ...
- python 实现快速排序(面试经常问到)
# -*- coding: UTF-8 -*- # 递归实现, 把过程打印出来便于理解 def quick_sort1(lis, start, end): if start >= end: re ...
- OOP_面向对象程序设计概述
李际军老师"面向对象程序设计"课程第一课笔记整理 面向对象程序设计概述 20世纪90年代以来面向对象程序设计(Object Oriented Programming, 简称OOP) ...
- php文件操作类
<?php /** *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 * 列出目录里的文件等功能,路径后面别忘了加"/" */ clas ...
- 『Linux』第一节: 部署虚拟环境
一. 准备工具 1. VMware Workstation Pro下载 1.1 VMware Workstation Pro 激活许可证 UY758-0RXEQ-M81WP-8ZM7Z-Y3HDA V ...
- chrome 调试 ios h5
1,安装itunes, 否则无法识别iphone设备 2,开启调试模式 ,打开 iPhone 依次进入 设置 > Safari > 高级 > Web 检查 > 启用 3,下载 ...