C# UdpClient接收消息问题?
请看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Net.Sockets;
using System.Net;
using System.Threading;namespace SendUdpMsg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
getMsg();
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
} void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show("出现未预料到的错误:" + e.Exception.Message);
Application.Exit();
} IPEndPoint ipend;
UdpClient uc;
Thread get; private void sendMsg_Click(object sender, EventArgs e)//发送消息
{
if (ipend == null && uc == null)
{
ipend = new IPEndPoint(IPAddress.Parse(ipaddr.Text.Trim()), 5566);
uc = new UdpClient(5567);
}
byte[] sendData = System.Text.Encoding.UTF8.GetBytes(msg.Text);
uc.Send(sendData, sendData.Length, ipend);
if (!ipaddr.Text.Contains("255"))
{
msgList.AppendText(DateTime.Now.ToString() + "\r");//msgList为一textBox
msgList.AppendText("我说: " + msg.Text + "\r");
}
} private void getMsg()
{
get = new Thread(new ThreadStart(getMsgCode));
get.Start();
} private void getMsgCode()//接收消息
{
IPHostEntry iphost = new IPHostEntry();
iphost = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipend = new IPEndPoint(iphost.AddressList[0], 5566);
UdpClient uc = new UdpClient(ipend);
while (true)
{
byte[] recvData = uc.Receive(ref ipend);
string recvString = System.Text.Encoding.UTF8.GetString(recvData);
msgList.AppendText(DateTime.Now.ToString() + "\r");
msgList.AppendText(ipend.Address.ToString() + ": " + recvString + "\r");
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//e.Cancel = DialogResult.No == MessageBox.Show("是否关闭?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
}
}
}现在有两个问题:
1.别人给我(自己广播也一样,如172.17.47.255)发送消息时,不管是中文还是英文,我总是收到"Drco"消息?
2.退出时(直接点右上角的X),进程仍然存在,只能在任条管理器中手动结束它,是不是有什么没有关闭,或没有释放?还是主线程退出时,get线程没有退出,在FromClosed事件中加入,get.Abort()也不行?
请看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Net.Sockets;
using System.Net;
using System.Threading;namespace SendUdpMsg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
getMsg();
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
} void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show("出现未预料到的错误:" + e.Exception.Message);
Application.Exit();
} IPEndPoint ipend;
UdpClient uc;
Thread get; private void sendMsg_Click(object sender, EventArgs e)//发送消息
{
if (ipend == null && uc == null)
{
ipend = new IPEndPoint(IPAddress.Parse(ipaddr.Text.Trim()), 5566);
uc = new UdpClient(5567);
}
byte[] sendData = System.Text.Encoding.UTF8.GetBytes(msg.Text);
uc.Send(sendData, sendData.Length, ipend);
if (!ipaddr.Text.Contains("255"))
{
msgList.AppendText(DateTime.Now.ToString() + "\r");//msgList为一textBox
msgList.AppendText("我说: " + msg.Text + "\r");
}
} private void getMsg()
{
get = new Thread(new ThreadStart(getMsgCode));
get.Start();
} private void getMsgCode()//接收消息
{
IPHostEntry iphost = new IPHostEntry();
iphost = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipend = new IPEndPoint(iphost.AddressList[0], 5566);
UdpClient uc = new UdpClient(ipend);
while (true)
{
byte[] recvData = uc.Receive(ref ipend);
string recvString = System.Text.Encoding.UTF8.GetString(recvData);
msgList.AppendText(DateTime.Now.ToString() + "\r");
msgList.AppendText(ipend.Address.ToString() + ": " + recvString + "\r");
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//e.Cancel = DialogResult.No == MessageBox.Show("是否关闭?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
}
}
}现在有两个问题:
1.别人给我(自己广播也一样,如172.17.47.255)发送消息时,不管是中文还是英文,我总是收到"Drco"消息?
2.退出时(直接点右上角的X),进程仍然存在,只能在任条管理器中手动结束它,是不是有什么没有关闭,或没有释放?还是主线程退出时,get线程没有退出,在FromClosed事件中加入,get.Abort()也不行?
2、
get = new Thread(new ThreadStart(getMsgCode));
get.Start();
改成:
get = new Thread(new ThreadStart(getMsgCode));
get.IsBackground=true;
get.Start();把线程设置后台线程就可以了!