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 8122135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:44:00+00:00 2026-06-06T05:44:00+00:00

In my Java application there is a Socket open and data is being read

  • 0

In my Java application there is a Socket open and data is being read from its InputStream.

In optimal conditions each packet that comes in results in a call to read() returning its application layer data. This is what I am trying to get – one ‘message’ per packet.

However, it is possible that data gets buffered in the socket based on what getReceiveBufferSize() returns. If this happens and the stream is read from, there may be data from multiple packets sitting there.

Is there another method to get data from individual packets, or does that violate layers of abstraction?
Alternatively, is it possible to set some sort of delimiter for when data gets appended to the buffer? Or is that something at the platform level that cannot be done?

Here is some example code to demonstrate what I am doing:

import java.io.InputStream;
import java.net.Socket;

public class Belnets {


public Belnets() {
    try{
        Socket s = new Socket("address", 23);
        System.out.println("platform will buffer this much: "+s.getReceiveBufferSize());
        InputStream sin = s.getInputStream();
        byte[] data = new byte[1024];
        int c;
        while(true){
            c = sin.read(data);
            for(int i=0; i<c; i++){
                System.out.print((char)data[i]);
            }
            System.out.println("=END OF READ=");

            //if you dont do this, read() seems to return once for each packet as this loop 
            //appears to be running fast enough to keep up with the rate of data that comes in.
            //otherwise data gets buffered and you get multiple packets worth of data before seeing
            //=END OF READ=
            Thread.sleep(1000);     

        }

    }catch(Exception e){
        e.printStackTrace();
    }
}   



public static void main(String args[]){
    new Belnets();
}

}
  • 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-06-06T05:44:01+00:00Added an answer on June 6, 2026 at 5:44 am

    The Java API doesn’t provide any way to read TCP packets. It’s stream-based, and you have absolutely no way to read one packet at a time.

    If you want to send and receive multiple messages, you need to implement an application-level protocol, which might use a specific delimiter between messages, or send the length of a message followed by the message body itself, for example.

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

Sidebar

Related Questions

I have a Java (6) application which uses Hibernate (V3.3.2) to read data from
Is it possible to run JUnit tests from inside my Java application? Are there
Is there any recommended Java application profiling tutorial? I am now using JProfiler and
Hi I have created a java application. I found there is an application menu
I've created a simple console Java application that is built with Maven. Is there
is there a way to call java me application out of Symbian's C++ app?
Is there a way to write a Java Swing application with a custom chrome?
In our legacy Java EE application, there are loads of value object (VO) classes
When designing an distributed application in Java there seem to be a few technologies
I developed a java web application which contains Wallpapers, there is like button for

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.