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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:31:05+00:00 2026-05-26T07:31:05+00:00

I made a TCP server which communicates with multiple clients at once, but I

  • 0

I made a TCP server which communicates with multiple clients at once, but I can’t seem to be able to make them stable. When one of the client sends 100 packets to the server, the server receives only a few of them.

Here’s the client code in PasteBin. It shows how the client connects to the server and then sends 100 messages in a For loop to the server.

And here’s how the server handles the connection. I couldn’t paste the full source as it’s hundreds of lines long so let me know if it’s missing any mandatory parts and I’ll upload them as well.

  • 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-26T07:31:05+00:00Added an answer on May 26, 2026 at 7:31 am

    I answered your question on reddit, as well, but figured I’d reply here in case someone else is searching for this kind of thing.

    You’ve got two problems with the way your server handles the reads. First of all, your client object’s constructor:

    Public Sub New(ByVal client As TcpClient)
        'New client connects
        Me.client = client
        client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
    End Sub
    

    BeginRead expects you to be collecting your data in a way that can be passed to the callback method; “Read” in this instance. Typically, that’s done by creating a new object to hold the “State” of this async operation, and passing it in as the last parameter to BeginRead. Your method call here is creating a new byte array, but isn’t holding a reference to it to pass in as the last argument to the method. That means that the data that gets read by this call will just disappear after being read, since it never gets passed into a method to store it.

    Secondly, your Read operation:

    Public Sub Read(ByVal ar As IAsyncResult)
        Dim reader As New StreamReader(client.GetStream())
        clientPacket &= reader.ReadLine()
        client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
    End Sub
    

    Since you’re not passing in the data that was read in the async call, you’re creating a new StreamReader here to pull more data in from the client. This is not an asynchronous method call. Your reader.ReadLine() call will block until a newline is encountered, at which point the data will be appended to clientPacket. You then call BeginRead again, with the same issue as noted above, meaning you’ll lose more data.

    In addition, you’re never clearing your AsyncResult objects by invoking EndRead() on the stream, which will eventually result in resource starvation when the CLR runs out of worker threads for your async operations.

    Here’s an example of the way I’d implement this kind of task. It’s in C# since that’s what I’m most comfortable with, so sorry about that. 😉

    Client Code

    Server Code

    I hope this helps!

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

Sidebar

Related Questions

I have made a TCP server which I have been testing locally and it
I've made a tcp server witch spawns a process to listen incoming connections. Here
I saw just what I needed at msdn to create my TCP server,but there
I made a custom server that accepts TCP connections on a certain port. I
I made a program that connects to a server (which I also wrote) via
If I make multiple HTTP Get Requests to the same server and get HTTP
I'm using Java to create a client/server application that communicates using TCP. The network
I'm working on a simple pair of TCP server and client, which are running
I made a client & server that establishes a TCP connection through sockets, I'm
I'm writing a server which is accepting incoming TCP connections. Let's suppose the server

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.