2012-05-31 201 views
0

我想通過調用另一種方法向訂閱者發送通知(說一個字符串)(訂閱者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

+2

什麼是錯誤? – Anas

+0

這樣的問題總是一個好主意 - 幫助人們精確地確定問題的原因...... –

+0

請考慮使用此代碼的SQL注入安全問題,考慮使用參數化SQL或存儲過程,或者ORM。 – Iain

回答

1

嘗試在通知下面的代碼,即禁用防火牆設置之前...

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() {} 

希望這將解決這一問題。

1

只是想補充一點,

byte[] ipb = Encoding.ASCII.GetBytes("255.255.255.255"); 
IPAddress ipAddress = new IPAddress(ipb); 

可以降低到

​​

,或者,如果你想使用其他IP地址,你也可以使用.Parse()

var ip = IPAddress.Parse("192.168.1.1"); 

更新:

其實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 }; 
相關問題