Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:58:41+00:00 2026-05-27T01:58:41+00:00

Is it possible for a server to know that the client has finished sending

  • 0

Is it possible for a server to know that the client has finished sending it data and is now ready for a response – regardless (and without knowledge) of the application protocol?

Edit:
Think about creating a proxy sever. You can’t affect protocols, you don’t know the applicative details of each protocol (so you don’t know when a stream “ends”). Regarding “client may close the input stream” – that’s true, but apparently clients do not actually close the stream (has to do with reuse?). This is empirically tested with web browsers.

I may be barking on the wrong tree though – for that proxy server scenario, wouldn’t copying the stream to the output stream of the “other end” be enough? However, how would the other end “know” that the stream has ended?

See this program:

public class Test {
public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(8081, 100, InetAddress.getByName("localhost"));
    final Socket socket1 = new Socket("www.google.com", 80);
    final Socket socket = serverSocket.accept();
    new Thread(new Runnable() {
        public void run() {
            try {
                final InputStream inputStream = socket1.getInputStream();
                final OutputStream outputStream = socket.getOutputStream();
                readwrite(inputStream, outputStream, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    final InputStream inputStream = socket.getInputStream();
    final OutputStream outputStream1 = socket1.getOutputStream();
    readwrite(inputStream, outputStream1, true);

}

private static void readwrite(InputStream inputStream, OutputStream outputStream1, boolean x) throws IOException {
    final byte[] bytes = new byte[4];
    int read = -1;
    while ((read = inputStream.read(bytes)) != -1) {
        System.out.println((x?"s ":"r ") + new String(bytes));
        outputStream1.write(bytes, 0, read);
    }
}
}

Instead of doing what you’d think it would do (proxy for http://www.google.com) it just hangs. The thread that’s supposed to read from google and write to the local socket doesn’t detect anything on that input stream.

Edit2:
DOH!
put me in the stupid corner. Forgot to “start” the thread.
Back to my original question – how do I know the stream has ended?

Edit3:
Well, my problem was something completely different (had to do with POST, Transfer-Encoding and HttpClient-annoying-internal-cache-that-can’t-be-turned-off).
For fairness sake, I’ll accept the answer that answers the original question best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:58:42+00:00Added an answer on May 27, 2026 at 1:58 am

    If ‘finished’ means that inbound or outbound stream of TCP is closed (so called half close connection), then it is possible. Inbound stream is closed if inputStream.read(bytes) returns -1.

    If ‘finished’ means something else, then the answer is no.

    If you are going to implement a general purpose proxy, then you should wait data from both sockets at the same time. And write any incoming data to other socket.
    And you should also stop reading a socket, if the another socket is not writable. Or else you must buffer data. Proxy should also handle half closed connections.

    Don’t try to detect, when delivered protocol has message boundary, because some protocols may stream data to both directions all the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if it is possible to stream data from the server
Is it possible for a web server to know which type of device request
Does anyone know if it is possible to install SQL 2000 Reporting Services Server
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: Selecting data from two different servers in SQL Server How can I
I have a client that I'm wanting to move to a virtual private server.
I have an ASP.NET server that provides its client as an MSI download (similar
I need to know if a client of my web service has successfully received
I'm working on a little client that interfaces with a game server. The server
Possible Duplicate: Sending email through Gmail SMTP server with C# I would like my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.