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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:06:57+00:00 2026-05-22T15:06:57+00:00

I’m comunicationg with a email gateway. That gateway has an specific ip and port.

  • 0

I’m comunicationg with a email gateway. That gateway has an specific ip and port.

The requests the gateway are JSON formated and the gateway normally responds first whith an proceeding state and then with a confirmation or error state, represented also in JSON.

The code to make the requests and receive the response is:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;
using System.Threading;

using Microsoft.Win32;

public class TcpClientSample
{
    public static void SendMessage(TcpClient client, string msg)
    {
        Console.WriteLine("REQUEST:" + msg);

        NetworkStream stream = client.GetStream();

        byte[] myWriteBuffer = Encoding.ASCII.GetBytes(msg);
        stream.Write(myWriteBuffer, 0, myWriteBuffer.Length);

        byte[] myWriteBuffer2 = Encoding.ASCII.GetBytes("\r\n");
        stream.Write(myWriteBuffer2, 0, myWriteBuffer2.Length);

        string gResponse = "";
        BinaryReader r = new BinaryReader(stream);        

        int receivedMessages = 0;
        while (true)
        {     
            while (true)
            {
                char currentChar = r.ReadChar();
                if (currentChar == '\n')
                    break;
                else
                    gResponse = gResponse + currentChar;
            }           

            if (gResponse != "")
            {
                Console.WriteLine("RESPONSE:" + gResponse);
                receivedMessages = receivedMessages + 1;    
            }

            if (receivedMessages == 2)
            {   
                break;
            }

        }        
    }

    public static void Main()
    {
        List<string> messages = new List<string>();        

        for (int i = 0; i < 1; i++)
        {
            String msg = "{ \"user\" : \"James\", \"email\" : \"james@domain.pt\" }";
            messages.Add(msg);
        }

        TcpClient client = new TcpClient();

        client.Connect("someIp", somePort);

        int sentMessages = 0;
        int receivedMessages = 0;
        foreach (string msg in messages)
        {
            Thread newThread = new Thread(() =>
            {
                sentMessages = sentMessages + 1;
                Console.WriteLine("SENT MESSAGES: " + sentMessages);
                SendMessage(client, msg);
                receivedMessages = receivedMessages + 1;
                Console.WriteLine("RECEIVED MESSAGES: " + receivedMessages);                
            });            
            newThread.Start();

        }        

        Console.ReadLine();
    }
}

If I send few emails (up to 10) the network stream is OK.

But if I send thousands of emails I get messed chars lie

:{iyo”asn ooyes” “ncd” 0,”s_d:”4379″ nme” 92729,”er_u” ,”ed_t_i” 2#” p cin_d:”921891010-11:11.725,”s” 4663175D0105E6912ADAAFFF6FDA393367″ rpy:”rcein”

Why is this?

Don’t worry I’m not a spammer 😀

  • 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-22T15:06:58+00:00Added an answer on May 22, 2026 at 3:06 pm

    I solved this issue by having a single method just to read from the stream like this:

    private TcpClient client;
    private NetworkStream stream;
    
    public void ListenFromGateway()
    {
       ...
       while (true)
          {
             byte[] bytes = new byte[client.ReceiveBufferSize];
    
             //BLOCKS UNTIL AT LEAST ONE BYTE IS READ
             stream.Read(bytes, 0, (int)client.ReceiveBufferSize);
    
             //RETURNS THE DATA RECEIVED 
             string returndata = Encoding.UTF8.GetString(bytes);
    
             //REMOVE THE EXCEDING CHARACTERS STARTING ON \r
             string returndata = returndata.Remove(returndata.IndexOf('\r'));
    
             ...
    }
    

    Thanks for the help

    • 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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
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 have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping 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.