Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneral sockets proxy server effectively working with keep-alive connections as well (Java)
    primarykey
    data
    text
    <p>I am trying to create a general TCP socket proxy server in Java - any connection that gets accepted on server socket is both-ways connected to another socket created to the intended target.</p> <p>I've googled a lot and not a single article, tutorial or any other piece of advice I've found gives solution to the basic problem I've encountered. First the backbone of the code:</p> <pre><code>is = socket1.getInputStream(); os = socket2.getOutputStream(); int read = 0; while ((read = is.read()) != -1) { os.write(read); } </code></pre> <p>The reason why this is not working as expected is this:</p> <p>Writing and reading from the sockets one-byte-at-a-time is slow. So I need to use BufferedInputStream/BufferedOutputStream or anything that does the same. When I use BufferedOutputStream, everything is just buffered and is only written when I tell it to be written (or when the buffer fills, which is not interesting case) by flushing or closing.</p> <p>Now it gets interesting. Pretty much every HTTP client <strong>doesn't actually close the connection/stream after sending a whole request</strong> (keep-alive connection). So I don't actually know <strong>when</strong> can I flush the output stream without reading the content of the transmission, determining if it's HTTP and if so, parse it's headers, pick the Content Length header and by that read only the specified amount of bytes of the body.</p> <ul> <li><p>First question: Am I right, or am I missing something and there is an easy way to overcome this?</p></li> <li><p>Second question: If I am right, is there any tool that would help me with the parsing? Parsing the HTTP communication by myself sounds like a very bad idea. But I don't need HttpServer or HttpClient - they offer way too much, I only need very little, and only sometimes (not all transmissions will be HTTP).</p></li> <li><p>Third question: Is there any way to do this much simpler if I restricted myself to only HTTP transmissions? Just accepting HTTP requests and redirecting them somewhere else (and responses back)?</p></li> </ul> <p>The intention here is to do this in Java 1.5 (this is a soft limit, but would be very nice) and keep the increase in response delay as low as possible (10 ms is OK, 50 ms sucks, 100 ms is unacceptable). I'll be grateful for any help.</p> <p><strong>CLARIFICATION:</strong> Maybe I wasn't clear enough, assuming by the first response. The issue here is that <strong>the read() method will not return -1 after one HTTP request, because the connection is kept open for another request ... so the loop doesn't end after that single request, the read() call hangs blocking and waiting for another input</strong>. Also, the buffered stream will not work without me calling flush(), but I don't know when to call it - don't know how to recognize end of HTTP request without parsing the content of the transmission.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload