# Faster TCPSockets

**URL:** <https://ifnotnil.com/t/faster-tcpsockets/1376>\
**Category:** General Programming\
**Created:** [30 November 2020 02:26 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376 "2020-11-30T02:26:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 02:26 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/1 "2020-11-30T02:26:19Z")

</div>

Splitting off from [here](https://ifnotnil.com/t/xojo-workers-multi-threading/1364)…

I did some tests…

There’s a manufacturer’s software (an Editor) that connects to a piece of hardware. They communicate via TCP.  
When the software is connecting directly to the hardware, it takes 36 seconds to synchronize. (Passing around 1mb of data)  
Speed is the same on MacOS or Windows.  
I created a test app in Xojo, using 2 TCPSockets, one receiving/sending from/to the Editor and one doing the same for the hardware. Each receive a packet and send to the other, with no processing in-between, and it takes around 7 **minutes!** to synchronize. Again, same speed on MacOS or Windows.

I then created an app in [VB.NET](http://VB.NET) on Windows, doing the same thing that the Xojo App is doing, passing the packets back and forth with no processing, and it takes only 36 seconds to synchronize, essentially adding no overhead to the communication at all. I wasn’t even using threads!

Clearly the Xojo TCPSocket is REALLY slowing things down. (Like 10x)

Would MBS Sockets work faster?

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 November 2020 03:21 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/2 "2020-11-30T03:21:24Z")

</div>

Would love to see the code for you Xojo stuff as I’ve done a decent amount with TCP Sockets (going as far as creating a full db “plugin” with them) and they arent THAT slow  
So I’m curious about your code to see why it behaves this way for you

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 03:44 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/3 "2020-11-30T03:44:11Z")

</div>

Basically:

```auto
RawConsoleSocket = New TCPSocket
RawConsoleSocket.Address = "192.168.1.128"
RawConsoleSocket.Port = 50000
RawConsoleSocket.Connect

RawEditorSocket = New TCPSocket
RawEditorSocket.Address = "127.0.0.1"
RawEditorSocket.Port = 50000
RawEditorSocket.Listen

While Not RawEditorSocket.IsConnected And RawEditorSocket.LastErrorCode = 0
  App.DoEvents
Wend

While RawEditorSocket.IsConnected
  EditorMsg = RawEditorSocket.ReadAll
  RawConsoleSocket.Write(EditorMsg)
  
  App.DoEvents
  
  ConsoleMsg = RawConsoleSocket.ReadAll
  RawEditorSocket.Write(ConsoleMsg)
Wend

```

Properties of the MainWindow:  
RawConsoleSocket, RawEditorSocket: TCPSocket  
ConsoleMsg, EditorMsg: String

btw, without `DoEvents` the app just hangs…

---

<div class="post-metadata">

**Author:** ![charonn0](https://ifnotnil.com/user_avatar/ifnotnil.com/charonn0/32/223_2.png) [@charonn0](https://ifnotnil.com/u/charonn0)\
**Post date:** [30 November 2020 04:12 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/4 "2020-11-30T04:12:19Z")

</div>

> [@Meestor\_X](#):
>
> btw, without `DoEvents` the app just hangs…

The loop is blocking the GUI from updating, but the app is probably not actually hung. Once the loop exits the GUI should become responsive again. Calling DoEvents is neither necessary nor appropriate here, and is almost certainly a major cause of the slowness.

Try it again without DoEvents and wait until the loop completes, even if the GUI hangs. I expect the the timing to be much closer to what you’re expecting.

To avoid blocking the GUI either put your loop in a Thread, or use the events of the socket (Connected, DataAvailable, etc.) to avoid looping at all.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 06:31 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/5 "2020-11-30T06:31:23Z")

</div>

> [@charonn0](#):
>
> The loop is blocking the GUI from updating, but the app is probably not actually hung. Once the loop exits the GUI should become responsive again. Calling DoEvents is neither necessary nor appropriate here, and is almost certainly a major cause of the slowness.

No, the app definitely hangs. The sockets will not do anything unless there’s a DoEvents in there.  
I also created the app with event-driven sockets, using DataAvailable and so on. Speed was the same.

I’m quite sure that putting it into a thread will be just as slow. But if you feel very certain of this, I can give it a try.

---

<div class="post-metadata">

**Author:** ![thommcgrath](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@thommcgrath](https://ifnotnil.com/u/thommcgrath)\
**Post date:** [30 November 2020 06:32 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/6 "2020-11-30T06:32:34Z")

</div>

If this is a console app, wait less time in DoEvents. You’ll use more CPU, but you can process more events per second.

If this is a desktop app, dear god stop this mayhem and refactor away from synchronous sockets. Use the actual socket events, never DoEvents, and you should have better results.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 06:35 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/7 "2020-11-30T06:35:15Z")

</div>

Everyone, please keep in mind this is just a test.  
Don’t lose sleep over the doevents thing. It’s not going to kill babies.

Again, I have done it both ways, with events and in a loop, to see if there was any difference. There wasn’t.

---

<div class="post-metadata">

**Author:** ![charonn0](https://ifnotnil.com/user_avatar/ifnotnil.com/charonn0/32/223_2.png) [@charonn0](https://ifnotnil.com/u/charonn0)\
**Post date:** [30 November 2020 07:32 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/8 "2020-11-30T07:32:39Z")

</div>

What happens if you replace DoEvents with `Socket.Poll()`?

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [30 November 2020 09:17 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/9 "2020-11-30T09:17:50Z")

</div>

We have an app with console, ServerSocket and DoEvents.  
When there is no connection, we use a delay of 10ms. If there is a connection, we use 1ms to be more responsive. And when there is an active transfer, we use 0ms delay to be as fast as possible.

and DoEvents runs Socket.Poll on each call.

---

<div class="post-metadata">

**Author:** ![ubogun](https://ifnotnil.com/user_avatar/ifnotnil.com/ubogun/32/1083_2.png) [@ubogun](https://ifnotnil.com/u/ubogun)\
**Post date:** [30 November 2020 11:51 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/10 "2020-11-30T11:51:04Z")

</div>

> [@Meestor\_X](#):
>
> `EditorMsg = RawEditorSocket.ReadAll`

I wonder what a ReadAll does if there is nothing to read yet. I would not be surprised if that could be (one of) the reason(s) of the slowness you encounter. I would definitely go the asynchronous way like @charonn0 and @thommcgrath already proposed or at least check for data availability before the reads.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [30 November 2020 13:32 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/11 "2020-11-30T13:32:11Z")

</div>

ReadAll returns immediately if there is no data.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 15:37 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/12 "2020-11-30T15:37:59Z")

</div>

Please stop thinking about DoEvents, everyone. It’s a red herring. If it gets the conversation moving, my previous version of the test worked the “usual” way with DataAvailable. I was only trying to see whether events were slowing things down so I tried it without events to see if it would be faster. It wasn’t. It was the same.  
Here’s what was inside each Socket’s DataAvailable handler:

EditorSocket.DataAvailable:

```auto
Sub DataAvailable()
  EditorMsg = Me.ReadAll
  ConsoleSocket.Write(EditorMsg)
End Sub

```

ConsoleSocket.DataAvailable:

```auto
Sub DataAvailable()
  ConsoleMsg = Me.ReadAll
  EditorSocket.Write(ConsoleMsg)
End Sub

```

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 November 2020 16:37 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/13 "2020-11-30T16:37:57Z")

</div>

this _should_ be about as fast as you can get since you dont do any buffering etc

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 16:54 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/14 "2020-11-30T16:54:22Z")

</div>

Ok, then would you agree that the Xojo TCPSockets implementation is the likely culprit?

@MonkeybreadSoftware, are your Sockets faster?

How would I “drop in” an MBS Socket in place of the TCPSocket to test?

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 November 2020 17:03 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/15 "2020-11-30T17:03:11Z")

</div>

Based on what you have posted there would _seem_ to be evidence that suggests they are not optimal

I have been able to flood very fast connections with them as well (sending & receiving)

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [30 November 2020 17:43 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/16 "2020-11-30T17:43:31Z")

</div>

Please let me know what sockets from us you talk about.

maybe [RAWSocketMBS](https://www.monkeybreadsoftware.net/class-rawsocketmbs.shtml) class?

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 20:42 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/17 "2020-11-30T20:42:41Z")

</div>

> [@MonkeybreadSoftware](#):
>
> Please let me know what sockets from us you talk about.

You tell me! 😅  
Do you have something that would work in place of a Xojo TCPSocket and be faster?

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 20:50 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/18 "2020-11-30T20:50:12Z")

</div>

The weird thing is that the data rate from the device is pretty darn slow. 1mb in 36s is like 27kB/s???

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [30 November 2020 21:14 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/19 "2020-11-30T21:14:49Z")

</div>

We have various classes for various use cases.  
e.g. [CURLSMBS](https://www.monkeybreadsoftware.net/class-curlsmbs.shtml) can do connection (with or without SSL) and you can then use Send/Receive commands to handle the protocol yourself.  
Otherwise [RAWSocketMBS](https://www.monkeybreadsoftware.net/class-rawsocketmbs.shtml) is a good socket replacement to do a bit more than the built-in one.

---

<div class="post-metadata">

**Author:** ![Meestor\_X](https://ifnotnil.com/user_avatar/ifnotnil.com/meestor_x/32/567_2.png) [@Meestor\_X](https://ifnotnil.com/u/Meestor_X)\
**Post date:** [30 November 2020 23:50 UTC](https://ifnotnil.com/t/faster-tcpsockets/1376/20 "2020-11-30T23:50:15Z")

</div>

Not sure what you mean by “handle protocol yourself”?  
It’s TCP, sending strings of bytes to and from.  
So RAWSocketMBS then?

Do you have any documentation on how to drop in a RAWSocketMBS in place of a TCPSocket?

[Next page](https://ifnotnil.com/t/faster-tcpsockets/1376.md?page=2)
