Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Sockets and Internet Protocols and the fly likes Java Http Server and Java Http Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java Http Server and Java Http Client" Watch "Java Http Server and Java Http Client" New topic
Author

Java Http Server and Java Http Client

Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Hi,
I am trying to write my own java client and a java http server. My main aim is to send an XML document in the body of http and in return get a sequence ID from my own http server. The code for both client and server is as follows:
***********SERVER**********************
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12001);
System.out.println("HTTP Server (only POST implemented) is ready and is listening on Port Number 12001 \n");
while(true) {
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket.getInetAddress().toString() + " " + clientSocket.getPort());
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
OutputStream out = clientSocket.getOutputStream();
String temp;
while((temp=in.readLine()) != null)
System.out.println(temp);
String response = "HTTP/1.1 200 OK\n\r";
response = response + "Date: Fri, 04 May 2001 20:08:11 GMT\n\r";
response = response + "Server: Sanjits Server\n\r";
response = response + "Connection: close\n\r";
response = response + "1";
byte[] bytes = response.getBytes();
out.write(bytes);
out.flush();
in.close();
out.close();
}
} catch(Exception e) {
System.out.println("ERROR: " + e.getMessage());
System.exit(1);
}}}
********************************************************* **********CLIENT***************************************
public class Client {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:12001");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
String test = "<name>Hello</name>";
byte[] bytes = test.getBytes();
con.setRequestProperty("Content-length", String.valueOf(bytes.length));
con.setRequestProperty("Content-type", "text/html");
OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String temp;
while((temp = in.readLine()) != null)
System.out.println(temp);
out.close();
in.close();
con.disconnect();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);}}}
*********************************************************** The output on the server side is as follows
HTTP Server (only POST implemented) is ready and is HTTP Server (only POST implemented) is ready and is listening on Port Number 12001
127.0.0.1/127.0.0.1 1592
POST / HTTP/1.1
Content-length: 18
Content-type: text/html
User-Agent: Java1.3.1_04
Host: localhost:12001
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
************************
As you can see above, the http server simply "hangs" after reading the http header. It never reaches the http body and doesnt even process the response to the client. Does anyone have any ideas how to solve this? I am stuck and any help would be appreciated. Please feel free to try the code above. Thanks in advance!
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
Hej,
The HTTP protocol headers should be suffixed by \r\n, rather than \n\r.
Also, if data is being transmitted, then the HTTP header-field "Content-Length" should be populated.
That aside, your client code

doesn't contain a line terminator does it, so in.readLine() is probably blocking until such time as a line terminator is read.
brgds
L


I have no java certifications. This makes me a bad programmer. Ignore my post.
Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Hi,
Thanks a lot for your reply.
1. As mentioned by you, I tried \r\n in my response string and then somehow I don�t receive anything on the client side. I only receive a response on the client side with "\n\r".. . I dont know why.
2. I put a line terminator (\n, \r, \r\n) in the client code and it still doesn�t work.
Any other ideas. Thanks!
SS
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Have another look at your server's readLine loop -- it will loop until the method returns null, i.e. until your client close()s the connection.
Compare this with the client's request code. It tries to read back the server response before it ever comes around to close()ing the request.
Deadlock.
Before you implement a request/response protocol, however simple, never start with code. Start with a blank sheet of paper, and get exactly clear how the protocol works, covering all eventualities and always paying special attention to the questions "how does the server know the request has finished?" and "how does the client know the response has finished?"
- Peter
Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Hi Peter,
Thanks a lot for your reply. I realize what you just said, but the strange thing is that if I put a out.close() method after out.flush(), then the message is received successfully on the server with the body, but the client and the server crashes (i.e. exceptions). I can imagine that out.close() releases some resources, including the connection. If you are interested, kindly try it out. I am confused. How can I simply send a series of bytes to the server and when the server receives all of it, then it sends a series of bytes back to the client. How can I read and write them? Any example would be great!
Thanks!
SS
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214

How can I simply send a series of bytes to the server and when the server receives all of it, then it sends a series of bytes back to the client. How can I read and write them? Any example would be great!

It's pretty unrealistic to expect to be able to send bunch of bytes and receive them at the other end, and then to send some acknowledgement back.
The key question would be How do you know exactly how many bytes there are to receive?
If you're going to stick with your existing HTTP example, the server knows how much data there is to read, because of the "Content-Length" header, read that much data, and then write some data back.
If you really are going to write your own HTTP server, then I suggest that you read (at least) RFC1945 and RFC2068... If I was harsh (sometimes I am), I'd go so far as to say, if you don't understand either RFC, then you've probably shouldn't be implementing your own HTTP server .
If you really want a shortcut to understanding HTTP, you could try http://www.jmarshall.com/easy/http/ which is pretty good.
For your initial problem, getting a sequence number, it might be sufficient to write a servlet and host it under tomcat or somesuch, if that's not enough then maybe you could try customising some freeware server like jetty (from mortbay?), or jigsaw from www.w3c.org

L
 
Don't get me started about those stupid light bulbs.
 
subject: Java Http Server and Java Http Client
 
Similar Threads
Reading bytes from Socket?
On The Fly Internationalization
URGENT: Transferring Zip Files
HTTP Client/Server Problem
Terminating Threads