Ritin,

The URLConnection object is a high-level abstraction for an
underlying socket connection, so you are effectively using
a socket.  Just briefly glancing over your code I didn't see any
glaring errors, but in my code (I do this sort of connection
frequently between applets/servlets) I typically:

- create the URL.
- Open an HTTP Socket Connection (cast the URLSocketConnection
     to HttpSocketConnection).
- set the headers/properties for the connection: input (true), output
(true), use-cache(false),
    and the reqeust method to "POST" or "GET" [note that this is a method of
the
    HTTPUrlConnection, and I don't think it is absolutely necessary as it
should default
    to "POST"]
- get an output stream on the connection (note that this will automatically
"connect" to
    the URL it is not already connected)
- write data to the output stream.
- close the output stream
- get input stream
- read data from input stream
- close input stream.

This appears to very similar to your code, and I definitely have got
this to work, so keep at it.  Note that when I need to send a cookie, I use
the "setRequestProperty" method of the connection, and it works.

Hope this helps,

Mike

----- Original Message -----
From: "Ritin Mathur" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, May 24, 2001 2:13 AM
Subject: Application - Servlet communication using sockets


> Hi guys,
>
> I have to make an application, which connects to a
> servlet over HTTP . I have
> to make the application interface in Swing. On
> clicking a button I have
> to generate cookies and send these cookies to the
> servlet. The communication
> between the application and the servlet has to be
> through sockets... The application
> also has to add its own headers to the request ...
>
> The servlet will accept the cookies from the
> application and will then
> send back an acknowledgement to the application along
> with its own
> cookies...the servlet will read the headers sent by
> the client application
> and send an acknowledgement to the application ...
>
> both the application and the servlet are running on
> the same machine , and
> i will be using servletrunner....
>
> maybe i can use the URLConnection class and make the
> connection to the servlet...
> but how do i create and send the cookies ?? and is
> making a connecton through the
> URLConnection class the same thing as using sockets ??
>
> The following is the code, that i THINK may work ...
>
> This is the code for the application client:
>
> **************
> try{
> URL url = new URL(http://localhost:servlet/myservlet);
> URLConnection urlcon = url.openConnection();
> urlcon.setAllowUserInteraction(true);
> urlcon.setDoInput(true);
> urlcon.setDoOutput(true);
> //urlcon.setRequestProperty("cookiename","cookievalue");
> System.out.println("calling the servlet...");
> urlcon.connect();
> }catch(MalformedURLException
> murle){System.out.println("MalformedURLException
> :"+murle);}}
>   catrch(IOException ioe){System.out.println("IO
> Excpeiton :"+ioe);}
>
> // write stuff to servlet
> PrintWriter toServlet = new
> PrintWriter(urlcon.getOutputStream());
> toServlet.print(cookiename);
> toServlet.print(cookievalue);
> toServlet.flush();
>
> //read stuff from servlet
> BufferedReader fromServlet = new BufferedReader(new
> InputStreamReader(urlcon.getInputStream()));
> String input;
> StringBuffer sb;
> while(input=fromServlet.readLine() != null)
> {
>   sb.append(input);
> }
>
> ****************
>
> In the above there are 2 ways that i can send the
> cookie to the servlet, one is by
> sending it as "RequestProperty" header and the other
> is by opening streams ...
>
> In the above code snippet i am getting the following
> exception :
>
>
> Calling the servlet...
> IO exceptionjava.net.ConnectException: Connection
> refused: no further information
>
>
> Will i have to use Socket objects for making the
> connection ?? With both the servlet and
> the application having server sockets and client
> sockets for communication ?? Please correct
> me where i am going wrong ...
>
> Thanks a lot for yr patience and time ... hope to get
> some ideas/pointers on this
>
> Regards,
>
> Ritin Mathur
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to