TCPClient in C# (Error)

TCPClient in C# (Error)

By : user3854135
Date : January 02 2021, 06:48 AM
I think the issue was by ths following , I tried the code you have posted. It works just fine.
I think, either the domain name Python.org is not resolvable (possible if you are behind a firewall or are using a proxy server) or a firewall is blocking the connection or just that the request is going to the wrong computer.
code :

Share : facebook icon twitter icon
TcpClient.GetStream().Read() vs. TcpClient.Client.Receive()

TcpClient.GetStream().Read() vs. TcpClient.Client.Receive()

By : user1568506
Date : March 29 2020, 07:55 AM
will be helpful for those in need There is, in fact, a pretty clear benefit of using the first option (TcpStream and not Socket). The benefit is that stream API is more flexible when different underlying implementations are needed at for the same program.
For example, a code which sometimes may use SSL and sometimes may not use it, can switch between SslStream and TcpStream with no changes to the calling code. This is something which is much harder to accomplish using only plain Socket API.
TcpClient.BeginRead/TcpClient.EndRead doesn't throw exception when internet disconnected

TcpClient.BeginRead/TcpClient.EndRead doesn't throw exception when internet disconnected

By : Deepak Joshi
Date : March 29 2020, 07:55 AM
I wish this help you The system generally has no chance to know that connection is broken. The only reliable way to know this is to attempt to send something. When you do this, the packet is sent, then lost or bounced and your system knows that connection is no longer available, and reports the problem back to you by error code or exception (depending on environment). Reading is usually not enough cause reading only checks the state of input buffer, and doesn't send the packet to the remote side.
The inverse of TcpClient.GetStream - Getting The TcpClient associated with a given NetworkStream

The inverse of TcpClient.GetStream - Getting The TcpClient associated with a given NetworkStream

By : Jörg'l
Date : March 29 2020, 07:55 AM
help you fix your problem There's no such function, at least none that I know of. Stream.Length is meaningless on a network stream, because you never know the remaining amount of data. That said, you're probably having a conceptual error there: The network stream just delivers data, there's no way to know how much data is left in the stream, because the other end can continuously send data on it, theoretically forever. If you want to know the size of i.e. a file you're downloading, you'll have to parse the contents of the network stream, assuming the protocol in use does send such an information.
C# TcpClient, Can following setup Deadlock (2 seperate programs communicating via TcpClient)

C# TcpClient, Can following setup Deadlock (2 seperate programs communicating via TcpClient)

By : Sunilkumar Basappa
Date : March 29 2020, 07:55 AM
help you fix your problem Based on the feedback, it seems my issue is that I risk blocking when two communicating programs both have their sending messages blocked. So I moved the sending function to a seperate class now.
Following is the class I decided to use. It should be started up once ready for use, and can be called with a target TcpClient and a message, the call can be done either Blocking or Nonblocking (Nonblocking returns true is message is accepted)
code :
    public class TcpSender
        Mutex moveToInternal = new Mutex();
        Mutex startOnce = new Mutex();

        List<Message> internalMessages = new List<Message>();
        List<Message> externalMessages = new List<Message>();

        Thread mainLoop;

        String status = "stopped";

        //returns true if started.
        public Boolean Start()
            if (startOnce.WaitOne(0))
                status = "started";
                mainLoop = new Thread(new ThreadStart(InternalMainLoop));
                mainLoop.IsBackground = true;
                return true;
            return false;

        private void InternalMainLoop()

                while (status == "started")
                    Boolean didSomething = false;

                    //pickup new messages and move to internal buffer
                    if (externalMessages.Count > 0)
                        didSomething = true;
                        if (moveToInternal.WaitOne())

                    //deliver messages
                    for (int i = 0; i < internalMessages.Count; i++)
                        //send request
                        Byte[] data = System.Text.Encoding.ASCII.GetBytes(internalMessages[i].GetMessage());
                        NetworkStream stream = internalMessages[i].GetClient().GetStream();
                        stream.Write(data, 0, data.Length);

                    if (!didSomething) Thread.Sleep(1); //Nothing to receive or send, maybe tomorr... next milisec there is something to do

        public void Stop()
            status = "stopped";

        public Boolean Status()
            return (status == "started");

        //return true if message was accepted
        public Boolean addMessageNonBlocking(TcpClient client, String message)
            Boolean gotMutex = false;
            if(gotMutex = moveToInternal.WaitOne(0));
                Message tmpMessage = new Message(client, message);
            return gotMutex;

        public void addMessageBlocking(TcpClient client, String message)
            //loop until message is delivered
            while (!addMessageNonBlocking(client, message))
                Thread.Sleep(1); //could skip this, but no need for busy waiting

        private class Message
            TcpClient client;
            String message;

            public Message(TcpClient client, String message)
                this.client = client;
                this.message = message;

            public TcpClient GetClient()
                return client;

            public String GetMessage()
                return message;
