c#网络编程UDP例子 5
求一个例子,,,要有一个连接button,一个发送button,2个Textbox的IP地址和2个端口,2个richbox.各位谁会给个例子哇...
求一个例子,,,要有一个连接button,一个发送button,2个Textbox的IP地址和2个端口,2个richbox.
各位谁会给个例子哇 展开
各位谁会给个例子哇 展开
1个回答
展开全部
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace UDPClient
{
public partial class frmUdp : Form
{
private UdpClient sendUdpClient;
private UdpClient receiveUpdClient;
public frmUdp()
{
InitializeComponent();
IPAddress[] ips = Dns.GetHostAddresses("");
tbxlocalip.Text = ips[2].ToString();
int port = 51883;
tbxlocalPort.Text = port.ToString();
tbxSendtoIp.Text = ips[2].ToString();
tbxSendtoport.Text = port.ToString();
}
// 接受消息
private void btnReceive_Click(object sender, EventArgs e)
{
// 创建接收套接字
IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
receiveUpdClient = new UdpClient(localIpEndPoint);
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start();
}
// 接收消息方法
private void ReceiveMessage()
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
// 关闭receiveUdpClient时此时会产生异常
byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);
string message = Encoding.Unicode.GetString(receiveBytes);
// 显示消息内容
ShowMessageforView(lstbxMessageView, string.Format("{0}[{1}]", remoteIpEndPoint, message));
}
catch
{
break;
}
}
}
// 利用委托回调机制实现界面上消息内容显示
delegate void ShowMessageforViewCallBack(ListBox listbox, string text);
private void ShowMessageforView(ListBox listbox, string text)
{
if (listbox.InvokeRequired)
{
ShowMessageforViewCallBack showMessageforViewCallback = ShowMessageforView;
listbox.Invoke(showMessageforViewCallback, new object[] { listbox, text });
}
else
{
lstbxMessageView.Items.Add(text);
lstbxMessageView.SelectedIndex = lstbxMessageView.Items.Count - 1;
lstbxMessageView.ClearSelected();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (tbxMessageSend.Text == string.Empty)
{
MessageBox.Show("发送内容不能为空","提示");
return;
}
// 选择发送模式
if (chkbxAnonymous.Checked == true)
{
// 匿名模式(套接字绑定的端口由系统随机分配)
sendUdpClient = new UdpClient(0);
}
else
{
// 实名模式(套接字绑定到本地指定的端口)
IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
sendUdpClient = new UdpClient(localIpEndPoint);
}
Thread sendThread = new Thread(SendMessage);
sendThread.Start(tbxMessageSend.Text);
}
// 发送消息方法
private void SendMessage(object obj)
{
string message = (string)obj;
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
IPAddress remoteIp = IPAddress.Parse(tbxSendtoIp.Text);
IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteIp, int.Parse(tbxSendtoport.Text));
sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpEndPoint);
sendUdpClient.Close();
// 清空发送消息框
ResetMessageText(tbxMessageSend);
}
// 采用了回调机制
// 使用委托实现跨线程界面的操作方式
delegate void ResetMessageCallback(TextBox textbox);
private void ResetMessageText(TextBox textbox)
{
// Control.InvokeRequired属性代表
// 如果空间的处理与调用线程在不同线程上创建的,则为true,否则为false
if (textbox.InvokeRequired)
{
ResetMessageCallback resetMessagecallback = ResetMessageText;
textbox.Invoke(resetMessagecallback, new object[] { textbox });
}
else
{
textbox.Clear();
textbox.Focus();
}
}
// 停止接收
private void btnStop_Click(object sender, EventArgs e)
{
receiveUpdClient.Close();
}
// 清空接受消息框
private void btnClear_Click(object sender, EventArgs e)
{
this.lstbxMessageView.Items.Clear();
}
}
}
追问
QQ:多少呀,加个好友我想请教TCP/IP和UDP相关内容。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询