2013-04-06 5 views
1

я хочу узнать XMPP с использованием C# .NET в Windows 7, так что я downlod библиотеку agsXMPP и jspon, и я написать следующий код только для теста, но я не могу отправить informtion:C# XMPP не связывает

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); 

    } 


} 

}

я ввести свой идентификатор (созданный для теста) usernme: [email protected]

здесь выход:

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" 

ответ

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); 
Смежные вопросы