using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace QuaLiX.TMS.App.Hardwares.RFIDReader { //public class TCPServer //{ // public AsyncCallback pfnWorkerCallBack; // private Socket m_mainSocket; // private System.Collections.ArrayList m_workerSocketList = // ArrayList.Synchronized(new System.Collections.ArrayList()); // public event EventHandler NewDataRecieved; // private int m_clientCount = 0; // public bool StartListening() // { // bool _status = false; // try // { // int port = System.Convert.ToInt32(PCParameter.ExitRFIDServiceIP) + PCParameter.LaneID; // // Create the listening socket... // m_mainSocket = new Socket(AddressFamily.InterNetwork, // SocketType.Stream, // ProtocolType.Tcp); // IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port); // // Bind to local IP Address... // m_mainSocket.Bind(ipLocal); // // Start listening... // m_mainSocket.Listen(100); // // Create the call back for any client connections... // m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); // _status = true; // } // catch (Exception ex) // { // _status = false; // //DBHelper.WriteLog("StartListening() Error= " + ex.ToString(), false); // } // return _status; // } // public void OnClientConnect(IAsyncResult asyn) // { // try // { // Socket workerSocket = m_mainSocket.EndAccept(asyn); // Interlocked.Increment(ref m_clientCount); // // Add the workerSocket reference to our ArrayList // m_workerSocketList.Add(workerSocket); // WaitForData(workerSocket); // // Since the main Socket is now free, it can go back and wait for // // other clients who are attempting to connect // m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); // } // catch (ObjectDisposedException ex) // { // // DBHelper.WriteLog("OnClientConnect() Error= " + ex.ToString(), false); // } // catch (SocketException se) // { // // DBHelper.WriteLog("OnClientConnect() Error= " + se.Message, false); // } // } // public void WaitForData(System.Net.Sockets.Socket soc) // { // try // { // if (pfnWorkerCallBack == null) // { // // Specify the call back function which is to be // // invoked when there is any write activity by the // // connected client // pfnWorkerCallBack = new AsyncCallback(OnDataReceived); // } // SocketPacket theSocPkt = new SocketPacket(); // theSocPkt.m_currentSocket = soc; // soc.BeginReceive(theSocPkt.dataBuffer, 0, // theSocPkt.dataBuffer.Length, // SocketFlags.None, // pfnWorkerCallBack, // theSocPkt); // } // catch (SocketException se) // { // // DBHelper.WriteLog("WaitForData() Error= " + se.Message, false); // } // } // List readBuffer = new List(); // private bool sync = false; // public void OnDataReceived(IAsyncResult asyn) // { // String content = String.Empty; // try // { // SocketPacket socketData = (SocketPacket)asyn.AsyncState; // int iRx = 0; // iRx = socketData.m_currentSocket.EndReceive(asyn); // if (iRx > 0) // { // sync = true;//packet was valid // if (NewDataRecieved != null) // NewDataRecieved(this, new SerialDataEventArgs(socketData.dataBuffer)); // } // WaitForData(socketData.m_currentSocket); // } // catch (ObjectDisposedException ex) // { // // DBHelper.WriteLog("OnDataReceived() Error= " + ex.ToString(), false); // } // catch (SocketException se) // { // // DBHelper.WriteLog("OnDataReceived() Error= " + se.Message, false); // } // } // public void StopListening() // { // if (m_mainSocket != null) // { // m_mainSocket.Shutdown(SocketShutdown.Both); // m_mainSocket.Close(); // } // Socket workerSocket = null; // for (int i = 0; i < m_workerSocketList.Count; i++) // { // workerSocket = (Socket)m_workerSocketList[i]; // if (workerSocket != null) // { // workerSocket.Close(); // workerSocket = null; // } // } // } //} //public class SocketPacket //{ // public System.Net.Sockets.Socket m_currentSocket; // public const int BufferSize = 1024; // public byte[] dataBuffer = new byte[BufferSize]; // public StringBuilder sb = new StringBuilder(); //} //public class SerialDataEventArgs : EventArgs //{ // public SerialDataEventArgs(byte[] dataInByteArray) // { // Data = dataInByteArray; // } // /// // /// Byte array containing data from serial port // /// // public byte[] Data; //} }