int i = 0;
String testmsg = "This is a 25 char test!!!";
Byte[] data = System.Text.Encoding.ASCII.GetBytes(testmsg);
while (true)
    testClient.GetStream().Write(data, 0, data.Length);
    if (i % 10 == 0) Console.WriteLine("Cycle Count: " + i);
Does TcpClient.Dispose() closes the TcpClient.GetStream?

Does TcpClient.Dispose() closes the TcpClient.GetStream?

By : Dixit Patel
Date : March 29 2020, 07:55 AM
seems to work fine For closing a TcpClient it's necessary to close the stream. And the usual way of doing that is: , Close calls Dispose, Dispose disposes the stream:
code :
IDisposable dataStream = m_DataStream;
if (dataStream != null)
Related Posts Related Posts :
  • Looking for a .Net ORM
  • Cost of SortedDictionary.Count
  • All possible combinations of n items selected randomly from a set of x items (algorithm)
  • Displaying process percentage on DOS screen
  • C# Importing Large Volume of Data from CSV to Database
  • c# control location precision
  • Printing to Zebra printer gives faint and blurry images
  • How to avoid stepping into dll code file?
  • Programmaticaly prevent Vista desktop search (WDS) from indexing pst files placed on mapped network drives
  • Is there anyway to reduce IsolatedStorage capacity in Silverlight?
  • C#: How to print a unicode string to console?
  • Frequency Table with Random Values
  • My Visual Studio 2008 web application keeps throwing a .Net error when I first run it, but refreshing fixes it
  • TCP Connection Creation and Closing Event Hooking
  • Find changed properties of a class
  • C#: Object with custom marshaller not containing data after PInvoke call
  • Version Control System with API. Need to get metrics
  • Performance comparison of Dictionaries
  • How to get process name and title of the top window on Windows / C#
  • How to save the ListView contents to a text file?
  • Why not .NET-style delegates rather than closures in Java?
  • Get CLSID by PIA interface Type
  • Visual Studio 2008 C# - Recover solution from application manifest possible?
  • Is a good practice to initialize private data members from within ctor by calling the associated properties instead of t
  • Inserting into a bitstream
  • How do you write byte[] array using log4.net
  • Clickonce + .NET client profile 4 framework + offline
  • Show form that showDialog from mainForm won't block it, but closing mainForm will close it
  • Developing a GPS car tracking system
  • dynamic log4net appender name?
  • Visual studio formatting issue
  • IndexOutOfRangeException when a stream is a multiple of the buffer size
  • How to deal with calculated values with Dependency Properties on a custom WPF control
  • counting from textbox,asp.net
  • Threading best practice when using SFTP in C#
  • how to load PDF file in website page using C#
  • Parent child class relationship design pattern
  • MessageQueue.BeginReceive() null ref error - c#
  • A C# collection, which behaves like C++ set or priority_queue?
  • Animating WPF buttons from inside a style
  • WPF Grid : GridSplitter event
  • Server 2008 Task Scheduler Mapped Drive Access C#
  • Cast problem with LINQ
  • Combo box Item source = ObservableCollection & I need a '-None-' dummy entry at the top
  • Which GUI control is suitable for this purpose?
  • Trying to access App.config file for mail settings but fails to work
  • Invoke PowerShell script from c# in runspace and get line number where error occured
  • How to show html contents with a RichTextBox?
  • FTP monitoring and downloading of new files
  • MEF + Plug-In not updating
  • Efficient handling of super wide, but not so tall, bitmap?
  • .NET 3.0 Unit Testing getting System.MethodAccessException calling .NET 2.0
  • Web Services vs Persistent Sockets
  • How do I dynamically reference incremented properties in C#?
  • Windows Installer (C#) error code 2869
  • Exception "The operation is not valid for the state of the transaction" using TransactionScope
  • Tools for listing database accesses from .NET code
  • Excel VSTO set selection
  • Creating a subscription based website in ASP.NET
  • Merging Resources Dictionaries
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk