public static void OnDataReceived(IAsyncResult asyn) { SocketPacket socketData = (SocketPacket)asyn.AsyncState; try { int numbytes = 0; numbytes = socketData.m_currentSocket.EndReceive(asyn); char[] chars = new char[numbytes]; //System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); string recievedData = new String(chars); m_currentmessage += recievedData; if (recievedData.Contains("\n") && OutputClientChanged != null) { OutputClientChanged(null, m_currentmessage); //(spacket.Socket, node); m_currentmessage = ""; } WaitForData(socketData.m_currentSocket, socketData.m_clientNumber); System.Net.Sockets.Socket workerSocket2 = (System.Net.Sockets.Socket)socketData.m_currentSocket; SendDataClient(workerSocket2); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed \n"); } catch (SocketException se) { if (se.ErrorCode == 10054) // Error code for Connection reset by peer { string msg = "Client " + socketData.m_clientNumber + " Disconnected" + "\n"; if (OutputClientChanged != null) { OutputClientChanged(null, msg); } m_workerSocketList[socketData.m_clientNumber - 1] = null; } else { if (OutputClientChanged != null) { OutputClientChanged(null, se.Message); } } } }
/// <summary> /// Waits for data to arrive from the socket connection. /// </summary> /// <param name="s">The socket to wait on.</param> private void WaitForData(Socket s) { try { SocketPacket spacket = new SocketPacket(); spacket.Socket = s; // // Begin listening for data. // s.BeginReceive(spacket.DataBuffer, 0, spacket.DataBuffer.Length, SocketFlags.None, m_datareciever, spacket); } catch (SocketException e) { } }
public static void WaitForData(System.Net.Sockets.Socket soc, int clientNumber) { try { if (pfnWorkerCallBack == null) { pfnWorkerCallBack = new AsyncCallback(OnDataReceived); } SocketPacket theSocPkt = new SocketPacket(soc, clientNumber); soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt); } catch (SocketException se) { Console.WriteLine(se.Message); } }
/// <summary> /// Fired when data is recieved from the socket. /// </summary> /// <param name="asyn"></param> protected void OnDataReceived(IAsyncResult asyn) { try { // // Get the socket packet (as set by BeginRecieve's state) // SocketPacket spacket = (SocketPacket)asyn.AsyncState; // // Decode the text. // int numbytes = 0; numbytes = spacket.Socket.EndReceive(asyn); char[] chars = new char[numbytes]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(spacket.DataBuffer, 0, numbytes, chars, 0); string recievedData = new String(chars); m_currentmessage += recievedData; // // Dispatch event. // //if (recievedData == "\0" && DataRecieved != null) if (recievedData.Contains("\n") && DataRecieved != null) { XmlNode node; try { /*XmlDocument doc = new XmlDocument(); * doc.LoadXml(m_currentmessage); * node = doc.FirstChild;*/ } catch { m_currentmessage = ""; WaitForData(m_socketclient); return; } DataRecieved(m_currentmessage); //(spacket.Socket, node); m_currentmessage = ""; } // // Wait for data again. // WaitForData(m_socketclient); } catch (ObjectDisposedException) { KillSocket(); if (ClientDisconnect != null) { ClientDisconnect(this, EventArgs.Empty); } } catch (SocketException e) { System.Diagnostics.Debugger.Log(0, "1", "\n" + e.ToString() + "\n"); } }
public static void WaitForData(System.Net.Sockets.Socket soc, int clientNumber) { try { if ( pfnWorkerCallBack == null ) { pfnWorkerCallBack = new AsyncCallback (OnDataReceived); } SocketPacket theSocPkt = new SocketPacket (soc, clientNumber); soc.BeginReceive (theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt); } catch(SocketException se) { Console.WriteLine(se.Message ); } }