我只是想问一下这里是否有问题,没有错误,但是当我在客户端上登录时,它说的阻止连接来自: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];
}
}
}
}
答案 0 :(得分:0)
Blocked connection from:
,这是条件:
if (!sqlDataReader.HasRows)
之所以会发生这种情况,是因为它没有在表中找到似乎是sollicitor的IP地址的内容:
"SELECT * FROM RohanUser.dbo.TUser where IPV4 = '{0}'"
您应该检查str1
的内容以及SQL请求的结果及其值。
编辑:@Alexander Higgins指出你的子串可能是原因。
答案 1 :(得分:0)
当sql数据readerL没有返回任何行时,您的程序会推出此消息
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
等于前5个字符的记录socket.RemoteEndPoint.ToString()
如果IPV4
是IP4 IP地址,则可能应该设置为str1
的5个字符。
如果它只是5个字符,那么您的数据源中没有行。