簡體   English   中英

C#插座塊連接

[英]C# Socket Block Connections

我只想問一下這里是否有問題,沒有錯誤,但是當我在客戶端上登錄時,它說來自192.168.xx的阻止連接,我無法弄清楚代碼的問題,請有人幫幫我,謝謝

    private static SqlConnection Database;

    public socket()
    {
    }

    private void Connect(EndPoint remoteEndpoint, Socket destination)
    {
        socket.State state = new socket.State(this._mainSocket, destination);
        this._mainSocket.Connect(remoteEndpoint);
        this._mainSocket.BeginReceive(state.Buffer, 0, (int)state.Buffer.Length, SocketFlags.None, new AsyncCallback(socket.OnDataReceive), state);
    }

    private static void OnDataReceive(IAsyncResult result)
    {
        socket.State asyncState = (socket.State)result.AsyncState;
        try
        {
            int num = asyncState.SourceSocket.EndReceive(result);
            if (num > 0)
            {
                asyncState.DestinationSocket.Send(asyncState.Buffer, num, SocketFlags.None);
                asyncState.SourceSocket.BeginReceive(asyncState.Buffer, 0, (int)asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(socket.OnDataReceive), asyncState);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine("Player disconnected...");
            asyncState.DestinationSocket.Close();
            asyncState.SourceSocket.Close();
        }
    }

    public void Start(IPEndPoint local, IPEndPoint remote)
    {
        this._mainSocket.Bind(local);
        this._mainSocket.Listen(10);
        while (true)
        {
            try
            {
                Socket socket = this._mainSocket.Accept();
                Intercept.socket _socket = new Intercept.socket();
                Intercept.socket.State state = new Intercept.socket.State(socket, _socket._mainSocket);
                SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder()
                {
                    DataSource = @"ASHTRA-PC\LocalServer",
                    MultipleActiveResultSets = true,
                    Password = "121314z!",
                    UserID = "sa"
                };
                Intercept.socket.Database = new SqlConnection()
                {
                    ConnectionString = sqlConnectionStringBuilder.ConnectionString
                };
                Intercept.socket.Database.Open();
                SqlCommand sqlCommand = Intercept.socket.Database.CreateCommand();
                string str = socket.RemoteEndPoint.ToString();
                string str1 = str.Substring(0,5);
                socket.RemoteEndPoint.ToString();
                sqlCommand.CommandText = string.Format("SELECT * FROM RohanUser.dbo.TUser where IPV4 = '{0}'",str1);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                sqlDataReader.Read();
                if (!sqlDataReader.HasRows)
                {
                    string str2 = sqlDataReader["login_id"].ToString();
                    _socket.Connect(remote, socket);
                    socket.BeginReceive(state.Buffer, 0, (int)state.Buffer.Length, SocketFlags.None, new AsyncCallback(Intercept.socket.OnDataReceive), state);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Accepted connection");
                    Console.ResetColor();
                    sqlDataReader.Close();
                    sqlCommand.CommandText = string.Format("Update RohanUser.dbo.TUser set IPV4 = 0 WHERE login_id = '{0}'", str2);
                    sqlCommand.ExecuteNonQuery();

                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(string.Concat("Blocked connection from: ", socket.RemoteEndPoint.ToString()));
                    Console.ResetColor();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
    }

    private static void Stress(socket.State state, int bytesRead, int times)
    {
        for (int i = 0; i < times; i++)
        {
            Console.WriteLine(string.Concat("Test ", times));
            state.DestinationSocket.Send(state.Buffer, bytesRead, SocketFlags.None);
        }
    }

    private class State
    {
        public byte[] Buffer
        {
            get;
            set;
        }

        public Socket DestinationSocket
        {
            get;
            private set;
        }

        public Socket SourceSocket
        {
            get;
            private set;
        }

        public State(Socket source, Socket destination)
        {
            this.SourceSocket = source;
            this.DestinationSocket = destination;
            this.Buffer = new byte[8192];
        }
    }
}

}

Blocked connection from:似乎在沒有結果時出現,這是以下情況:

if (!sqlDataReader.HasRows)

發生這種情況是因為它在表中找不到似乎是該請求者的IP地址:

"SELECT * FROM RohanUser.dbo.TUser where IPV4 = '{0}'"

您應該檢查str1的內容和SQL請求的結果及其值。

編輯:@Alexander Higgins指出您的子字符串可能是原因。

當sql數據讀取器沒有返回任何行時,程序將發出此消息

 Console.WriteLine(string.Concat("Blocked connection from: ", socket.RemoteEndPoint.ToString()));

它正在運行此查詢:

string.Format("SELECT * FROM RohanUser.dbo.TUser where IPV4 = '{0}'",str1);

str1定義為:

string str = socket.RemoteEndPoint.ToString();
string str1 = str.Substring(0,5);

因此它正在嘗試查詢數據源中IPV4等於socket.RemoteEndPoint.ToString()的前5個字符的記錄socket.RemoteEndPoint.ToString()

如果IPV4應該是IP4 IPAddress,則可能要比str1設置的5個字符長。

如果只包含5個字符,則數據源中沒有行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM