Exemple #1
0
    private void initUDPClient()
    {
        udpState           = new UDPState();
        udpState.udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        udpState.serverIP  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8686);
        udpState.remoteEP  = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));

        asyncUDPSend("我来了");
        asyncUDPReceive();
    }
Exemple #2
0
        private static void StartListener(int port)
        {
            IPEndPoint endPoint      = new IPEndPoint(IPAddress.Any, port);
            UdpClient  listener      = new UdpClient(endPoint);
            UDPState   listenerState = new UDPState();

            listenerState.client  = listener;
            listenerState.address = endPoint;

            listener.BeginReceive(new AsyncCallback(ReceiveCallback), listenerState);
        }
    private void udpServerBind()
    {
        IPAddress  ip   = IPAddress.Parse("127.0.0.1");
        IPEndPoint Ipep = new IPEndPoint(ip, 8686);

        Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        udpServer.Bind(Ipep);

        IPEndPoint clientIP = new IPEndPoint(IPAddress.Any, 0);

        udpState           = new UDPState();
        udpState.udpServer = udpServer;
        udpState.remoteEP  = clientIP;

        asyncUDPReceive();
    }
Exemple #4
0
        //Создание слушающего UDP сокета
        private void StartListener(int port)
        {
            //Создание конечной точки подключения
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
            //Создание слушающего UDP сокета
            UdpClient listener = new UdpClient(endPoint);
            //Установка свойств UDP сокета
            UDPState listenerState = new UDPState();

            listenerState.client  = listener;
            listenerState.address = endPoint;
            //Начало операции приема данных
            bytesWrited = 0;
            fSize       = 0;
            fStream     = null;
            listener.BeginReceive(new AsyncCallback(ReceiveCallback), listenerState);
        }
        public static void UDPReceiveMessages()
        {
            // Receive a message and write it to the console.
            IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
            UdpClient  u = new UdpClient(e);
            UDPState   s = new UDPState();

            s.e = e;
            s.u = u;
            Console.WriteLine("listening for UDP messages");
            u.BeginReceive(new AsyncCallback(UDPReceiveCallback), s);

            // Do some work while we wait for a message.
            while (!messageReceived)
            {
                // Do something
            }
        }
        public void Start(Configuration config)
        {
            this._config = config;
            this._shareOverLAN = config.shareOverLan;

            if (CheckIfPortInUse(_config.localPort))
                throw new Exception(I18N.GetString("Port already in use"));

            try
            {
                // Create a TCP/IP socket.
                _tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                _udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                IPEndPoint localEndPoint = null;
                if (_shareOverLAN)
                {
                    localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort);
                }
                else
                {
                    localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort);
                }

                // Bind the socket to the local endpoint and listen for incoming connections.
                _tcpSocket.Bind(localEndPoint);
                _udpSocket.Bind(localEndPoint);
                _tcpSocket.Listen(1024);

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Shadowsocks started");
                _tcpSocket.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    _tcpSocket);
                UDPState udpState = new UDPState();
                _udpSocket.BeginReceiveFrom(udpState.buffer, 0, udpState.buffer.Length, 0, ref udpState.remoteEndPoint, new AsyncCallback(RecvFromCallback), udpState);
            }
            catch (SocketException)
            {
                _tcpSocket.Close();
                throw;
            }
        }
Exemple #7
0
        public void Start(Configuration config)
        {
            this._config       = config;
            this._shareOverLAN = config.shareOverLan;

            if (CheckIfPortInUse(_config.localPort))
            {
                throw new Exception(I18N.GetString("Port already in use"));
            }

            try
            {
                // Create a TCP/IP socket.
                _tcpSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                _udpSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);
                _tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                _udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                IPEndPoint localEndPoint = null;
                localEndPoint = _shareOverLAN
                    ? new IPEndPoint(IPAddress.Any, _config.localPort)
                    : new IPEndPoint(IPAddress.Loopback, _config.localPort);

                // Bind the socket to the local endpoint and listen for incoming connections.
                _tcpSocket.Bind(localEndPoint);
                _udpSocket.Bind(localEndPoint);
                _tcpSocket.Listen(1024);

                // Start an asynchronous socket to listen for connections.
                Logging.Info("Fuckshadows started");
                Logging.Info($"TFO: {Program.TFOSupported}");
                Logging.Info(Encryption.EncryptorFactory.DumpRegisteredEncryptor());

                _tcpSocket.BeginAccept(new AsyncCallback(AcceptCallback), _tcpSocket);
                UDPState udpState = new UDPState();
                udpState.socket = _udpSocket;
                _udpSocket.BeginReceiveFrom(udpState.buffer, 0, udpState.buffer.Length, 0, ref udpState.remoteEndPoint, new AsyncCallback(RecvFromCallback), udpState);
            }
            catch (SocketException)
            {
                _tcpSocket.Close();
                throw;
            }
        }
Exemple #8
0
    public void CreateHost(int _port)
    {
        if (isInit)
        {
            return;
        }

        udpClient = new UdpClient(_port);

        isHost = true;
        isInit = true;

        udpState            = new UDPState();
        udpState.socket     = udpClient;
        udpState.ipEndPoint = new IPEndPoint(IPAddress.Any, _port);
        udpClient.BeginReceive(Callback, udpState);

        ownerText.text = "";

        CreateCharacter(username);
    }
Exemple #9
0
        public void RecvFromCallback(IAsyncResult ar)
        {
            UDPState state = (UDPState)ar.AsyncState;

            try
            {
                int bytesRead = _udpSocket.EndReceiveFrom(ar, ref state.remoteEndPoint);
                foreach (Service service in _services)
                {
                    if (service.Handle(state.buffer, bytesRead, _udpSocket, state))
                    {
                        break;
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception)
            {
                this.Broken();
            }
            finally
            {
                try
                {
                    _udpSocket.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, 0, ref state.remoteEndPoint, new AsyncCallback(RecvFromCallback), state);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                }
                catch (Exception)
                {
                    this.Broken();
                }
            }
        }
        public void RecvFromCallback(IAsyncResult ar)
        {
            UDPState state  = (UDPState)ar.AsyncState;
            var      socket = state.socket;

            try
            {
                int bytesRead = socket.EndReceiveFrom(ar, ref state.remoteEndPoint);
                foreach (IService service in _services)
                {
                    if (service.Handle(state.buffer, bytesRead, socket, state))
                    {
                        break;
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                Logging.Debug(ex);
            }
            finally
            {
                try
                {
                    socket.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, 0, ref state.remoteEndPoint, new AsyncCallback(RecvFromCallback), state);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                }
                catch (Exception)
                {
                }
            }
        }
        public static void UDPReceiveCallback(IAsyncResult ar)
        {
            UdpClient  u = (UdpClient)((UDPState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UDPState)(ar.AsyncState)).e;

            Byte[] receiveBytes = u.EndReceive(ar, ref e);

            //Console.WriteLine("Bytes received: {0}", receiveBytes.Length);

            messageReceived = true;

            UDPState s = (UDPState)ar.AsyncState;

            UDPMessage msg = UDPMessage.Parser.ParseFrom(receiveBytes);

            BufferBlockWrapperUDP wrap = new BufferBlockWrapperUDP();

            wrap.message = msg;
            wrap.netID   = clients.Find(c => ((IPEndPoint)c.workSocket.RemoteEndPoint).Address.Equals(e.Address)).netID;

            asyncQueueUDP.SendAsync(wrap);

            u.BeginReceive(new AsyncCallback(UDPReceiveCallback), s);
        }