• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Connecting Sockets thru Proxy

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am trying to make socket connections thru a local firewall, for which I am setting the proxy host and port num.
I tried the method described in the following link
Getting through a Proxy with Sockets
But somehow I was unsuccessful to do so. Using the URLConnection approach it works i.e. when I do the

or its equivalent mentioned in link above.
But for sockets it doesn't work, although I am trying to connect to the http port only. I even tried reading the source code of URL and its related classes -> URLConnection, etc. but it looks like URLConnection doesn't use the socket abstraction (Socket class in Java) and instead uses native methods to connect thru OS level sockets. Correct me if I am wrong in my understanding.
In Java API doc, its written

An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.


Will this method work ? If yes how do we write custom socket factory ?
Thanks and regards,
Gopal
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a guess as to why Socket(host, port) fails with Connection refused. Does this make sense?
----
This is how I see the problem:
This works:
URL u = new URL(http://java.sun.com);
URLConnection uc = u.openConnection();

But this does not work:
InetAddress addr = new InetAddress(http://java.sun.com);
Socket s = new (addr, 80);
This is the exception:
java.net.ConnectException: Connection refused: connect
----
When I do this:
URL u = new URL(host);
URLConnection uc = u.openConnection();
System.out.println(uc.getClass());
I get this:
class sun.net.www.protocol.http.HttpURLConnection
----
Here is my explanation. Does this make sense?
sun.net.www.protocol.http.HttpURLConnection is a subclass of the abstract class URLConnection. Sun must implement the abstract method URLConnection.connect().
I am guessing, when Sun implements connect(), Sun is calling Socket(host, port) with host and port of the proxy server. Sun is negotiating with the proxy server using the HTTP protocol.
[ March 03, 2004: Message edited by: Marlene Miller ]
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic