[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 786
  • Last Modified:

.Net.Sockets Async

Sending & receiving data with sockets.
I am able to send & receive data  from my device with Sockets IP & port.
On receiving data back from the device I want to send another command to the device.
I am unable to do that after I receive data from the device. Should I close the socket & reopen it or what should I do to send data again.
Herewith is my code.......
byte[] buffer = null;       
        public override void SendData(string data)
        {
            try
            {
                if (!mSocket.Connected)
                {
                    Connect();
                }
 
                buffer = Encoding.UTF8.GetBytes(data);//Convert to UTF8 to communicate with Unibadger
               
               
                mSocket.BeginSend(buffer, 0, bufferNew.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                //mSocket.Send(bufferNew);
                mSocket.Listen(5000);//Keep port open to listen for incoming data
                mSocket.BeginAccept(OnDataReceived, mSocket);
 
            }
            catch (SocketException se)
            {
                string err = se.ToString();
            }
          
 
        }
 
 
 byte[] m_DataBuffer;
        public void WaitForData()
        {
            m_DataBuffer = new byte[256];
            if (pfnCallBack == null)
            {
                pfnCallBack = new AsyncCallback(OnDataReceived);
            }
            // now start to listen for any data... 
            m_asynResult = mSocket.BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);           
           
           
        }
        public void OnDataReceived(IAsyncResult asyn)
        {
            //end receive...
            int iRx = 0;
            try
            {
                if (m_asynResult.AsyncWaitHandle.WaitOne())
                {
                    iRx = mSocket.EndReceive(asyn);
                    char[] chars = new char[iRx + 1];
                    char[] charsNew = new char[iRx];//Decoder adds 65533 to starting char. Used to remove the first value 655355
                    Decoder d = Encoding.UTF8.GetDecoder();
                    int charLen = d.GetChars(m_DataBuffer, 0, iRx, chars, 0);
                    chars[0] = ((char)1);
                    Array.Copy(chars, 1, charsNew, 0, iRx);
                    String szData = new String(chars);
 
                    foreach (var item in chars)//Add info to Mylist to be returned
                    {
                        myList.Add(Convert.ToByte(item));
                    }
                    if (szData.Length == 0)//If no data recieved check again for data
                    {
                        WaitForData();//Method to check for data
 
                    }
                    mSocket.Disconnect(true);
                }
 
            }
            catch (SocketException ex)
            {
                string m = ex.ToString();
            }
                     
            
        }

Open in new window

0
u2envy1
Asked:
u2envy1
  • 2
1 Solution
 
u2envy1Author Commented:
On sending the second command im getting error 10057
SocketErrorCode = NotConnected
base {System.ComponentModel.Win32Exception} = {"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"}
0
 
u2envy1Author Commented:
I ended the Connection. Socket.Close() then reopened it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now