none
TCP Client的問題 RRS feed

  • 問題

  • 各位好

    以下是我參考網上資料做的TCP Client軟體,目前的問題是當資料進來的時候不會完整,我的TCP Server是別人寫的,市售品,應該不會有問題,其實我是希望能夠一直非同步方式收資料,收多、送少,不知哪位可以小弟一點意見,或是有哪位可以提供我C#的sample Code也是感謝不盡。

     public partial class Form1 : Form
        {
            TcpClient tcpc = new TcpClient(AddressFamily.InterNetwork);
            private AsyncCallback callbackProc;
            private Socket s;
            private IPEndPoint iep;
            Byte[] m_byBuff = new Byte[32767];
            string ans = string.Empty;


            public Form1()
            {
                InitializeComponent();
                CheckForIllegalCrossThreadCalls = false;
            }

            private void ConnectTo()
            {
                string address = "11.22.33.44";
                int port = 5001;

                //IPHostEntry IPHost = Dns.Resolve(address);
                IPHostEntry IPHost = Dns.GetHostEntry(address);
                string[] aliases = IPHost.Aliases;
                IPAddress[] addr = IPHost.AddressList;

                try
                {
                    // Create New Socket
                    s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    // Create New EndPoint
                    iep = new IPEndPoint(addr[0], port);
                    // This is a non blocking IO
                    s.Blocking = false;
                    // Assign Callback function to read from Asyncronous Socket
                    callbackProc = new AsyncCallback(ConnectCallback);
                    // Begin Asyncronous Connection
                    s.BeginConnect(iep, callbackProc, s);
                    s.ReceiveTimeout = 30;

                }
                catch (Exception eeeee)
                {
                    MessageBox.Show(eeeee.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    Application.Exit();
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                ConnectTo();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                DispatchMessage("this is a book");
            }

            void DispatchMessage(string strText)
            {
                try
                {
                    Byte[] smk = new Byte[strText.Length];
                    for (int i = 0; i < strText.Length; i++)
                    {
                        Byte ss = Convert.ToByte(strTextIdea);
                        smkIdea = ss;
                    }

                    IAsyncResult ar2 = s.BeginSend(smk, 0, smk.Length, SocketFlags.None, callbackProc, s);
                    s.EndSend(ar2);
                }
                catch (Exception ers)
                {
                    MessageBox.Show("ERROR IN RESPOND OPTIONS");
                }
            }

            private void button3_Click(object sender, EventArgs e)
            {
                MessageBox.Show(ans);
            }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                tcpc.Close();
            }

            public void ConnectCallback(IAsyncResult ar)
            {
                try
                {
                    // Get The connection socket from the callback
                    Socket sock1 = (Socket)ar.AsyncState;
                    if (sock1.Connected)
                    {
                        // Define a new Callback to read the data
                        AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
                        // Begin reading data asyncronously
                        sock1.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, sock1);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Setup Recieve callbackProc failed!");
                }
            }


            public void OnRecievedData(IAsyncResult ar)
            {
                // Get The connection socket from the callback
                Socket sock = (Socket)ar.AsyncState;
                // Get The data , if any
                int nBytesRec = sock.EndReceive(ar);
                if (nBytesRec > 0)
                {
                    string sRecieved = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
                    ans = ans + sRecieved;
                    IAsyncResult ar2 = s.BeginReceive(m_byBuff, 0, m_byBuff.Length, 0, ConnectCallback, s);
                }
              }
        }

    2008年8月20日 上午 10:04

解答