我想通过调用另一种方法向订阅者发送通知(说一个字符串)(订阅者IP地址在服务器端的数据库中)。当我调用该方法时,输出会变成错误 - 一些。发送通知客户端
[WebMethod]
public string GetGroupPath(string emailAddress, string password, string ipAddress)
{
//SqlDataAdapter dbadapter = null;
DataSet returnDS = new DataSet();
string groupName = null;
string groupPath = null;
SqlConnection dbconn = new SqlConnection("Server = localhost;Database=server;User ID = admin;Password = password;Trusted_Connection=false;");
dbconn.Open();
SqlCommand cmd = new SqlCommand();
string getGroupName = "select users.groupname from users where emailaddress = "+"'"+ emailAddress+"'"+ " and "+ "users.password = " + "'" +password+"'";
cmd.CommandText = getGroupName;
cmd.Connection = dbconn;
SqlDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
groupName = reader["groupname"].ToString();
}
}
catch (Exception)
{
groupPath = "Invalied";
}
dbconn.Close();
dbconn.Open();
if (groupName != null)
{
string getPath = "select groups.grouppath from groups where groupname = " + "'" + groupName + "'";
cmd.CommandText = getPath;
cmd.Connection = dbconn;
try
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
groupPath = reader["grouppath"].ToString();
}
}
catch
{
groupPath = "Invalied";
}
}
else
groupPath = "Invalied";
dbconn.Close();
if (groupPath != "Invalied")
{
dbconn.Open();
string getPath = "update users set users.ipaddress = "+"'"+ipAddress+"'"+" where users.emailaddress = " + "'" + emailAddress + "'";
cmd.CommandText = getPath;
cmd.Connection = dbconn;
cmd.ExecuteNonQuery();
dbconn.Close();
}
NotifyUsers();
//NotifyUsers nu = new NotifyUsers();
//List<string> ipList = new List<string>();
//ipList.Add("192.168.56.1");
//nu.Notify();
return groupPath;
}
private void NotifyUsers()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] ipb = Encoding.ASCII.GetBytes("255.255.255.255");
IPAddress ipAddress = new IPAddress(ipb);
IPEndPoint endPoint = new IPEndPoint(ipAddress, 15000);
string notification = "new_update";
byte[] sendBuffer = Encoding.ASCII.GetBytes(notification);
sock.SendTo(sendBuffer, endPoint);
sock.Close();
}
这是基本要做的。在服务器端,我有一个监听线程,并在服务器发送数据时获取通知(现在假设数据库包含客户端IP地址)。越看越我调用Web方法它给出了一个错误“无效的ip地址” ATLINE
byte[] ipb = Encoding.ASCII.GetBytes("255.255.255.255");
谢谢:)因为这是我第一次发帖请好心给我一个更好的反馈太:) thaks
什么是错误? – Anas
这样的问题总是一个好主意 - 帮助人们精确地确定问题的原因...... –
请考虑使用此代码的SQL注入安全问题,考虑使用参数化SQL或存储过程,或者ORM。 – Iain