线程的应用udpClient通信
来源:互联网 发布:开淘宝店需要多少钱 编辑:程序博客网 时间:2024/06/05 03:27
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 UDP
{
class Program
{
private static UdpClient udpSender;
private static UdpClient udpRecver;
static void Main(string[] args)
{
//BeginListen();
IPEndPoint localIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); //本机ip,发送的ip地址
udpSender = new UdpClient(localIpEP);
Thread thrSend = new Thread(SendMessage); 发送的线程
thrSend.Start("hello,this is server");
BeginListen(); 开启接收线程
Console.WriteLine("server started");
Console.Read();
}
private static void BeginListen()
{
IPEndPoint localIpep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 8849); // 本机IP和监听端口号 ********************这个监听端口是必须的(个人的理解)
udpRecver = new UdpClient(localIpep);
Thread thrReceive = new Thread(ReceiveMessage);
thrReceive.Start();
Console.WriteLine("client started");
}
private static void ReceiveMessage()
{
// throw new NotImplementedException();
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
// 从远处接收到的数据
byte[] bytReceive = udpRecver.Receive(ref remoteIpEP);
string msg = Encoding.Unicode.GetString(bytReceive, 0, bytReceive.Length);
Console.WriteLine("received from server");
Console.WriteLine(msg);
}
}
private static void SendMessage(object obj)
{
//throw new NotImplementedException();
// string msg = (string)obj;
string msg ="this is send data";
byte[] sendbytes = Encoding.Unicode.GetBytes(msg);
//发送到的ip地址
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8849); 发送到的ip地址
udpSender.Send(sendbytes, sendbytes.Length, remoteIpEP);
udpSender.Close();
}
}
}
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 UDP
{
class Program
{
private static UdpClient udpSender;
private static UdpClient udpRecver;
static void Main(string[] args)
{
//BeginListen();
IPEndPoint localIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); //本机ip,发送的ip地址
udpSender = new UdpClient(localIpEP);
Thread thrSend = new Thread(SendMessage); 发送的线程
thrSend.Start("hello,this is server");
BeginListen(); 开启接收线程
Console.WriteLine("server started");
Console.Read();
}
private static void BeginListen()
{
IPEndPoint localIpep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 8849); // 本机IP和监听端口号 ********************这个监听端口是必须的(个人的理解)
udpRecver = new UdpClient(localIpep);
Thread thrReceive = new Thread(ReceiveMessage);
thrReceive.Start();
Console.WriteLine("client started");
}
private static void ReceiveMessage()
{
// throw new NotImplementedException();
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
// 从远处接收到的数据
byte[] bytReceive = udpRecver.Receive(ref remoteIpEP);
string msg = Encoding.Unicode.GetString(bytReceive, 0, bytReceive.Length);
Console.WriteLine("received from server");
Console.WriteLine(msg);
}
}
private static void SendMessage(object obj)
{
//throw new NotImplementedException();
// string msg = (string)obj;
string msg ="this is send data";
byte[] sendbytes = Encoding.Unicode.GetBytes(msg);
//发送到的ip地址
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8849); 发送到的ip地址
udpSender.Send(sendbytes, sendbytes.Length, remoteIpEP);
udpSender.Close();
}
}
}
阅读全文
0 0
- 线程的应用udpClient通信
- UDPClient类通信实践
- C#UDP入门|Socket通信|UdpClient通信
- C#UDP入门|Socket通信|UdpClient通信
- UdpClient的Available属性
- c# 使用UDPClient实现异步通信
- c# 正在终止线程 关闭UdpClient
- Socket编程-UdpClient的使用
- java 线程间通信简介(wait/notify的应用)
- 多线程在Android中的应用以及线程间的通信
- javase线程间通信(锁)的应用
- [UDP] C#UDP入门|Socket通信|UdpClient通信
- [UDP] C#UDP入门|Socket通信|UdpClient通信
- 线程通信---传统的线程通信
- 线程间的通信
- 线程之间的通信
- 线程的通信
- 线程间的通信
- 云星数据---Scala实战系列(精品版)】:Scala入门教程042-Scala实战源码-Scala 集合List
- MVC模式(三层架构模式)
- Voxel Space:不到20行代码实现地形渲染
- JMS之学习路线
- 缓存编(转)
- 线程的应用udpClient通信
- iOS 快速创建常用UI,使代码更加整洁并且提高开发效率<1>
- ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only err
- matlab中errorbar和shadedErrorBar
- 浅谈MySQL Online DDL (上)
- dubbo教程
- Ubuntu16.04 + Titan XP + cuda8.0 + cudnn5.1 + opencv3.3.0 + caffe
- 干货 | DDD实战:基于洋葱模型的分层代码架构设计
- 阅读 netmap: a novel framework for fast packet I/O 文章总结