【问题标题】:c# XMPP does not connectc# XMPP不连接
【发布时间】:2013-04-06 18:36:52
【问题描述】:

我想在 Windows 7 中使用 c#.net 学习 XMPP,所以我下载了 agsXMPP 库和 jspon,我编写以下代码只是为了测试,但我无法发送信息:

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;
using System.Threading;

namespace ConsoleApplication1

{
    class Program
    {
    static void xmpp_OnLogin(object sender)
    {
        Console.WriteLine("Logged In");
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Login");
        Console.WriteLine();
        Console.WriteLine("JID: ");
        string JID_Sender = Console.ReadLine();
        Console.WriteLine("Password: ");
        string Password = Console.ReadLine();

        Jid jidSender = new Jid(JID_Sender);
        XmppClientConnection xmpp = new XmppClientConnection(jidSender.Server);

        xmpp.Open(jidSender.User, Password);
        xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);


        Presence p = new Presence(ShowType.chat, "Online");
        p.Type = PresenceType.available;
        xmpp.Send(p);

    }


}

}

我输入我的 id(只是为测试创建的) 用户名:belkacemsend@jabber.org

这是输出:

Exception:Thrown: "Object reference not set to an instance of an object." (System.NullReferenceException)
A System.NullReferenceException was thrown: "Object reference not set to an instance of an object."


Exception:Caught: "Object reference not set to an instance of an object." (System.NullReferenceException)
A System.NullReferenceException was caught: "Object reference not set to an instance of an object."


Exception:Thrown: "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied" (System.Net.Sockets.SocketException)
A System.Net.Sockets.SocketException was thrown: "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"


Exception:Caught: "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied" (System.Net.Sockets.SocketException)
A System.Net.Sockets.SocketException was caught: "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"

【问题讨论】:

    标签: c# xmpp


    【解决方案1】:

    您的问题是您需要等待 OnLogin 将被提出

    xmpp.Open(jidSender.User, Password);
    xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
    
    // Wait here
    
    Presence p = new Presence(ShowType.chat, "Online");
    p.Type = PresenceType.available;
    xmpp.Send(p);
    

    codeproject.com 上的这篇文章对此Creating a Jabber Client using the agsXMPP Library 有解决方案,虽然不是最好的,但它是一个很好的开始。

    bool isLoggedIn = false;
    xmpp.OnLogin += (sender) => { isLoggedIn = true; };
    xmpp.Open(jidSender.User, Password);
    
    while (isLoggedIn == false) 
    { 
        System.Threading.Thread.Sleep(100); 
    }
    
    Presence p = new Presence(ShowType.chat, "Online");
    p.Type = PresenceType.available;
    xmpp.Send(p);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多