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アドレス」というエラーが表示されます

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

ありがとう:)これは私の初めての投稿なので、私にもより良いフィードバックを与えるのに十分親切にしてください:)ありがとう

4

2 に答える 2

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

これで問題が解決することを願っています。

于 2012-05-31T10:32:40.977 に答える