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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:34:06+00:00 2026-05-25T17:34:06+00:00

I have a quick and dirty question. So as it stands, i have two

  • 0

I have a quick and dirty question. So as it stands, i have two clients and a server running. I can communicate messages from the clients to the server without any problem. my problem appears when i want to read two messages from the client – rather than just one message.

The error which i receive is: IOException was unhandled. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Here is my code on the server side:

private static void HandleClientComm(object client)
{
/** creating a list which contains DatabaseFile objects **/
List theDatabase = new List();

        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        do
        {
            bytesRead = 0;

            try
            {
                // Blocks until a client sends a message                    
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch (Exception)
            {
                // A socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                // The client has disconnected from the server
                break;
            }

            // Message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();

            Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
            Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
            Console.WriteLine(encoder.GetString(message, 0, bytesRead));


            if (encoder.GetString(message, 0, bytesRead) == "OptionOneInsert")
            {
                byte[] message2 = new byte[4096];
                int bytesRead2 = 0;

                **bytesRead2 = clientStream.Read(message, 0, 4096);** //ERROR occurs here!

                Console.WriteLine("Attempting to go inside insert)");
                Menu.Insert(theDatabase, bytesRead2); 
            }

Here is my client code:

        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = encoder.GetBytes("OptionOneInsert");

        Console.ReadLine(); 
        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();

        NetworkStream clientStream2 = client.GetStream();
        String text = System.IO.File.ReadAllText("FirstNames.txt");
        clientStream2.Write(buffer, 0, buffer.Length);
        clientStream2.Flush();
        ASCIIEncoding encoder2 = new ASCIIEncoding();

        byte[] buffer2 = encoder2.GetBytes(text);
        Console.WriteLine("buffer is filled with content"); 
        Console.ReadLine();

When the client sends the message “optionOne” it is received by the server just fine. It’s only when i attempt to send the string called “text” that the issues appears!

Any help would be greatly appreciated – I’m not all that familiar with Sockets, hence i’ve been struggling with trying to understand this for sometime now

  • 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-25T17:34:07+00:00Added an answer on May 25, 2026 at 5:34 pm

    You’ve got a big problem here – there’s nothing to specify the end of one message and the start of another. It’s quite possible that the server will receive two messages in one go, or half a message and then the other half.

    The simplest way of avoiding that is to prefix each message with the number of bytes in it, e.g. as a fixed four-byte format. So to send a message you would:

    • Encoding it from a string to bytes (ideally using UTF-8 instead of ASCII unless you’re sure you’ll never need any non-ASCII text)
    • Write out the length of the byte array as a four-byte value
    • Write out the content

    On the server:

    • Read four bytes (looping if necessary – there’s no guarantee you’d even read those four bytes together, although you almost certainly will)
    • Convert the four bytes into an integer
    • Allocate a byte array of that size
    • Loop round, reading from “the current position” to the end of the buffer until you’ve filled the buffer
    • Convert the buffer into a string

    Another alternative is simply to use BinaryReader and BinaryWriter – the ReadString and WriteString use length-prefixing, admittedly in a slightly different form.

    Another alternative you could use is to have a delimiter (e.g. carriage-return) but that means you’ll need to add escaping in if you ever need to include the delimiter in the text to transmit.

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

Sidebar

Related Questions

I have quick question for you SQL gurus. I have existing tables without primary
I have a quick question about fetching results from a weakly typed cursor and
I'm looking for a quick-and-dirty solution to this, I have to set up a
I have a quick question about encapsulating specific types with typedef . Say I
I have two quick questions: When do two file descriptors point to the same
I have a quick question regarding debug and release in VS 2008. I have
I just have a quick question about how to get the length of a
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I have a JTable in a swing application. I wrote a quick and dirty
I'm using JAXB for quick-and-dirty XML generation. I have a class that represents a

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.