Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServerSocket Problem  RSS feed

 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with socket programing and hope it's possible to solve.
I use java.net.ServerSocket for running a simple http server, and (for example) it listens on port '1313', it works well, it receives and sends Streams as well as "new String()". but there is a problem.
how can I (is it possible) to handle cookie from incoming Socket object? I want send the response with meta information, something like setContentHeader(setHeaderContent) in Servlets. or specify response type. I don't know, anything that I can handle cookies.

sorry I cannot explain it clearly, I thing the following code snipped would help.



thanks. wish you a perfect day friends.
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you would need to implement something that can parse the HTTP protocol http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

which is basically reading the header lines
then as many lines for headers until a line of only \r\n

and the response renders headers, ending with a \r\n, and then the content.

that painful \r\n thing is actually somehow in the spec. http://tools.ietf.org/html/rfc2616

where, cookies are just another http header

the general format is

Set-Cookie: key=value; key2=value2;

where there are some pre-defined and 'standard' key/value types typically in use and have certain meanings with modern browsers

http://en.wikipedia.org/wiki/HTTP_cookie
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!