Reading bytes from Socket
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sanjit_Singh
Posted On:   Thursday, October 31, 2002 02:43 AM

Hi Everyone, I am trying to achieve something and I am not sure if it is the right way of doing it. My client program is as follows: ***********CLIENT********************************* URL url = new URL("http://localhost:12000"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); con.setUseCaches(false); String test = "Normally here would be contents of an XML file"; byte[] bytes = test.getBytes("UTF8"); OutputStream out = con.getOutputStream(); out   More>>

Hi Everyone,


I am trying to achieve something and I am not sure if it is the right way of doing it.

My client program is as follows:


***********CLIENT*********************************

URL url = new URL("http://localhost:12000");

HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoOutput(true);

con.setDoInput(true);

con.setRequestMethod("POST");

con.setUseCaches(false);



String test = "Normally here would be contents of an XML file";

byte[] bytes = test.getBytes("UTF8");



OutputStream out = con.getOutputStream();

out.write(bytes);

out.flush();

out.close();

con.disconnect();

**********************************************


Due to some reasons, the client code cannot be changed. I need to send the contents as a byte array over http using the POST method. Now, I am interested in creating my own Server which takes this post request and stores the contents in a string. Could someone kindly help me with this. I have written the following so far:


*************SERVER*******************************
ServerSocket serverSocket = new ServerSocket(12000);

System.out.println("HTTP Server (only POST implemented) is ready and is listening on Port Number 12000
");

Socket clientSocket = serverSocket.accept();

BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());

//kindly help here.code for manipulating the bytes. storing stuff in byte array and storing the contents received in a string

in.close();

clientSocket.close();

serverSocket.close();

*************************************************


Please note that I have tried the BufferedReader and readLine() method, but I do not receive anything and it doesnt seem to work. Any code would be very helpful. I am nterested in extracting the xml file that is in the body of the http post request and storing this in a string on the server.

Thanks in advance!

Sanjit

   <<Less

Re: Reading bytes from Socket

Posted By:   Bob_Lee  
Posted On:   Thursday, October 31, 2002 04:59 AM

In the client program modify the last line like this:

con.getInputStream();
con.disconnect();

It's a quirk in the API that it actually makes the connection when you invoke getInputStream().

Re: Reading bytes from Socket

Posted By:   eimi_nos  
Posted On:   Thursday, October 31, 2002 03:12 AM

Try the method



read(byte[] b, int off, int len)



of the class BufferedInputStream and the constructor



public String(byte[] bytes, String charsetName)



of the class java.lang.String.
About | Sitemap | Contact