Michurin
Michurin

Reputation: 3

C# TcpClient connection and multiple threads

I am new to TCP programming so here is a few simple questions:

  1. Client app is calling a listener. I am opening a connection with TcpClient:

    TcpClient myTcp = new TcpClient("Server", 1000);
    myTcp.connect();
    

    For how long will myTcp hold a connection? Is there any default value? Can I change it?

  2. If my calling application has a multiple threads (it's a WCF service), each of threads has to call a listener. Can I create myTcp as a singleton and reuse it in multiple threads?

  3. If question 2 yes, how many simultaneous connections can myTcp handle?

  4. With singleton approach is there a chance to have synchronization issue between calls and responses?

Upvotes: 0

Views: 3447

Answers (1)

Josh
Josh

Reputation: 69242

As far as the server goes, if it's a WCF service then WCF will take care of the threading concerns for you. You have a fair bit of control over the threading behavior of the service. But I suspect that's not what you're asking about.

On the client, technically you can access a TcpClient or Socket from multiple threads but you cannot read or write to it concurrently so for all intents and purposes, no don't try to share a TcpClient across threads unless you are very familiar with synchronization locks.

But why are you using a TcpClient to talk to a WCF service? Why not use the WCF client channels to access it?

Upvotes: 2

Related Questions