Пример #1
0
        ForwardState StartRemote(Socket local_socket, IPEndPoint local, IPEndPoint remote)
        {
            if (_remote_socket != null)
            {
                _remote_socket.Close();
                _remote_socket = null;
            }

            if (_remote_socket == null)
            {
                _remote_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            Thread.Sleep(100);
            _remote_socket.Connect(remote);
            Debug.WriteLine(">>>>forward remote connected");

            var state = new ForwardState()
            {
                LocalEndPoint  = local,
                RemoteEndPoint = remote,
                LocalSocket    = local_socket,
                RemoteSocket   = _remote_socket
            };

            return(state);
        }
Пример #2
0
        private static void LocalDataRecive(IAsyncResult result)
        {
            ForwardState state = null;

            try
            {
                state = (ForwardState)result.AsyncState;
                var br = state.LocalSocket.EndReceive(result);
                if (br > 0)
                {
                    try
                    {
                        Thread.Sleep(1);
                        if (state.RemoteSocket != null)
                        {
                            state.RemoteSocket.BeginSend(state.LocalBuffer, 0, br, SocketFlags.None, RemoteDataSend, state);
                        }
                        Debug.Write($">>>>{state.LocalEndPoint.Port} => {state.RemoteEndPoint.Port}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"####remote send error : {ex.Message}");
                    }
                    state.LocalBuffer = new byte[state.LocalBuffer.Length];
                    state.LocalSocket.BeginReceive(state.LocalBuffer, 0, state.LocalBuffer.Length, 0, LocalDataRecive, state);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"####local recive error : {ex.Message}");
            }
        }
Пример #3
0
 private void StartRemoteRecive(ForwardState state)
 {
     _remote_socket.BeginReceive(state.RemoteBuffer, 0, state.RemoteBuffer.Length, 0, RemoteDataRecive, state);
 }
Пример #4
0
 private void StartLocalRecive(ForwardState state)
 {
     _local_socket.BeginReceive(state.LocalBuffer, 0, state.LocalBuffer.Length, 0, LocalDataRecive, state);
 }