Inheritance: AbstractTcpSocket
Exemplo n.º 1
0
 public void Connect(IPAddress ip, int port)
 {
     var client = new TcpClientSocket();
     client.OnConnect += onConnected;
     client.Connect(ip, port);
     socket = client;
 }
Exemplo n.º 2
0
 static void initClient(string[] args)
 {
     IPAddress ip = null;
     int port = 0;
     try {
         ip = IPAddress.Parse(args[1]);
         port = int.Parse(args[2]);
         var client = new TcpClientSocket();
         client.OnDisconnect += onDisconnect;
         client.Connect(ip, port);
         _socket = client;
         start();
     } catch (Exception) {
         Console.WriteLine("Please specify a valid ip and port");
     }
 }
Exemplo n.º 3
0
    void when_created()
    {
        TcpClientSocket client = null;
        before = () => {
            client = new TcpClientSocket();
        };

        it["isn't connected"] = () => client.isConnected.should_be_false();

        it["disconnects without triggering event"] = () => {
            client.OnDisconnect += (sender, e) => fail();
            client.Disconnect();
        };

        it["can not connect when host is not available"] = () => {
            client.OnConnect += (sender, e) => fail();
            client.Connect(IPAddress.Loopback, Port);
            wait();
            client.isConnected.should_be_false();
        };

        it["can connect when host is available"] = () => {
            var didConnect = false;
            createServer(Port);
            client.OnConnect += (sender, e) => didConnect = true;
            client.Connect(IPAddress.Loopback, Port);
            wait();
            client.isConnected.should_be_true();
            didConnect.should_be_true();
            closeServer();
        };

        it["can't send"] = () => client.Send(new byte[] { 1, 2 });

        context["when connected"] = () => {
            before = () => {
                createServer(Port);
                client.Connect(IPAddress.Loopback, Port);
                wait();
            };

            after = () => {
                closeServer();
            };

            it["can disconnect"] = () => {
                var didDisconnect = false;
                client.OnDisconnect += (sender, e) => didDisconnect = true;
                client.Disconnect();
                wait();
                client.isConnected.should_be_false();
                didDisconnect.should_be_true();
            };

            it["receives disconnect"] = () => {
                var didDisConnect = false;
                client.OnDisconnect += (sender, e) => didDisConnect = true;
                closeServer();
                wait();
                didDisConnect.should_be_true();
                client.isConnected.should_be_false();
            };

            it["receives message"] = () => {
                const string message = "Hello";
                var receivedMessage = string.Empty;
                client.OnReceive += (sender, e) => receivedMessage = Encoding.UTF8.GetString(e.bytes);
                _clientServer.Send(Encoding.UTF8.GetBytes(message));
                wait();
                message.should_be(receivedMessage);
            };

            it["receives multiple messages"] = () => {
                const string message1 = "Hello1";
                const string message2 = "Hello2";
                ReceiveEventArgs receiveEventArgs = null;
                client.OnReceive += (sender, e) => receiveEventArgs = e;

                _clientServer.Send(Encoding.UTF8.GetBytes(message1));
                wait();
                message1.should_be(Encoding.UTF8.GetString(receiveEventArgs.bytes));

                _clientServer.Send(Encoding.UTF8.GetBytes(message2));
                wait();
                message2.should_be(Encoding.UTF8.GetString(receiveEventArgs.bytes));
            };

            it["can send"] = () => {
                const string message = "Hello";
                var receivedMessage = string.Empty;
                byte[] buffer = new byte[_clientServer.ReceiveBufferSize];
                _clientServer.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
                    ar => {
                        var server = (Socket)ar.AsyncState;
                        var bytesReceived = server.EndReceive(ar);
                        var trimmedBuffer = new byte[bytesReceived];
                        Array.Copy(buffer, trimmedBuffer, bytesReceived);
                        receivedMessage = Encoding.UTF8.GetString(trimmedBuffer);
                    }, _clientServer);

                client.Send(Encoding.UTF8.GetBytes(message));
                wait();
                message.should_be(receivedMessage);
            };
        };
    }