示例#1
0
        public String Connect(String ip, int port)
        {
            String clientUUID = "";

            tcpClient = new TcpClient();
            tcpClient.Connect(IPAddress.Parse(ip), port);
            tcpClient.NoDelay = true;
            ConfigureSocket.ConfigureTcpSocket(tcpClient.Client);
            networkStream = tcpClient.GetStream();
            streamReader  = new System.IO.StreamReader(networkStream);
            streamWriter  = new System.IO.StreamWriter(networkStream);
            isConnected   = true;
            while (true)
            {
                String serverData = streamReader.ReadLine();
                if (serverData.StartsWith("connect_ok"))
                {
                    String[] data = serverData.Split('|');
                    clientUUID = data[1];
                    break;
                }
            }

            Debug.WriteLine("CLIENT: Connected to the server at {0}:{1}", ip, port);
            return(clientUUID);
        }
示例#2
0
        public void Start()
        {
            tcpListener.Start();
            Program.serverScreen.ShowMessage(String.Format("SERVER: Server started at {0}:{1}", serverIp, serverPort));

            while (true)
            {
                if (tcpListener.Pending())
                {
                    Socket socket = tcpListener.AcceptSocket();
                    ConfigureSocket.ConfigureTcpSocket(socket);
                    Program.serverScreen.ShowMessage("SERVER: Client connected");
                    var task = Task.Run((() => Listeners(socket)));
                }
            }
        }
示例#3
0
        public static async Task <(Socket, Stream)> ConnectAsync(string host, int port, ConfigureSocket configureSocket, CancellationToken cancellationToken)
        {
            // Rather than creating a new Socket and calling ConnectAsync on it, we use the static
            // Socket.ConnectAsync with a SocketAsyncEventArgs, as we can then use Socket.CancelConnectAsync
            // to cancel it if needed. Rent or allocate one.
            ConnectEventArgs saea;

            if (!s_connectEventArgs.TryDequeue(out saea))
            {
                saea = new ConnectEventArgs();
            }

            try
            {
                saea.Initialize(cancellationToken);

                // Configure which server to which to connect.
                saea.RemoteEndPoint = new DnsEndPoint(host, port);

                // Initiate the connection.
                if (Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, saea))
                {
                    // Connect completing asynchronously. Enable it to be canceled and wait for it.
                    using (cancellationToken.Register(s => Socket.CancelConnectAsync((SocketAsyncEventArgs)s), saea))
                    {
                        await saea.Builder.Task.ConfigureAwait(false);
                    }
                }
                else if (saea.SocketError != SocketError.Success)
                {
                    // Connect completed synchronously but unsuccessfully.
                    throw new SocketException((int)saea.SocketError);
                }

                Debug.Assert(saea.SocketError == SocketError.Success, $"Expected Success, got {saea.SocketError}.");
                Debug.Assert(saea.ConnectSocket != null, "Expected non-null socket");

                // Configure the socket and return a stream for it.
                Socket socket = saea.ConnectSocket;
                socket.NoDelay = true;
                configureSocket(socket);
                return(socket, new NetworkStream(socket, ownsSocket: true));
            }
            catch (Exception error)
            {
                throw CancellationHelper.ShouldWrapInOperationCanceledException(error, cancellationToken) ?
                      CancellationHelper.CreateOperationCanceledException(error, cancellationToken) :
                      new HttpRequestException(error.Message, error);
            }
            finally
            {
                // Pool the event args, or if the pool is full, dispose of it.
                saea.Clear();
                if (s_connectEventArgs.Count <= Math.Max(2, Environment.ProcessorCount))
                {
                    s_connectEventArgs.Enqueue(saea);
                }
                else
                {
                    saea.Dispose();
                }
            }
        }