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 ...
随机推荐
- c#操作json数据使用newtonsoft.json
开源项目提供的一个读取示例 using System; using System.Collections.Generic; using System.IO; using System.Linq; us ...
- url请求特殊字符转换
一.问题阐述 用URL传参数的时候,用&符号连接,如果某一个参数中含"#$ ^ & * + ="这些符号的时候,在另一个页面getParameter就会取不到传过来 ...
- rsync参数说明
参数说明: log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建 pidfile = /var/run/rsyncd.pid ...
- 涨停复盘:5G概念持续活跃,军工股强势崛起
午后银行股快速拉升,三大股指大幅拉升,沪指一度临近2800点,但未能持续随后沪指小幅下行,题材股表现强势,证券板块高开低走.截止收盘,沪指涨0.93%,创业板指涨1.51%. 盘面上,银行板块午后拉升 ...
- selenium3 web自动化测试框架 三:项目实战中PO模型的设计与封装
po模型设计思想 Page Object 模式主要是将每个页面设计为一个class,其中包含页面中的需要测试的元素(按钮,输入框,标题等),这样在Selenium测试页面中可以通过调取页面类来获取页面 ...
- darknet标签转化为COCO标签
import sys import json import cv2 import os import shutil dataset = { "info": { "desc ...
- Python爬取链家二手房源信息
爬取链家网站二手房房源信息,第一次做,仅供参考,要用scrapy. import scrapy,pypinyin,requests import bs4 from ..items import L ...
- 三节课MINI计划第二周
任务:完成一份用户反馈的收集,并进行分析 第一步:去你能想到的公开.非公开渠道收集最近90天,至少40条和B站相关的有效用户差评反馈,并根据你对业务的理解分类整理,以表格的形式进行整理,以图片的方式提 ...
- 最新 美团java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.美团等10家互联网公司的校招Offer,因为某些自身原因最终选择了美团.6.7月主要是做系统复习.项目复盘.LeetCode ...
- Spring 使用日志
1. spring boot项目默认使用什么技术处理日志? 实例代码 log.debug("===============================用户信息:", user) ...