Ejemplo n.º 1
0
        /// <summary>
        /// Sends data to the remote host
        /// </summary>
        /// <param name="data">the data to send</param>
        public void SendData(string data)
        {
            Debug.WriteLine(string.Format("Calling: {0}", MethodBase.GetCurrentMethod().Name));
            Debug.WriteLine("Sending: " + data);

            const string Delimiter = "<EOF>";

            //add the protocol <EOF> to delimit the stream
            data += Delimiter;

            SocketState state = new SocketState(0, _Socket);
            byte[] binarydata = UTF8Encoding.UTF8.GetBytes(data);

            //Use poll to see if writing is possible
            if (_Socket.Poll(Args.SendTimeOut, Sockets.SelectMode.SelectWrite))
            {
                _Socket.BeginSend(binarydata, 0, data.Length, Sockets.SocketFlags.None, new AsyncCallback(SendCallback), state);
                _SendDone.WaitOne(Args.SendTimeOut, false);
                _SendDone.Reset();

                if (state.ErrorNumber != 0)
                {
                    string message = string.Format("Socket Exception: error number {0}.", state.ErrorNumber);
                    throw new ApplicationException(message);
                }
            }
            else
            {
                string message = string.Format("Socked Adapter: Send timeout", Args.SendTimeOut);
                throw new ApplicationException(message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Connects to the remote host
        /// </summary>
        public void Connect()
        {
            Debug.WriteLine(string.Format("Calling: {0}", MethodBase.GetCurrentMethod().Name));
            try
            {
                SetupSocket();
                SocketState state = new SocketState(0, this._Socket);
                this._Socket.BeginConnect(Args.EndPoint, new AsyncCallback(ConnectCallback), state);
                this._ConnectionDone.WaitOne(Args.RecieveTimeOut, false);
                this._ConnectionDone.Reset();

                if (!this.IsConnected)
                {
                    string message = "Socket Exception: connection refused.";
                    throw new ApplicationException(message);
                }

                if (state.ErrorNumber != 0)
                {
                    string message = String.Format("Socket Exception: error number {0}.", state.ErrorNumber);
                    throw new ApplicationException(message);
                }
            }
            catch (Sockets.SocketException e)
            {
                if (e.NativeErrorCode != WSAEWOULDBLOCK)
                    throw;
                else
                {
                    //WSAEWOULDBLOCK is not an error. Just poll until connected or timeout occurs
                    if (this._Socket.Poll(Args.RecieveTimeOut, Sockets.SelectMode.SelectWrite) == false)
                        throw new ApplicationException("Socket Exception: timeout");
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Receives data from the remote host
        /// </summary>
        /// <param name="maxBufSize">the maximum size of the data to receive</param>
        /// <returns></returns>
        public string ReceiveData()
        {
            Debug.WriteLine(string.Format("Calling: {0}", MethodBase.GetCurrentMethod().Name));
            SocketState state = new SocketState(this._Args.MaxBufferSize, _Socket);
            if (_Socket.Poll(Args.RecieveTimeOut, Sockets.SelectMode.SelectRead))
            {
                _Socket.BeginReceive(state.WorkBuffer, 0, state.BufferSize, Sockets.SocketFlags.None, new AsyncCallback(RecieveMessageCallback), state);
                if (!this._RecieveDone.WaitOne(Args.RecieveTimeOut, false))
                    throw new ApplicationException("Socket Exception: timeout");

                if (state.ErrorNumber != 0)
                    throw new ApplicationException(String.Format("Socket Exception: error number {0}.", state.ErrorNumber));

                this._RecieveDone.Reset();

                if (state.ErrorNumber != 0)
                    throw new ApplicationException(String.Format("Socket Exception: error number {0}.", state.ErrorNumber));

                string reply = UTF8Encoding.UTF8.GetString(state.WorkBuffer, 0, state.ActualSize);
                //Remove the protocol <EOF>
                string ack = ((char)ushort.Parse("6", System.Globalization.NumberStyles.HexNumber)).ToString();
                if (reply.StartsWith(ack))
                    reply = reply.Replace(ack, "");
                if (reply.IndexOf("<EOF>") > 0)
                    reply = reply.Replace("<EOF>", "");
                if (reply.IndexOf("\r\n") > 0)
                    reply = reply.Replace("\r\n", "");
                return reply;

            }
            else
            {
                Trace.WriteLine("Receive timed out on poll function. TimeOut = " + Args.RecieveTimeOut + " microseconds.");
                throw new ApplicationException("Socket Exception: timeout");
            }
        }