Mono.MicroThreads.MicroSocket.Connect C# (CSharp) Method

Connect() public method

public Connect ( IPAddress address, int port ) : bool
address System.Net.IPAddress
port int
return bool
        public bool Connect(IPAddress address, int port)
        {
            Print("Begin connecting to {0}:{1}", address, port);

            try
            {
                m_socket.Connect(new IPEndPoint(address, port));
            }
            catch (SocketException)
            {
                // ignore blocking connect exception. shouldn't there be some other way to do this...
                //Console.WriteLine("exc cont");
            }

            m_writeCS.Enter();
            m_writingThread = MicroThread.CurrentThread;
            m_writingThread.Wait();
            m_writingThread = null;
            m_writeCS.Exit();

            //Console.WriteLine("STATE {0}", m_waitState);

            if ((m_selectStatus & MicroSocketSelectStatus.Error) != 0)
            {
                Console.WriteLine("Connect failed");
                return false;
            }
            else if ((m_selectStatus & MicroSocketSelectStatus.Write) != 0)
            {
                //Console.WriteLine("Connected!");
                return true;
            }
            else
            {
                throw new Exception("illegal state");
            }
        }

Usage Example

コード例 #1
0
ファイル: Client.cs プロジェクト: mono/mono-microthreads
        public void Run()
        {
            m_socket = new MicroSocket();

            //Console.Write("-");

            s_connectingSockets++;

            IPAddress baal = IPAddress.Parse("127.0.0.1");
            IPAddress torturer = IPAddress.Parse("192.168.1.1");

            if (m_socket.Connect(baal, 12345) == false)
            {
                Console.WriteLine("Connection failed");
                return;
            }

            s_connectingSockets--;
            s_connectedSockets++;

            /*
            while (s_connectedSockets < 500)
                MicroThread.CurrentThread.Sleep(1000);
            */

            //Console.Write(".");

            long sentBlocks = 0;

            byte[] buf = new byte[m_blockSize];
            while (true)
            {
                m_socket.Send(buf, buf.Length);

                sentBlocks++;
                s_totalBlocksSent++;

                if (sentBlocks >= m_blocksToSend)
                    break;
            }

            //Console.Write("x");
            s_connectedSockets--;

            m_socket.Shutdown();
            m_socket.Close();
        }