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

  • Home
  • SEARCH
  • 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 6046747
In Process

The Archive Base Latest Questions

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

I have program where client peers communicate with each other via TCP-IP. When one

  • 0

I have program where client peers communicate with each other via TCP-IP. When one client does something he will signal other clients one by one that this happened. Here is a code I use to send data across:

public static string SendDirect(string data, string hostName, int portNumber)
        {
            string responseData;

            try
            {
                var client = new TcpClient(hostName, portNumber);

                Stream s = client.GetStream();
                var sw = new StreamWriter(s) { AutoFlush = true };

                sw.WriteLine(data);

                s.Close();
                client.Close();

                s.Dispose();
                sw.Dispose();

                responseData = "OK";
            }
            catch (SocketException ex)
            {
                responseData = ex.Message;
            }

            return responseData;
        }

Line

var client = new TcpClient(hostName,
portNumber);

can be very slow at times for some machines. For example, in my home network it takes like 2 or 3 seconds. Can you see how it’s real bad with 15 clients.

I was wondering how expensive or if even possible to not Close client every time and keep 30-40 of them open at all times? I assume some mechanism to check to make sure they alive and to make sure they all closed properly need to be coded but I wonder if idea itself is correct..

Thanks!

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

    Nothing should keep or limit you from creating more than one client/connection at a time. Actually initiating and closing tons of connections might trigger different security stuff (trying to fend of a possible DDOS attack or whatever). You might as well speed up the process resolving host names before and caching those. It doesn’t necessarily have to be the object creation that slows you down actually.

    The OS might throttle the number of pending connections per second (think 10 per second under Windows) but other than that there shouldn’t be any issues. You shouldn’t open/close connections for single commands anyway in my opinion. You should think about keeping both open, the TcpClient as well as the StreamWriter. Just ensure you flush once you’re done writing your packet. To improve performance you should think about manual flushing, especially if there’s is more than one command/packet to be sent to each client as each packet will take the minimum TCP window size (usually something around 1492-1500 bytes).

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

Sidebar

Related Questions

No related questions found

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.