Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
AK80 #1
Member since Jan 2014 · 4 posts
Group memberships: Members
Show profile · Link to this post
Subject: Proxy: System.Net.Sockets.SocketException
I would like to create a feature that allows the user to set the proxy if he has one (what's the best way to do this? Only with BOSH?), here is my code but I get a SocketException.

  1. xmppClient.SetUsername(XMPP_USERNAME);
  2. xmppClient.Password = XMPP_PASSWORD;
  3. xmppClient.SetXmppDomain(XMPP_DOMAIN);
  4. xmppClient.Resource = XMPP_RESOURCE;
  5. xmppClient.Status = "";
  6. xmppClient.Show = Matrix.Xmpp.Show.NONE;
  7. xmppClient.StartTls = false;
  8. xmppClient.ResolveSrvRecords = false;
  9. xmppClient.Port = 5222;
  10. xmppClient.Hostname = XMPP_SERVER_IP;
  11.  
  12. if(USE_PROXY)
  13. {
  14.     xmppClient.ProxyType = ProxyType.HttpTunnel;
  15.  
  16.     xmppClient.ProxyHostname = PROXY_ADDRESS_INSERTED_BY_THE_USER;
  17.  
  18.     if (!string.IsNullOrEmpty(PROXY_PORT_INSERTED_BY_THE_USER))
  19.         xmppClient.ProxyPort = int.Parse(PROXY_PORT_INSERTED_BY_THE_USER);
  20.  
  21.     if(!string.IsNullOrEmpty(PROXY_USERNAME_INSERTED_BY_THE_USER) && !string.IsNullOrEmpty(PROXY_PASSWORD_INSERTED_BY_THE_USER))
  22.     {
  23.         xmppClient.ProxyUser = PROXY_USERNAME_INSERTED_BY_THE_USER;
  24.         xmppClient.ProxyPass = PROXY_PASSWORD_INSERTED_BY_THE_USER;
  25.     }   
  26. }
  27.  
  28. xmppClient.Open();

Exception:

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
XMPP_SERVER_IP:PROXY_PORT_INSERTED_BY_THE_USER

I do not know the type of the proxy but this code works for my HTTP requests:

  1. if(USE_PROXY)
  2. {
  3.     request.Proxy = CreateProxy();
  4. }
  5.  
  6. private WebProxy CreateProxy()
  7. {
  8.     WebProxy proxy;
  9.  
  10.     if (string.IsNullOrEmpty(PROXY_PORT_INSERTED_BY_THE_USER))
  11.         proxy = new WebProxy(PROXY_ADDRESS_INSERTED_BY_THE_USER);
  12.     else
  13.         proxy = new WebProxy(PROXY_ADDRESS_INSERTED_BY_THE_USER, Convert.ToInt32(PROXY_PORT_INSERTED_BY_THE_USER));
  14.  
  15.     if(string.IsNullOrEmpty(PROXY_USERNAME_INSERTED_BY_THE_USER) && string.IsNullOrEmpty(PROXY_PASSWORD_INSERTED_BY_THE_USER))
  16.         return proxy;
  17.  
  18.     NetworkCredential nc = new NetworkCredential(PROXY_USERNAME_INSERTED_BY_THE_USER, PROXY_PASSWORD_INSERTED_BY_THE_USER);
  19.     proxy.Credentials = nc;
  20.  
  21.     return proxy;
  22. }
This post was edited on 2014-01-18, 12:29 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4322 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
There are 2 different transports for XMPP

  • Sockets
  • BOSH

Sockets is a long living TCP/IP connections, based on sockets.
BOSH is based on HTTP and is using HTTP requests.

BOSH is using .NETs HttpWebRequest classes, while sockets are based System.Net.Sockets.

The code you are posted and which is working is using BOSH and you add the proxy information to the .NETs HTTP request.
An HTTP proxy and a Sockets proxy (like SOCKS) are 2 totally different technologies.

Alex
Avatar
AK80 #3
Member since Jan 2014 · 4 posts
Group memberships: Members
Show profile · Link to this post
Quote by Alex:
The code you are posted and which is working is using BOSH and you add the proxy information to the .NETs HTTP request.
An HTTP proxy and a Sockets proxy (like SOCKS) are 2 totally different technologies.

Thank you for your answer.

Maybe I'm misleading you... I think my first code snippet isn't using BOSH because xmppClient.Transport is Matrix.Net.Transport.Socket.
Maybe my mistake is because I'm trying to use a proxy of type ProxyType.HttpTunnel. Is all correct?
Avatar
Alex #4
Member since Feb 2003 · 4322 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by AK80:
Maybe I'm misleading you... I think my first code snippet isn't using BOSH because xmppClient.Transport is Matrix.Net.Transport.Socket.

that's correct. My understanding was that you use the same proxy in your BOSH snippet than in your socket snippet, which may not work.

When you use HttpTunnel then MatriX tries to establish a HTTP tunnel over the HTTP CONNECT method. So your proxy must support HTTP CONNECT and must allow the tunnel to the given host and the given port (5222 by default).

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: MatriX RSS