wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Socket to HTTP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket to HTTP" Watch "Socket to HTTP" New topic
Author

Socket to HTTP

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Hi,
Is following senario possible?: If yes,how?
Java client(using java program) open SOCKET to Java Server(which consist of httpserver). Whenever java client send request to server, server must get request through HTTPREQUEST and send response back to client using HTTPRESPONSE and client should get response from server using SOCKET listener.
Thanks
Angela
P.S. Capital word are must for the senario.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
You can most definitely do that.
It will mean that you must learn the HTTP protocol, ie. you will be "manually" writing HTTP HEAD/GET/POST etc requests.
The server will of course take care of sending you back the correct HTTP response, you just read it and process it however you want.
Here's a nice resource for an introduction to the HTTP protocol.
http://www.jmarshall.com/easy/http/
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Originally posted by James Swan:

The server will of course take care of sending you back the correct HTTP response, you just read it and process it however you want.

How server will receive HTTP request & send HTTP response i.e using socket? I don't want to send HTTPresponse and receive HTTPRequest using socket interface on server side.
Thanks,
Angela
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Angela can you explain your requirements in more detail?
Are you responsible for only creating the client or do you also have to create the server?
My assumption was:
- you were creating a client to send HTTP requests to an existing HTTP/Application server
If this is the case then the server already knows how to process HTTP requests/responses and will be listening for them on a defined port (usually 80).
If you have to write the server yourself then it is a bit more involved, but that link I provided is still useful.
Please provide more details on what you're going to be doing.
James.
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Originally posted by James Swan:

Are you responsible for only creating the client or do you also have to create the server?

I have to create client only. I already have existing Java HTTP Server. All I have to do is to create client socket program to send request and get response from the existing HTTPServer.
I don't need to write any socket code on serverside to do HTTP request and HTTP response.
As it is HTTP server, it will do through HTTP
Am I right???

Thanks a lot,
Angela
Michael Bronshteyn
Ranch Hand

Joined: Mar 26, 2002
Posts: 85
if you use raw socket you will have to form http request yourself.
you can use httpclient ( check apache jakarta commons httpclient ) to communicate from client side to the server without need of forming http request yourself. avoid using java.net default httpclient because it caches request content into buffer ( at least in jdk1.3 ), rather than establishing communication channel with server.


Michael
SCJP2
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Since you are only creating the client it is relatively straight forward.
Here are the high level steps that your client will need to perform:
1) Open a socket connection to your HTTP/Application server (eg www.myserver.com, port 80)
2) Write to the socket your HTTP request (see the 1st link I sent you), I'm assuming you're wanting to do something non-trivial, for example calling a servlet/jsp/cgi with some parameters, this is achieved by performing a GET or POST request (again see the link)
3) Read the response from the socket, the server "knows" HTTP and will respond appropriately
4) Process the "message" in the response
5) close the socket, done.
It is no different from any other client/server setup using sockets, the only difference is format of the messages being passed back and forth, in this case HTTP.
James.
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thanks a lot I got it. I will write the following code on client side:
1) Open a socket connection to your HTTP/Application server (eg www.myserver.com, port 80)
2) Write to the socket your HTTP request (see the 1st link I sent you), I'm assuming you're wanting to do something non-trivial, for example calling a servlet/jsp/cgi with some parameters, this is achieved by performing a GET or POST request (again see the link)
3) Read the response from the socket, the server "knows" HTTP and will respond appropriately
4) Process the "message" in the response
5) close the socket, done.
On Serverside:
No code for socket. As it is HTTP Server.
Thanks,
Angela
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
well, you will have to remember to strip of the headers @ the client end, off the response from the server. You will also have to make sure you are not behind a firewall on the client side.
Regds
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket to HTTP