Ejemplo n.º 1
0
        static void Main()
        {
            var      serverAddres   = IPAddress.Loopback;
            var      serverEndPoint = new IPEndPoint(serverAddres, ServerPort);
            EndPoint emptyEndPoint  = new IPEndPoint(IPAddress.None, ServerPort);

            var client = new AsyncClientArgs(BufferSize);
            var socket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            socket.ReceiveTimeout = 500;
            Console.WriteLine("Enter the message for server");

            string request;

            do
            {
                Console.Write("> ");
                request            = Console.ReadLine();
                client.UTF8Message = request;
                socket.SendTo(client.Buffer, serverEndPoint);
                try
                {
                    socket.ReceiveFrom(client.Buffer, ref emptyEndPoint);
                    Console.WriteLine("< " + client.UTF8Message);
                }
                catch (SocketException ex)
                {
                    Console.WriteLine("Error: " + ex.SocketErrorCode);
                }
            } while (!string.IsNullOrEmpty(request));
        }
        public void RequestResponse()
        {
            _server.Start();

            var client = new AsyncClientArgs(BufferSize);
            client.AcceptSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            for (var i = 0; i < 100; i++)
            {
                var message = "Request" + i;
                client.UTF8Message = message;
                client.AcceptSocket.SendTo(client.Buffer, _ipEndPoint);

                client.UTF8Message = null;
                client.AcceptSocket.ReceiveFrom(client.Buffer, ref _emptyEndPoint);

                Assert.AreEqual(message, client.UTF8Message);
            }

            _server.Stop();
        }
Ejemplo n.º 3
0
        public void RequestResponse()
        {
            _server.Start();

            var client = new AsyncClientArgs(BufferSize);

            client.AcceptSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            for (var i = 0; i < 100; i++)
            {
                var message = "Request" + i;
                client.UTF8Message = message;
                client.AcceptSocket.SendTo(client.Buffer, _ipEndPoint);

                client.UTF8Message = null;
                client.AcceptSocket.ReceiveFrom(client.Buffer, ref _emptyEndPoint);

                Assert.AreEqual(message, client.UTF8Message);
            }

            _server.Stop();
        }
Ejemplo n.º 4
0
	private void CloseClientConnection(AsyncClientArgs args)
	{
		try
		{
			args.AcceptSocket.Shutdown(SocketShutdown.Both);
		}
		catch (SocketException ex)
		{
			Log.Debug(ex.Message);
		}
		
		args.AcceptSocket.Close();
		
		// освобождаем ресурс для следующего клиента
		_clientPool.Release(args);
	}
Ejemplo n.º 5
0
	private void ProcessSend(AsyncClientArgs args)
	{
		if (args.SocketError == SocketError.Success)
		{
			Log.Debug("{0} < {1}", args.AcceptSocket.RemoteEndPoint, args.UTF8Message);
		}
		else
		{
			Log.Error("{0} < {1}", args.AcceptSocket.RemoteEndPoint, args.SocketError);
		}
		
		CloseClientConnection(args);
	}
Ejemplo n.º 6
0
	private void ProcessReceive(AsyncClientArgs args)
	{
		// если данные получены
		if (args.SocketError == SocketError.Success)
		{
			if (args.BytesTransferred > 0)
			{
				var message = args.UTF8Message;
				Log.Debug("{0} > {1}", args.AcceptSocket.RemoteEndPoint, message);
				
				// передаем сообщение логическому серверу и получаем ответ
				//message = LogicServer.GetResponse(message);
				
				if (message == null)
				{
					CloseClientConnection(args);
					return;
				}
				
				args.UTF8Message = message;
				
				// посылаем сообщение обратно клиенту
				if (!args.AcceptSocket.SendAsync(args))
					ProcessSend(args);
			}
			else
			{
				// клиент закончил передачу сообщения
				CloseClientConnection(args);
			}
		}
		else
		{
			Log.Error("{0} > {1}", args.AcceptSocket.RemoteEndPoint, args.SocketError);
			CloseClientConnection(args);
		}
	}