none
endinvoke for asynchronous begininvoke in c#.net RRS feed

  • Question

  • respected madam/sir,

                                Im new for networing concept,im my network project, i have a server and a client process for message transfer, i have send and recieve the messages in local network but i can't save the message to the database as im not in depth with delegates and invoke methods.

      m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 5656);
                     m_socListener.Bind(ipLocal);
                     m_socListener.Listen(4);
                      m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);

    server code:

    public void OnClientConnect(IAsyncResult asyn)
            {
                try
                {
                    m_socWorker = m_socListener.EndAccept(asyn);
                   
                    WaitForData(m_socWorker);
                }
                catch (ObjectDisposedException)
                {
                    System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }

            }
            public class CSocketPacket
            {
                public System.Net.Sockets.Socket thisSocket;
                public byte[] dataBuffer = new byte[0];
            }

            public void WaitForData(System.Net.Sockets.Socket soc)
            {
                try
                {
                    if (pfnWorkerCallBack == null)
                    {
                        pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                        
                    }
                  CSocketPacket theSocPkt = new CSocketPacket();
                    theSocPkt.thisSocket = soc;
                    // now start to listen for any data...
                    soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);

                   
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }

            }

            public void OnDataReceived(IAsyncResult asyn)
            {
                
                try
                {
                  //recievesmstext.Text = "";
                    CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
                    //end receive...
                    int iRx = 0;
                    iRx = theSockId.thisSocket.EndReceive(asyn);
                    char[] chars = new char[iRx + 1];
                    System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                    int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                    System.String szData = new System.String(chars);
                  

                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        recievesmstext123.Text = recievesmstext123.Text + szData;
                        s = recievesmstext123.Text;  
                    }));

                         
                    
                    WaitForData(m_socWorker);

                   
                }
                catch (ObjectDisposedException)
                {
                    System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
            }

    In the above server code i can't save to xml as  asynchronous thread .i don't know how to write the endinvoke method for this project.plz anyone help me....

    Friday, February 10, 2012 8:48 AM

Answers

  • kavitha,

    Sorry but you have posted to a forum that deals exclusively with questions/issues about Microsoft Project, a planning and scheduling application. I suggest you find a forum that deals with c#.net.

    John

    Friday, February 10, 2012 3:35 PM

All replies

  • kavitha,

    Sorry but you have posted to a forum that deals exclusively with questions/issues about Microsoft Project, a planning and scheduling application. I suggest you find a forum that deals with c#.net.

    John

    Friday, February 10, 2012 3:35 PM
  • k sir, thank u for the reply.........
    Saturday, February 11, 2012 12:33 PM