主题:求C#的UDP通信的源码程序
很急阿 谁有能告诉我阿?
我的Email:ling19841215@163.com
下面是我写的一个程序的一部分,帮我看看哪里不对
private IPAddress hostIP,remoteIP;
private IPEndPoint hostPoint,remotePoint;
private UdpClient uc;
int port;
private void btnSend_Click(object sender, System.EventArgs e)
{
if(uc==null)
{
hostIP=IPAddress.Parse(txtHostIP.Text);
remoteIP=IPAddress.Parse(txtRemoteIP.Text);
port=Convert.ToInt32(txtPort.Text);
hostPoint=new IPEndPoint(hostIP,port);
remotePoint=new IPEndPoint(remoteIP,port);
uc=new UdpClient(hostPoint);
}
string strSend =rtbSend.Text;
byte[] byteSend = Encoding.BigEndianUnicode.GetBytes(strSend);
uc.Send(byteSend,byteSend.Length,remotePoint);
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if(uc==null)
{
hostIP=IPAddress.Parse(txtHostIP.Text);
remoteIP=IPAddress.Parse(txtRemoteIP.Text);
port=Convert.ToInt32(txtPort.Text);
hostPoint=new IPEndPoint(hostIP,port);
remotePoint=new IPEndPoint(remoteIP,port);
uc=new UdpClient(hostPoint);
}
Thread myThread=new Thread(new ThreadStart(run));
myThread.Start();
}
private void run()
{
while(true)
{
byte[] byteReceive=uc.Receive(ref remotePoint);
string strReceive=Encoding.BigEndianUnicode.GetString(byteReceive);
rtbReceive.AppendText(strReceive+"\r\n");
}
}
我的Email:ling19841215@163.com
下面是我写的一个程序的一部分,帮我看看哪里不对
private IPAddress hostIP,remoteIP;
private IPEndPoint hostPoint,remotePoint;
private UdpClient uc;
int port;
private void btnSend_Click(object sender, System.EventArgs e)
{
if(uc==null)
{
hostIP=IPAddress.Parse(txtHostIP.Text);
remoteIP=IPAddress.Parse(txtRemoteIP.Text);
port=Convert.ToInt32(txtPort.Text);
hostPoint=new IPEndPoint(hostIP,port);
remotePoint=new IPEndPoint(remoteIP,port);
uc=new UdpClient(hostPoint);
}
string strSend =rtbSend.Text;
byte[] byteSend = Encoding.BigEndianUnicode.GetBytes(strSend);
uc.Send(byteSend,byteSend.Length,remotePoint);
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if(uc==null)
{
hostIP=IPAddress.Parse(txtHostIP.Text);
remoteIP=IPAddress.Parse(txtRemoteIP.Text);
port=Convert.ToInt32(txtPort.Text);
hostPoint=new IPEndPoint(hostIP,port);
remotePoint=new IPEndPoint(remoteIP,port);
uc=new UdpClient(hostPoint);
}
Thread myThread=new Thread(new ThreadStart(run));
myThread.Start();
}
private void run()
{
while(true)
{
byte[] byteReceive=uc.Receive(ref remotePoint);
string strReceive=Encoding.BigEndianUnicode.GetString(byteReceive);
rtbReceive.AppendText(strReceive+"\r\n");
}
}