PDA上一个UDP通讯客户端

放了个TEXTBOX输入,TEXTBOX多行来记录日志(其实可以简单的用LISTBOX)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace UDPClient
{
    
public partial class FrmMain : Form
    
{
        
private FrmSetup _setup;  //设置远程服务器IP
        private UdpClient _udpSend;
        
private UdpClient _udpReceive;
        
private Thread _receive;
        
public delegate void UpdateMsg(String Msg);
        
public delegate void SendMsg(String Msg);
        
public UpdateMsg myDelegate;
        
public SendMsg mySendMsg;

        
public void UpdateMsgMethod(String Msg)
        
{

            
if (Msg.Length == 0)
            
{

                Msg 
= "\"空\"\r\n";
                
//tBResult.AppendText("\"空\"\r\n");


            }

                tBResult.Text 
= tBResult.Text + Msg;
                tBResult.SelectionStart 
= tBResult.TextLength - Msg.Length;
            
            
            tBResult.SelectionLength 
= 0;
            tBResult.ScrollToCaret();   
            
        }


        
private void Connect(string ServerIP, Int32 ServerPort)
        
{
            
try
            
{
                _udpSend.Connect(ServerIP, ServerPort);
            }

            
catch (Exception ex)
            
{
                UpdateMsgMethod( 
"异常:" + ex.Message.ToString() + "\r\n");
            }

        }



        
private void Send(string Msg)
        
{
            
byte[] sendBytes = Encoding.ASCII.GetBytes(Msg);

            
try
            
{


                _udpSend.Send(sendBytes, sendBytes.Length);
            }

            
catch (Exception ex)
            
{

                UpdateMsgMethod(
"异常:" + ex.Message.ToString() + "\r\n");
            }


        }


        
private void ThreadPro()
        
{



            
try
            
{
                IPEndPoint remotePoint 
= new IPEndPoint(IPAddress.Any, 0);
                Byte[] receiveBytes;
                
while (true)
                
{
                    receiveBytes 
= _udpReceive.Receive(ref remotePoint);

                    
string returnData = Encoding.ASCII.GetString(receiveBytes, 0, receiveBytes.Length);

                    
//tBResult.Text = tBResult.Text + "收到:\"" + returnData + "\"\r\n";
                    if (returnData == "<RETURNSTATE/>")//服务器获取状态的特色命令
                    {
                        
this.Invoke(this.mySendMsg, new object[] "<RETURNOK/>" });//获取状态
                    }

                    
else
                    
{


                        returnData 
= "收到:\"" + returnData + "\"\r\n";
                        
this.Invoke(this.myDelegate, new object[] { returnData });
                    }

                }

            }

            
catch (Exception ex)
            
{

                
//tBResult.Text = tBResult.Text + ex.Message.ToString() + "\r\n";
                this.Invoke(this.myDelegate, new object[] {"异常:"+ ex.Message.ToString() + "\r\n" });
            }



        }


        
public FrmMain()
        
{
            InitializeComponent();
            _setup 
= new FrmSetup();
            _udpSend 
= new UdpClient();
            _udpReceive 
= new UdpClient(10120);
            _receive 
= new Thread(new ThreadStart(ThreadPro));
            myDelegate 
= new UpdateMsg(UpdateMsgMethod);
            mySendMsg 
= new SendMsg(Send);
        }


        
private void menuItem3_Click(object sender, EventArgs e)
        
{
            Close();
        }


        
private void menuItem2_Click(object sender, EventArgs e)
        
{
            _setup.ShowDialog();
            Close();
        }


        
private void FrmMain_Load(object sender, EventArgs e)
        
{
            
if (File.Exists("setup.xml"))
            
{
                
//MessageBox.Show("setup.xml");
                XmlReader _rdoc = XmlReader.Create("setup.xml");
                
if (!_rdoc.EOF)
                
{
                    
//MessageBox.Show(_rdoc.ReadElementString());
                    _setup.ServerIP = _rdoc.ReadElementString();  //服务器IP
                    
//MessageBox.Show(_setup.ServerIP);
                    lbServer.Text = "Server:"+ _setup.ServerIP;
                    Connect(_setup.ServerIP, 
4660);
                    _receive.Start();
                    tBMsg.SelectAll();
                    tBMsg.Focus();

                }

                _rdoc.Close();
            }

            
else
            
{
                _setup.ServerIP 
= "";
                _setup.ShowDialog();
                Close();
            }

        }


        
private void tBMsg_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if (e.KeyChar == (char)Keys.Enter)
            
{
                
//MessageBox.Show(tBMsg.Text);
                Send(tBMsg.Text);
                tBMsg.SelectAll();
                tBMsg.Focus();
            }

        }


        
private void FrmMain_Closing(object sender, CancelEventArgs e)
        
{
            _receive.Abort();
//必须关闭,否则端口会一直被占用.在虚拟机上看不出的
            _udpReceive.Close();
            _udpSend.Close();
        }


 
 


    }

}

posted @ 2008-07-02 15:16  Enli  阅读(456)  评论(0编辑  收藏  举报