1、界面和功能如下,实现功能
(1)100毫秒接收一次,超过1秒钟没有接收到目标信息,则视为连接断开
(2)接收的Soecket目标地址、端口号显示
(3)1秒钟自动发送
(4)本机地址和端口号、目标地址和端口号,从配置文件中读取。
2、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UdpTest
{
public partial class MaiForm : Form
{
public MaiForm()
{
InitializeComponent();
}
//
IniFile ini = new IniFile("./Config.ini");
//
bool isRecvIpShow = false;
bool isContinueSend = false; //是否连续发送
bool isAutoNewLine = false; //是否自动换行
// 创建UdpClient实例
Socket udpClient;
Thread t;
Thread t2;
string udpRecv;
string target_IpPort;
long heartFlag = 0;
long nRecvLengthSum = 0;
private void MaiForm_Load(object sender, EventArgs e)
{
//char ch = '1';
//string str = "";
//str = ch.ToString();
//string str = "1,2,3";
//char ch1 = str[0];
//char ch2 = str[2];
//char ch3 = str[4];
//long a = 6553611111111111111;
try
{
//
txtLocalIp.Text = ini.IniReadValue("系统参数", "本机IP地址");
txtLocalPort.Text = ini.IniReadValue("系统参数", "本机端口号");
txtSendIp.Text = ini.IniReadValue("系统参数", "目标IP地址");
txtSendPort.Text = ini.IniReadValue("系统参数", "目标端口号");
//
udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpClient.Bind(new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), int.Parse(txtLocalPort.Text)));
t = new Thread(sendMsg);
t.Start();
t2 = new Thread(ReciveMsg);
t2.Start();
timer1.Start();
target_IpPort = txtSendIp.Text + ":" + txtSendPort.Text;
txtLocalUdpState.Text = "Udp创建成功";
}
catch
{
txtLocalUdpState.Text = "Udp创建失败";
}
}
/// <summary>
/// 向特定ip的主机的端口发送数据报
/// </summary>
void sendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse(txtSendIp.Text), int.Parse(txtSendPort.Text));
while (true)
{
if(isContinueSend)
{
string msg = txtSend.Text;
udpClient.SendTo(Encoding.Default.GetBytes(msg), point);
Thread.Sleep(1000);
}
}
}
/// <summary>
/// 接收发送给本机ip对应端口号的数据报
/// </summary>
void ReciveMsg()
{
while (true)
{
try
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
byte[] buffer = new byte[1024];
int length = udpClient.ReceiveFrom(buffer, ref point);//接收数据报
string strRecvTemp = Encoding.UTF8.GetString(buffer, 0, length);
if (length > 0)
{
nRecvLengthSum += length;
//是否显示接收的ip地址和端口号
if (isRecvIpShow)
{
//是否自动换行
if (!isAutoNewLine)
{
udpRecv += point.ToString() + ": " + strRecvTemp;
}
else
{
udpRecv += point.ToString() + ": " + strRecvTemp + "\r\n";
}
}
else
{
if (!isAutoNewLine)
{
udpRecv += strRecvTemp;
}
else
{
udpRecv += strRecvTemp + "\r\n";
}
}
string str = point.ToString();
if (target_IpPort == str)
{
heartFlag = 0;
}
}
Thread.Sleep(100);
}
catch { }
}
}
private void btnSend_Click(object sender, EventArgs e)
{
EndPoint point = new IPEndPoint(IPAddress.Parse(txtSendIp.Text), int.Parse(txtSendPort.Text));
string msg = txtSend.Text;
udpClient.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
private void timer1_Tick(object sender, EventArgs e)
{
isAutoNewLine = checkAutoNewLine.Checked;
isRecvIpShow = check_接收Ip地址和端口号.Checked;
txtUdpRecv.Text = udpRecv;
txtRecvLength.Text = nRecvLengthSum.ToString();
if (heartFlag > 10)
{
txtTargetConnectState.Text = "连接断开";
}
else
{
txtTargetConnectState.Text = "连接成功";
}
heartFlag++;
//if (heartFlag > 65534)
//{
// heartFlag = 100;
//}
txtDisConnectTime.Text = (heartFlag / 10).ToString();
}
private void MaiForm_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(0); //强制关闭所有线程
Application.Exit();
}
private void checkContinueSend_CheckedChanged(object sender, EventArgs e)
{
isContinueSend = checkContinueSend.Checked;
if(isContinueSend)
{
btnSend.Enabled = false;
}
else
{
btnSend.Enabled = true;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtUdpRecv.Text = "";
udpRecv = "";
nRecvLengthSum = 0;
}
}
}
3、下载连接
https://download.csdn.net/download/panjinliang066333/89879538