1 Replies - 823 Views - Last Post: 25 January 2013 - 02:34 PM Rate Topic: -----

#1 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Problem with multiple (array) socket receive

Posted 22 January 2013 - 09:19 AM

Hi!

I have to make a connection (client application) to 8 different socket. And then receive data from all socket. What I'd tried to do is make an array of socket and the connect them. This is working fine. Afterwards I trie to asynchronically receive data from all socket using methods bellow.
#region Global variables
        Socket[] m_socClient = new Socket[8];
        IAsyncResult[] m_asynResult = new IAsyncResult[8];
        AsyncCallback[] pfnCallBack = new AsyncCallback[8];
        byte[][] m_DataBuffer = new byte[8][];
        int timeOut = 500;
        string IPstring = "10.0.0.15";
        int[] PortNo = new int[8];
        string bar;
#endregion
#region Constructor
        public BAR()
        {
            IPAddress ip = IPAddress.Parse(IPstring); 
            for (int i = 0; i < 8; i++)
            {
                m_socClient[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                
                if (m_socClient[i].Connected == false)
                {
                    PortNo[i] = (2001 + (i * 10));
                    IPEndPoint ipEnd = new IPEndPoint(ip, PortNo[i]);
                    m_socClient[i].Connect(ipEnd);
                    //Čakaj dokler povezava ni vzpostavljena
                    while (m_socClient[i].Connected != true) ;
                }
            }
            WaitForData();
        }
#endregion

#region Ethernet received
        private void WaitForData()
        {                        
            for (int i = 0; i < 8; i++)
            {         
                if(pfnCallBack[i] == null)
                {
                    pfnCallBack[i] = new AsyncCallback(OnDataReceived);
                }                
                m_DataBuffer[i] = null;
                m_DataBuffer[i] = new byte[1024];
                m_asynResult[i] = m_socClient[i].BeginReceive(m_DataBuffer[i], 0, 1024, SocketFlags.None, pfnCallBack[i], null);
            }
        }

        private void OnDataReceived(IAsyncResult asyn)
        {
            //end receive...  
            for (int i = 0; i < 8; i++)
            {
                if (m_DataBuffer[i][0] != 0)
                {
                    m_socClient[i].EndReceive(asyn);
                    bar = ByteArrayToStr(m_DataBuffer[i]);
                }
            }            
            WaitForData();
        }
        #endregion


The method works fine if I receive data only on one socket (it doesn't meter witch), but after I receive some data on any other socket, method OnDataReceived won't fire. What am I doing wrong? Or what would be a right approach?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with multiple (array) socket receive

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Problem with multiple (array) socket receive

Posted 25 January 2013 - 02:34 PM

These chat program tutorials are socket based and allow for multiple connections. You should be able to pull out the basic concept of how they run multiple sockets concurrently and apply it to your app.

Q: ...make a chat or client-server application?
A:Client/server chat tutorial
Peer-to-peer chat
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1