C# 发送通知客户端
我想通过调用另一个方法向订阅者(订阅者的ip地址在服务器端的数据库中)发送通知(比如字符串)。每当我调用该方法时,输出都会出现一些错误C# 发送通知客户端,c#,.net,web-services,webmethod,C#,.net,Web Services,Webmethod,我想通过调用另一个方法向订阅者(订阅者的ip地址在服务器端的数据库中)发送通知(比如字符串)。每当我调用该方法时,输出都会出现一些错误 [WebMethod] public string GetGroupPath(string emailAddress, string password, string ipAddress) { //SqlDataAdapter dbadapter = null; DataSet returnDS = new DataSet(); stri
[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();
}
谢谢:)因为这是我的第一篇帖子,请也给我一个更好的反馈:)
thaks在通知中尝试以下代码,然后禁用防火墙设置
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,15000);
string notification = "new_update";
byte[] sendBuffer = Encoding.ASCII.GetBytes(notification);
sock.SendTo(sendBuffer, remoteEP );
sock.Close();
}
catch() {}
希望这能解决问题。只是想补充一点
byte[] ipb = Encoding.ASCII.GetBytes("255.255.255.255");
IPAddress ipAddress = new IPAddress(ipb);
可以简化为
IPAddress.Broadcast
或者,如果您想使用另一个IP地址,也可以使用.Parse()
更新:
实际上Encoding.ASCII.GetBytes(“255.255.255.255”)代码>将给您一个完全不同的结果
var bytes = Encoding.ASCII.GetBytes("255.255.255.255");
// bytes returns something like
// new byte[] { 50, 53, 53, 46... }
// what you want is
var bytes = new byte[] { 255, 255, 255, 255 };
在这样的问题中包含异常总是一个好主意-帮助人们准确地缩小问题的范围…还请考虑此代码的SQL注入安全问题,考虑使用参数化SQL或存储过程或ORM。SQL注入在这里是正确的。我只是想先解决这个问题。好的,我已经解决了这个问题。有人知道我应该在byte[]ipb=Encoding.ASCII.GetBytes(“255.255.255.255”)行中使用什么ip地址吗;我使用过localhost(127.0.0.1),但没有使用。客户端也在同一台机器上运行。谢谢
var ip = IPAddress.Parse("192.168.1.1");
var bytes = Encoding.ASCII.GetBytes("255.255.255.255");
// bytes returns something like
// new byte[] { 50, 53, 53, 46... }
// what you want is
var bytes = new byte[] { 255, 255, 255, 255 };