Пример #1
0
        public void Send_message_SendsMessage()
        {
            // Establish the local endpoint for the socket.
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 20055);

            // Create a server
            Socket server = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            server.Bind(localEndPoint);
            server.Listen(2);

            // Create a SocketConnectionController
            SocketConnection socket = new SocketConnection(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(localEndPoint);

            SocketConnectionController <MessageType> socketConnectionController = new SocketConnectionController <MessageType>(socket, 1024);

            socketConnectionController.Start();

            Socket server_receiver = server.Accept();

            Thread.Sleep(1000);
            byte[] message      = Encoding.ASCII.GetBytes("ThisIsAMessage");
            byte[] expectedData = PacketProtocol <MessageType> .WrapMessage(MessageType.Standard, message);

            socketConnectionController.Send(MessageType.Standard, message);
            byte[] receiveBuffer = new byte[expectedData.Length];
            server_receiver.Receive(receiveBuffer);
            Assert.AreEqual(expectedData, receiveBuffer);
        }
Пример #2
0
 private void Connect()
 {
     _ConnectionState = State.CONNECTING;
     //GameConsole.Log("Connecting to TCP Server");
     if (_Connection.Connect())
     {
         _ConnectionState = State.CONNECTED;
         //GameConsole.Log("Connected to TCP Server");
         Reciever.SetConnected(true);
     }
     else
     {
         //GameConsole.Log("Failed to connect, retrying in background.");
         StartCoroutine(BackgroundConnecting());
     }
 }
Пример #3
0
            public SocketConnection ConnectSocket(string address, int port, Type socketType)
            {
                Debug.Log(string.Format("Trying connection to {0}:{1} as type {2}", address, port, socketType));

                SocketConnection socket = (SocketConnection)Activator.CreateInstance(socketType);

                if (socket != null)
                {
                    socket.owner = this;
                    try {
                        socket.Connect(address, port);
                        Debug.Log(string.Format("{2} :: Connected to client @ {0}:{1} as {3}", address, port,
                                                socket.IsConnected(), socketType));
                        socket.OnConnectionMade(this, new SocketEventArgs(socketType));
                    }
                    catch (Exception e) {
                        Debug.Log(e.Message);
                    }
                }
                else
                {
                    Debug.Log("Failed to create client");
                    //socket = null;
                }

                return(socket);
            }
Пример #4
0
        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            var server = txtBoxServer.Text;
            var port   = txtBoxPort.Text;

            SocketConnection.Connect(server, port);
            commandAllowed = true;
        }
Пример #5
0
    // 链接服务器
    public void Connect(string ip, int port)
    {
        _conn = new SocketConnection();

        _conn.ConnectionSuccess += new EventHandler(OnConnectSuccessEvent);
        _conn.ConnectionFailed  += new EventHandler(OnConnectFailedEvent);
        _conn.ConnectionLost    += new EventHandler(OnConnectLostEvent);

        _conn.Connect(ip, port);
    }
Пример #6
0
        public bool Login(string user, string pass)
        {
            _controlConnection.Connect();
            _controlConnection.Receive(100);

            _controlConnection.Send($"USER {user}");
            var _ = _controlConnection.Receive(100);

            _controlConnection.Send($"PASS {pass}");
            return(_controlConnection.Receive(100).ResponseCode == 230);
        }
Пример #7
0
        private void OpenDataConnection()
        {
            _dataConnection?.Dispose();

            _controlConnection.Send("PASV");
            var recv = _controlConnection.Receive(100).Message;

            var(ip, port) = CalculatePasvIpAddressFromResponse(recv);

            _dataConnection = new SocketConnection(IPAddress.Parse(ip), port);
            _dataConnection.Connect();
        }
Пример #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") org.neo4j.bolt.v1.transport.socket.client.TransportConnection startBoltSession(String username, String password) throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual TransportConnection StartBoltSession(string username, string password)
        {
            TransportConnection          connection = new SocketConnection();
            HostnamePort                 address    = Neo.lookupConnector(DEFAULT_CONNECTOR_KEY);
            IDictionary <string, object> authToken  = map("principal", username, "credentials", password, "scheme", "basic");

            connection.Connect(address).send(Util.acceptedVersions(1, 0, 0, 0)).send(Util.chunk(new InitMessage("TestClient/1.1", authToken)));

            assertThat(connection, eventuallyReceives(new sbyte[] { 0, 0, 0, 1 }));
            assertThat(connection, Util.eventuallyReceives(msgSuccess()));
            return(connection);
        }
        private void connectBtn_Click(object sender, EventArgs e)
        {
            // Connect to the socket
            try
            {
                Client = new SocketConnection(ipAddressTextBox.Text, Convert.ToInt32(portTextBox.Text));
                Client.SocketSetup();
                Client.Connect();

                buttotnStatus();
                initSwitch = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"ERROR: {ex.Message}. Make sure the server is active.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #10
0
 public void TryReconnectSocket(string address, int port, Type socketType, ref SocketConnection socket)
 {
     if (socket != null)
     {
         try {
             socket.Connect(address, port);
             Debug.Log(string.Format("{2} :: Connected to client @ {0}:{1} as {3}", address, port,
                                     socket.IsConnected(), socketType));
             socket.OnConnectionMade(this, new SocketEventArgs(socketType));
         }
         catch (Exception e) {
             socket.OnConnectionLost(this, null);
             //Debug.Log(e.Message);
         }
     }
     else
     {
         Debug.Log("Failed to create client");
         //socket = null;
     }
 }
Пример #11
0
        static void Main(string[] args)
        {
            SocketConnection client = new SocketConnection("127.0.0.1", 1234);

            client.Connect();

            //Console.ReadKey();

            client.SendText("bibliotekGetAll");

            int size = client.GetDataSize();

            Console.WriteLine(size);

            client.GetDataType();

            List <Bibliotek> bib = (List <Bibliotek>)client.ReceiveObject(size);

            Console.WriteLine(bib[0].Navn);

            Console.ReadKey();
        }