// this is a blocking call and it terminates when an incoming connection is
        // initiated -or- when an exception occurs
        // returns true if a connection is initiated otherwise returns false
        public bool WaitForIncomingConnection()
        {
            try
            {
                if (isDisposed)
                {
                    return(false);
                }

                server.Start();
                socket = server.AcceptSocket();
                socket.ReceiveBufferSize = receiveBufferSize;

                if (sender == null || receiver == null)
                {
                    sender   = new TcpCommandSender(socket, converter);
                    receiver = new TcpCommandReceiver(socket, converter);
                }
                else
                {
                    sender.Connection   = socket;
                    receiver.Connection = socket;
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemple #2
0
        public bool Connect(string serverIpAddress, int port)
        {
            try
            {
                client = new TcpClient();
                client.SendBufferSize    = sendBufferSize;
                client.ReceiveBufferSize = receiveBufferSize;
                client.NoDelay           = true;
                socket = client.Client;
                client.Connect(serverIpAddress, port);

                if (receiver == null || sender == null)
                {
                    receiver = new TcpCommandReceiver(socket, converter);
                    sender   = new TcpCommandSender(socket, converter);
                }
                else
                {
                    receiver.Connection = socket;
                    sender.Connection   = socket;
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }