0

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?

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your response. I am writing WCF service which has to accept requests from multiple clients, service works with data received and then has to call 3rd party app, get a response and send it back to client. Client-WCF Service|WCF-3rd party app|3rd party app to WCF|WCFback to client. WCF creates a new TcpClient per each request from client, opens connection to 3rd party app, after response received closes TcpClient. As far as my understanding opening connection/ closing connection is time consuming, so i am thinking how i can open connection and reuse it for all clients requiests.
Depending upon the type of channel used, if you have an instance of the client proxy (which derives from ClientBase<TChannel>) you can call Open on the channel to establish the connection and Close to close it. But the same caveats apply. Although you can use it across threads, you have to be very careful not to do so at the same time.
Sorry Josh but i am afraid you lost me. As i understand you are saying that its not safe to share TcpClient used to call 3rd party app between clients calling WCF service. Can i share networkStream, or its not save to call stream.write simultaneously for two different clients of WSF service? Thank you!
No, it is never ok to read/write to any stream/socket/writer/etc from two threads simultaneously. You would need very solid thread synchronization code around the shared objects. It's a pretty complex topic. I would avoid it unless absolutely necessary in which case I'd read up a lot on thread synchronization first.
Here is another thought. If I am not mistaken one socket can handle a few parallel streams, so what if I’ll share TcpClient between threads but network stream will be initiated by each thread TcpClient myTcp = new TcpClient(); myTcp holds connection and shared. Each thread will have NetworkStream ns = new NetworkStream(); ns = myTcp.GetStream() in this case each stream will be referencing to a new instance of stream or not? Thank you.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.