Multiple device connections using Sockets

Asked

Viewed 295 times

1

I have a method that connects to a card reader via TCP/Ip (ip, port) using Sockets. For the 1:1 connection is working, but now I need to connect to more than one device simultaneously. I would have to create a Thread for each connection, or would I have another way to do it? Remembering that my system is a "client" of readers.

  • 1

    And what is the doubt/problem?

  • "I would have to create a Thread for each connection, or have another way to do it?"

2 answers

0

Dear Jcsaint,

A socket implementation is not such a simple task, even with the various abstractions . NET offers.

It is possible to start a new thread per connection but this is not the recommended method because if there is overload the operating system will probably kill your process.

On a web server I developed using C++ I used the asynchronous approach, using 1 thread to receive new connections and a queue (pool) of connections which is processed by the other physical threads available in the hardware (thus obtained better performance for my case), being these properly synchronized when necessary.

I hope I’ve helped.

  • In this case I would have to implement something in c++, generate a dll and consume it in c# ?

  • The same implementation can be done in C#, using the . NET Framework ready sockets or other technology. I put as an example of the implementation I did in C++ because I did not develop equivalent code in C#, but it is fully possible.

0

This depends on the methods you’re using to connect, send and receive data.

If you are using methods for example Connect, Send and Receive they block execution so it would be necessary to use Threads, however this is not the most recommended, the most recommended is to use the asynchronous methods.

Using the methods BeginConnect, BeginSend and BeginReceive you should have no problems since they do not block the thread and use a Callback system to notify when they have finished running or received data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.