Skip to content
Advertisement

Parallel TCP connection using threads

I am trying to build a system that opens parallel TCP sockets using threads. My threads are triggered using message queue IPC , thus every time a packet arrive to the message queue a thread “wakes up” , open TCP connection with remote server and send the packet. My problem is that in Wireshark , I can see the the time it takes to send a file is smaller using threads instead of one connection , but the throughput does not change.
My questions are :

  1. How can i verify my threads working parallel?
  2. How can i improve this code?, 3.How can i open several sockets using one thread?

I am using Virtual machine to run the multithreaded clients. The IDE I am using is Clion , language is C. My code:

JavaScript

Advertisement

Answer

thus every time a packet arrive to the message queue a thread “wakes up” , open TCP connection with remote server and send the packet

If you’re at all concerned about speed or efficiency, don’t do this. The single most expensive thing you can do with a TCP socket is the initial connection. You’re doing a 3-way handshake just to send a single message!

Then, you’re holding a global mutex while doing this entire operation – which, again, is the slowest operation in your program.

The current design is effectively single-threaded, but in the most complicated and expensive possible way.

I can see the the time it takes to send a file is smaller using threads instead of one connection , but the throughput does not change

I have no idea what you’re actually measuring, and it’s not at all clear that you do either. What is a file? One fragment? Multiple fragments? How big is it compared to your MTU? Have you checked that the fragments are actually received in the correct order, because it looks to me like the only possible parallelism is the spot where that could break.

How is it possible to have lower latency and unchanged throughput for a single file?

How can i verify my threads working parallely?

If you see multiple TCP connections in wireshark with different source ports, and their packets are interleaved, you have effective parallelism. This is unlikely though as you explicitly prohibited it with your global mutex!

What is the best way to check the throughput in wireshark?

Don’t. Use wireshark for inspecting packets, use the server to determine throughput. That’s where the results actually matter.

3.Is the concept of parallel TCP suppose to increase the throughput?

Why did you implement all this complexity if you don’t know what it’s for?

There’s a good chance a single thread (correctly coded with no spurious mutex thrashing) can saturate your network, so: no. Having multiple I/O threads is generally about conveniently partitioning your logic and state (ie, having one client per thread, or different unrelated I/O subsystems in different threads), rather than performance.


If you want to pull packets off a message queue and send them to TCP, the performant way is to:

  1. use a single thread just doing this (your program may have other threads doing other things – avoid synchronizing with them if possible)
  2. open a single persistent TCP connection to the server and don’t connect/close it for every fragment
  3. that’s it. It’s much simpler than what you have and will perform much better.

You can realistically have one thread handling multiple different connections, but I can’t see any way this would be useful in your case, so keep it simple.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement