Esempio n. 1
0
        /*
         * Open the sockets and make the connection
         */
        public override int Open()
        {
            int Result = 0;

            RemoteIP   = Server;
            RemotePort = PortOut.ToString();
            LocalPort  = PortIn.ToString();

            try
            {
                sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                LocalIP = GetLocalIP();
            }
            catch (Exception ex)
            {
                AddError(Result = 801, ex.Message, GetType().Name + ".SocketClient", "Failed to set socket");
            }

            try
            {
                // binding socket
                epLocal = new IPEndPoint(IPAddress.Parse(LocalIP), Convert.ToInt32(LocalPort));
                sck.Bind(epLocal);

                // connect to remote IP and port
                epRemote = new IPEndPoint(IPAddress.Parse(RemoteIP), Convert.ToInt32(RemotePort));
                sck.Connect(epRemote);

                Connected = true;

                if (DebugLevel > 5)
                {
                    WriteDebug("Waiting...");
                }

                // starts to listen to an specific port
                buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
            }
            catch (Exception ex)
            {
                AddError(Result = 802, ex.Message, GetType().Name + ".SocketClient", "Failed to open socket");
            }

            return(Result);
        }