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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:01:27+00:00 2026-06-11T09:01:27+00:00

I have a TCP socket client-server program in Java. From the client, I send

  • 0

I have a TCP socket client-server program in Java.

From the client, I send several times per sencond a binary message (binary data from a byte[]).

From the server, I want to read those messages in the same order and size.

The problem is that the server apparently dont always read the messages in the same size. It seems to “group” them. For example:

The client sends:
12 bytes – 12 bytes – 12 bytes – 12 bytes – 15 bytes – 15 bytes

The server receives:
12 bytes – 48 bytes – 15 bytes – 15 bytes

How can I prevent this?

Right now, my code is:

Client:

(initialization)
mySocket = new Socket(direccion, puerto);
mySocket.setTcpNoDelay(true);
BufferedOutputStream os = new BufferedOutputStream( mySocket.getOutputStream() );

(sending)
os.write( bytes );
os.flush();

Server

(initialization)
serverSocket = new ServerSocket(12321);
Socket con = serverSocket.accept();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

(receiving)
char[] msg = new char[1024];
String mensaje;
int nbytes;
nbytes = reader.read(msg);

I cant manually separate messages that come grouped, because they are binary messages, and this module cant “understand” them.

So, the question is: How can I assure that the reader gets the messages individually?

Thanx.

  • 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-11T09:01:29+00:00Added an answer on June 11, 2026 at 9:01 am

    You are using a Stream of data to transport your messages. A stream is conceptionally just a sequence of bytes – there are no messages. You need to take care of splitting the stream into messages/packets yourself.
    You could even receive part of a message with one read-call and the next part with the next call in your current code. As it is now your code is fundamentally flawed.

    Introduce a mechanism that allows clear detection where a message starts/ends. A simple approach would be to just encode the length of each packet into the first byte(s) of each packet. The receiver can then read the length first and after that it knows how many of the following bytes belong to that packet.

    Another approach is to have a message separator symbol that signals the end of the current packet. Since you are sending binary data, this would require that the packets not contain all possible byte values or you need encode the packet in a way that it doesn’t use every possible byte value.

    The approach with the length is much simpler to implement, while the end symbol approach would be useful for schemes where you easily can introduce a new symbol (e.g. if the entire packet were huffman encoded).

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

Sidebar

Related Questions

I have developed a Java server using Eclipse that accepts TCP socket connection from
I have a TCP socket client receiving messages (data) from a server. messages are
I have a C/S program. Client use socket to send a file to server,
In sockets I have written the client server program. First I tried to send
I have an java application using Socket TCP/IP and GUI. Server always listens connection
I have a tcp client - server implementation running in the same program, on
I am trying to implemante a client- server program communication using TCP socket programming
I have a simple TCP connection with a server and a client program. I
I have a tcp chat program: server.c and client.c . The server is in
I have a server-client application [TCP sockets, .NET 4.0].. the application about : do

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.