• 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

Reading bytes from Socket?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 \n");
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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjit,
What can I say, other than goto http://examples.oreilly.com/javanp2/ and get the examples. Buy the book too
If I'm not mistaken chapter 12 has the src for a v.simple threaded http server.
This should give you the basis for your server program so that you can accept POST requests.
brgds
L
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic