C# WinForm UDP 发送和接收消息
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
UdpClient client = new UdpClient();
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
Thread recvThread = new Thread(RecvMsg);
recvThread.IsBackground = true;
recvThread.Start();
} private void button1_Click(object sender, EventArgs e)
{
string ip = textBox1.Text.Trim();
string port = textBox2.Text.Trim();
if (ip == null || port == null)
{
return;
}
IPEndPoint host = new IPEndPoint(IPAddress.Parse(ip),Convert.ToInt32(port)); client.Connect(host);
richTextBox1.Text = "已连接\n";
textBox1.Enabled = false;
textBox2.Enabled = false;
button1.Enabled = false;
} private void button2_Click(object sender, EventArgs e)
{
if (client == null)
{
return;
}
string content = richTextBox2.Text.Trim();
if (content == "" || content == null)
{
return;
}
byte[] buff = Encoding.UTF8.GetBytes(content);
int v = client.Send(buff,buff.Length);
richTextBox1.Text += content + "\n";
richTextBox2.Text = null;
} UdpClient recvClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.1.237"),40477));//接收方的IP
private void RecvMsg()
{
while (1==1)
{
IPEndPoint remoteHost = null;
byte[] recvByte = recvClient.Receive(ref remoteHost);
string msg = Encoding.UTF8.GetString(recvByte);
richTextBox1.Text += msg+"\n";
}
}
}
}
界面以及结果:
C# WinForm UDP 发送和接收消息的更多相关文章
- ROS_Kinetic_26 使用rosserial_windows实现windows与ROS master发送与接收消息
使用rosserial_windows实现windows与ROS master发送与接收消息(适用版本hydro,indigo,jade,kinetic) 官方wiki地址汇总请参考:http://b ...
- ActiveMQ实例1--简单的发送和接收消息
一.环境准备 1,官网http://activemq.apache.org/下载最新版本的ActiveMQ,并解压 2,打开对应的目录,在Mac环境下,一般可以运行命令: cd /Users/***/ ...
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...
- ActiveMQ 发送和接收消息
一.添加 jar 包 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>a ...
- rabbitMQ学习笔记(二) 简单的发送与接收消息 HelloWorld
首先要下载rabbitmq的javaClient库,然后加入到项目中,下载地址为:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1. ...
- C# UDP发送和接收
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- 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 ...
- UDP发送和接收
发送函数 public bool udpSend(string ip, int port, byte[] data) { Socket socket = new Socket(AddressFamil ...
随机推荐
- Java获取不到Canal服务器端数据问题汇总(坑人的东西)
情况1:(基本都是这样的问题,) 1.需要修改canal.properties配置 vim conf/canal.properties canal.instance.parser.parallelTh ...
- python创建Django项目
创建Django项目 关注公众号"轻松学编程"了解更多. 创建一个HelloDjango项目 GitHub地址:https://github.com/liangdongchang/ ...
- [LuoguP1005]矩阵取数游戏 (DP+高精度)
题面 传送门:https://www.luogu.org/problemnew/show/P1005 Solution 我们可以先考虑贪心 我们每一次都选左右两边尽可能小的数,方便大的放在后面 听起来 ...
- http post 四种方式
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...
- c#中简单工厂模式
运算类 public class yunsuan { public static operation create(string operate) { operation oper = null; s ...
- 第三方库文件Joi对数据进行验证的方法以及解决Joi.validate is not a function的问题
Joi:javaScript对象的规则描述语言和验证器 1.npm install joi@14.3.1 2.建立joi.js文件 3.导入第三方包joi const Joi = require('j ...
- Tim Urban:如何选择真正适合你的职业?
Wait But Why是一个专注于写长博客的网站,Tim Urban是网站的创始人之一.Tim Urban专注于写长论文,与时下的轻度阅读完全背道而驰,文章动辄几千甚至上万字,但令人吃惊的是却拥有惊 ...
- SSH个人小结
初学SSH的一些总结,主要来源于谷歌搜索和鸟叔的教程http://cn.linux.vbird.org/linux_server/0310telnetssh_2.php 以及阮一峰的博客http:// ...
- 查找数组中第k大的数
问题: 查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...
- JAVA注解的继承性
摘要 本文从三个方面介绍java注解的**"继承性"**: 基于元注解@Inherited,类上注解的继承性 基于类的继承,方法/属性上注解的继承性 基于接口的继承/实现,方法/属 ...