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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:23:12+00:00 2026-06-09T21:23:12+00:00

We’re developing a server application in Java (1.6) which is a transactional server, that

  • 0

We’re developing a server application in Java (1.6) which is a transactional server, that listen for connections through TCP sockets. Each new connections create a new thread that keeps alive until the connection is closed. Each client will send transactions to the server that will be processed and then the response is sent back to the client.

This works fine. Problem now arises when we want to send many async transactions (or messages) through the same socket. I’ve written a small application that send 1000 transactions with an interval of 10 ms between each transaction. The application is async, so messages are sent and the responses comes in the middle.

This is the code from the part that handles the incomming messages and send them to another component to be processed (this component has a thread pool):

public void run() {
...
...

  socketBuf = new BufferedInputStream(input);
  baos = new ByteArrayOutputStream();

  while ((bytes_read = socketBuf.read(buffer)) != -1) {

    if (bytes_read < 0) {
        log.error("Tried to read from socket, read() returned < 0,  Closing socket.");
        return;
    }

    baos.write(buffer, 0, bytes_read);
    break;
  }

  if (bytes_read >= 0) {

    baos.flush();
    byte data[] = baos.toByteArray();

    if (data.length > 0) {                      
        GWTranData tData = posMessage.decode(data, false);   
        if (tData.getMessageType() > 0) {

            // Send to the Pre-Online Manager to be processed                       
            PreOnlineJob newJob = new PreOnlineJob(tData);
            newJob.addJobStatusListener(this);
            GWServer.getPreOnlineInstance().addJob(newJob);
        }
    }
  }
  else {
    clientSocket.close();
    break;
  }

} while(true);
  } 

The problem we’re facing when sending many transactions in a brief time is that some messages are lost and don’t reach the server. Doing a deep analysis, we’ve found that when messages are sent too fast, there’re more than one message in the buffer, so data[] has two or more messages but only one will be performed. The size of the message sent is 200 bytes, so a buffer of 512 is more than enough.

Is there any problem with the way I’ve implemented the socket read? Is there a better way?

Thanks guys.

  • 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-09T21:23:14+00:00Added an answer on June 9, 2026 at 9:23 pm

    The problem is with the way you consume bytes read from the socket. Your assumption is that you get one “message” per read. That assumption is wrong – TCP does not know about your application message boundaries, but gives you a stream of bytes, so you can get several messages at once, or a part of a message, or both.

    You have to buffer unprocessed part of the received stream, check if you got complete message, read some more until you do, process the message, and continue in a loop.

    Edit 0:

    There are several ways to design your application-level protocol on top of TCP:

    • fixed length messages (easy),
    • delimited messages (needs explicit byte-sequence to designate end/start of message, like SOH in FIX, or \r\n in HTTP),
    • length-prefixed messages as @Thomas suggests in comments,
    • “self-describing” messages – like s-expressions or what not, need to be parsed,
    • maybe others.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.