Is there any benefit to using windows winsock API functions compared to BSD-style socket functions? - winsock

Is there any benefit on Windows to use the WSA winsock functions compared to the BSD-style ones?

The most significant difference is the availability of Asynchronous Event style APIs in Winsock.
With Berkeley sockets, each time you read or write your application will "block" until the network is ready, which could make your application unresponsive (unless the network I/O is handled in a different thread).
With an async interface, you can arrange for a callback function to be called as part of the normal windows message loop each time data is received or when the transmit buffer is empty.

Only if you plan to deploy to a legacy platform like Windows 95 or there is something in the winsock API that you absolutely cannot live without and you don't want to roll yourself (<-- doubtful tho).

If you design around the BSD paradigm, your code can work on other platforms with less porting work. If you assume that your network library will support asynchronous I/O (as Alnitak mentions), you're going to have to do a lot more work if that gets pulled out from under you.
Of course, if you're sure you'll never leave the warm bosom of Microsoft, feel free to go to town.

With respect to Alnitak's answer, I agree - I'd just add that you need not use a message loop to use asynch operations on sockets. Using I/O completion ports is a very scalable way to build a high-performance networked application.

Related

Integrating open62541 into an existing libev event loop

I have an existing application that makes extensive use of libev for its event loop. I would now like to add OPC UA server functionality, but am unsure how best to integrate the open62541 event loop into libev.
The following possibilities come to mind:
Call UA_Server_run_iterate from within the libev event loop with a waitInternal time of 0. This would either mean that the server could never sleep (polling open62541 in an ev_idle), or that requests from an OPC UA client would experience an additional latency of up to 50ms (the default max wait time of open62541).
Patch open62541 to allow retrieval of the file descriptors currently in use (serverSockets and connections) by the server network layer. This would allow adding libev events for those file descriptors, which could in turn poll UA_Server_run_iterate only when necessary.
Implement a custom server network layer that makes use of libev. This seems to imply quite a bit of code duplication... Are there any examples/tutorials for implementing a custom network layer?
Run the open62541 event loop in a separate thread. I really really really want to avoid this, since the whole purpose of an event system such as libev is to avoid issues associated with asynchronous operation. For example, all callbacks from open62541 would have to synchronize with the the main libev thread.
Which of the above options would you consider "best" in terms of complexity and performance?
Can you think of any other options not listed above?
Also posted on open62541 mailing list.
I'd recommend either option 1 or option 2. (Disclaimer: I'm one of the core devs of open62541)
Call UA_Server_run_iterate from within the libev event loop with a waitInternal time of 0. This would either mean that the server could never sleep (polling open62541 in an ev_idle), or that requests from an OPC UA client would experience an additional latency of up to 50ms (the default max wait time of open62541).
For now this is probably the best option you could go. You can call UA_Server_run_iterate in a fixed interval, e.g., every 10ms, depending on the requirements of the application. All the other options require patching of open62541 and currently there's a lot going on in the internal API since many features are being added currently. Also have a look at my note at the end!
Patch open62541 to allow retrieval of the file descriptors currently in use (serverSockets and connections) by the server network layer. This would allow adding libev events for those file descriptors, which could in turn poll UA_Server_run_iterate only when necessary.
You probably do not need to patch open62541 since you can get the file descriptor of the socket through the network layer of the server config if the network layer is a TCP layer: server->config.networkLayers[i].serverSockets[j].
This may also introduce a lot of maintenance work since there can be different kinds of network layers. E.g. pubsub is using UDP where the sockets are stored inside of config->pubsubTransportLayers
Implement a custom server network layer that makes use of libev. This seems to imply quite a bit of code duplication... Are there any examples/tutorials for implementing a custom network layer?
You could implement your own network layer using the plugin interface, i.e., write your own (https://github.com/open62541/open62541/blob/master/arch/ua_network_tcp.c). Since this is using an internal API, you can expect a lot of maintenance work and patch-fixing. -> Too much work
Run the open62541 event loop in a separate thread. I really really really want to avoid this, since the whole purpose of an event system such as libev is to avoid issues associated with asynchronous operation. For example, all callbacks from open62541 would have to synchronize with the the main libev thread.
I'd say this is not a good option since you introduce async callbacks.
General note:
We currently have an internal draft and sketch to rework the network interface, especially having one select for all the socket FDs. Currently we have multiple selects for multiple FDs.
Also have a look at the following PR where we already started with the rework:
https://github.com/open62541/open62541/pull/2271

.Net 4.5 TCP Server scale to thousands of connected clients

I need to build a TCP server using C# .NET 4.5+, it must be capable of comfortably handling at least 3,000 connected clients that will be send messages every 10 seconds and with a message size from 250 to 500 bytes.
The data will be offloaded to another process or queue for batch processing and logging.
I also need to be able to select an existing client to send and receive messages (greater then 500 bytes) messages within a windows forms application.
I have not built an application like this before so my knowledge is based on the various questions, examples and documentation that I have found online.
My conclusion is:
non-blocking async is the way to go. Stay away from creating multiple threads and blocking IO.
SocketAsyncEventArgs - Is complex and really only needed for very large systems, BTW what constitutes a very large system? :-)
BeginXXX methods will suffice (EAP).
Using TAP I can simplify 3. by using Task.Factory.FromAsync, but it only produces the same outcome.
Use a global collection to keep track of the connected tcp clients
What I am unsure about:
Should I use a ManualResetEvent when interacting with the TCP Client collection? I presume the asyc events will need to lock access to this collection.
Best way to detect a disconnected client after I have called BeginReceive. I've found the call is stuck waiting for a response so this needs to be cleaned up.
Sending messages to a specific TCP Client. I'm thinking function in custom TCP session class to send a message. Again in an async model, would I need to create a timer based process that inspects a message queue or would I create an event on a TCP Session class that has access to the TcpClient and associated stream? Really interested in opinions here.
I'd like to use a thread for the entire service and use non-blocking principals within, are there anythings I should be mindful of espcially in context of 1. ManualResetEvent etc..
Thank you for reading. I am keen to hear constructive thoughts and or links to best practices/examples. It's been a while since I've coded in c# so apologies if some of my questions are obvious. Tasks, async/await are new to me! :-)
I need to build a TCP server using C# .NET 4.5+
Well, the first thing to determine is whether it has to be base-bones TCP/IP. If you possibly can, write one that uses a higher-level abstraction, like SignalR or WebAPI. If you can write one using WebSockets (SignalR), then do that and never look back.
Your conclusions sound pretty good. Just a few notes:
SocketAsyncEventArgs - Is complex and really only needed for very large systems, BTW what constitutes a very large system? :-)
It's not so much a "large" system in the terms of number of connections. It's more a question of how much traffic is in the system - the number of reads/writes per second.
The only thing that SocketAsyncEventArgs does is make your I/O structures reusable. The Begin*/End* (APM) APIs will create a new IAsyncResult for each I/O operation, and this can cause pressure on the garbage collector. SocketAsyncEventArgs is essentially the same as IAsyncResult, only it's reusable. Note that there are some examples on the 'net that use the SocketAsyncEventArgs APIs without reusing the SocketAsyncEventArgs structures, which is completely ridiculous.
And there's no guidelines here: heavier hardware will be able to use the APM APIs for much more traffic. As a general rule, you should build a barebones APM server and load test it first, and only move to SAEA if it doesn't work on your target server's hardware.
On to the questions:
Should I use a ManualResetEvent when interacting with the TCP Client collection? I presume the asyc events will need to lock access to this collection.
If you're using TAP-based wrappers, then await will resume on a captured context by default. I explain this in my blog post on async/await.
There are a couple of approaches you can take here. I have successfully written a reliable and performant single-threaded TCP/IP server; the equivalent for modern code would be to use something like my AsyncContextThread class. It provides a context that will cause await to resume on that same thread by default.
The nice thing about single-threaded servers is that there's only one thread, so no synchronization or coordination is necessary. However, I'm not sure how well a single-threaded server would scale. You may want to give that a try and see how much load it can take.
If you do find you need multiple threads, then you can just use async methods on the thread pool; await will not have a captured context and so will resume on a thread pool thread. In this case, yes, you'd need to coordinate access to any shared data structures including your TCP client collection.
Note that SignalR will handle all of this for you. :)
Best way to detect a disconnected client after I have called BeginReceive. I've found the call is stuck waiting for a response so this needs to be cleaned up.
This is the half-open problem, which I discuss in detail on my blog. The best way (IMO) to solve this is to periodically send a "noop" keepalive message to each client.
If modifying the protocol isn't possible, then the next-best solution is to just close the connection after a no-communication timeout. This is how HTTP "persistent"/"keep-alive" connections decide to close. There's another possibile solution (changing the keepalive packet settings on the socket), but it's not as easy (requires p/Invoke) and has other problems (not always respected by routers, not supported by all OS TCP/IP stacks, etc).
Oh, and SignalR will handle this for you. :)
Sending messages to a specific TCP Client. I'm thinking function in custom TCP session class to send a message. Again in an async model, would I need to create a timer based process that inspects a message queue or would I create an event on a TCP Session class that has access to the TcpClient and associated stream? Really interested in opinions here.
If your server can send messages to any client (i.e., it's not just a request/response protocol; any part of the server can send messages to any client without the client requesting an update), then yes, you'll need a proper queue of outgoing requests because you can't (reliably) issue multiple concurrent writes on a socket. I wouldn't have the consumer be timer-based, though; there are async-compatible producer/consumer queues available (like BufferBlock<T> from TPL Dataflow, and it's not that hard to write one if you have async-compatible locks and condition variables).
Oh, and SignalR will handle this for you. :)
I'd like to use a thread for the entire service and use non-blocking principals within, are there anythings I should be mindful of espcially in context of 1. ManualResetEvent etc..
If your entire service is single-threaded, then you shouldn't need any coordination primitives at all. However, if you do use the thread pool instead of syncing back to the main thread (for scalability reasons), then you will need to coordinate. I have a coordination primitives library that you may find useful because its types have both synchronous and asynchronous APIs. This allows, e.g., one method to block on a lock while another method wants to asynchronously block on a lock.
You may have noticed a recurring theme around SignalR. Use it if you possibly can! If you have to write a bare-bones TCP/IP server and can't use SignalR, then take your initial time estimate and triple it. Seriously. Then you can get started down the path of painful TCP with my TCP/IP FAQ blog series.

Persistent socket in Lua in parallel with other Lua code

I am implementing sockets in Lua, and the example code I'm working from uses the following method to keep the connection alive:
while true do
-- handle socket traffic here
socket.sleep(1)
end
The loop obviously prevents the rest of the project code to be run, but if I exit the loop the socket server immediately says that the connection was closed.
So how do I keep the socket open simultaneously as the rest of my Lua code runs as normal? (Is there some sort of background job support? Can coroutines be used for this purpose?)
I used Lua Lanes to start a thread that is doing the socket i/o and running in the background as you stated.
http://kotisivu.dnainternet.net/askok/bin/lanes/
Take a look at this answer, which gives info on using Lua Lanes and sockets.
LuaLanes and LuaSockets
The Dual-Threaded Polling solution provided there is probably the most viable, but, there's information about coroutines there also.
(Your question is similar to this question (and I have appropriately flagged it as a duplicate), but here's a copy of my answer for your convenience!)
There are a various ways of handling this issue; which one you will select depends on how much work you want to do.*
But first, you should clarify (to yourself) whether you are dealing with UDP or TCP; there is no "underlying TCP stack" for UDP sockets. Also, UDP is the wrong protocol to use for sending whole data such as a text, or a photo; it is an unreliable protocol so you aren't guaranteed to receive every packet, unless you're using a managed socket library (such as ENet).
Lua51/LuaJIT + LuaSocket
Polling is the only method.
Blocking: call socket.select with no time argument and wait for the socket to be readable.
Non-blocking: call socket.select with a timeout argument of 0, and use sock:settimeout(0) on the socket you're reading from.
Then simply call these repeatedly.
I would suggest using a coroutine scheduler for the non-blocking version, to allow other parts of the program to continue executing without causing too much delay.
Lua51/LuaJIT + LuaSocket + Lua Lanes (Recommended)
Same as the above method, but the socket exists in another lane (a lightweight Lua state in another thread) made using Lua Lanes (latest source). This allows you to instantly read the data from the socket and into a buffer. Then, you use a linda to send the data to the main thread for processing.
This is probably the best solution to your problem.
I've made a simple example of this, available here. It relies on Lua Lanes 3.4.0 (GitHub repo) and a patched LuaSocket 2.0.2 (source, patch, blog post re' patch)
The results are promising, though you should definitely refactor my example code if you derive from it.
LuaJIT + OS-specific sockets
If you're a little masochistic, you can try implementing a socket library from scratch. LuaJIT's FFI library makes this possible from pure Lua. Lua Lanes would be useful for this as well.
For Windows, I suggest taking a look at William Adam's blog. He's had some very interesting adventures with LuaJIT and Windows development. As for Linux and the rest, look at tutorials for C or the source of LuaSocket and translate them to LuaJIT FFI operations.
(LuaJIT supports callbacks if the API requires it; however, there is a signficant performance cost compared to polling from Lua to C.)
LuaJIT + ENet
ENet is a great library. It provides the perfect mix between TCP and UDP: reliable when desired, unreliable otherwise. It also abstracts operating system specific details, much like LuaSocket does. You can use the Lua API to bind it, or directly access it via LuaJIT's FFI (recommended).
* Pun unintentional.
The other answers are nice, but kind of miss the most important point here:
There is rarely a need nowadays to use threads when dealing with sockets
Why? Because multiple sockets are so common, that the OSes (most notably *ix systems) implemented the "multiple poll" in the form of epoll function.
All high-performance networking libraries such as ZeroMQ keep only a few threads, and operate inside them. That lower the memory requirements, but doesn't sacrifice speed.
So my suggestion would be to hook up to OS libraries directly, which is really easy in Lua. You don't have to write the code yourself - quick google search brought me this epoll wrapper [1] You can then still use coroutines to read only from sockets that actually have some data.
You might also want to take a look at ZeroMQ library itself.
[1]Neopallium created Lua bindings for ZMQ, so I think it's legit.
You can indeed use coroutines for that purpose. This is what the popular library Copas does.
Depending on your use case you can use Copas or look at its source code to see how it does it. You may also look at lua-websockets which uses Copas.

Creating a Delay in lua

I'm making a IRC client using LUA. I'm using the the libraries that came with "Lua for Windows ". So I'm using luasocket for the comms and IUP for the UI bits.
The problem I'm having is that I'm getting stuck in a loop when I read the IO. I tried the timer in IUP but that didn't seem to work.
I'm was looking for a way to delay the IO read loop.
I set the time out for the reads to 0 and that worked.
You are probably making a blocking read on a TCP socket inside the GUI thread. That will lock up your whole application if you do not receive the expected data in a timely manner. Either perform the socket I/O in a separate thread (see Lua Lanes) or use non-blocking I/O (see settimeout).
The Kepler Project is a great resource for guidance on networking applications with Lua, but it is focused on web applications versus an IRC client. For example, the Copas library uses Lua coroutines to handle multiple TCP connections.
Now if you really just wanted to know how to create a delay in Lua, then the Sleep Function article in the lua-users wiki should provide all the information you need.

How to Monitor Sockets activity in a computer?

I want to write a program from scratch to see the sockets activity, what they send, what they receive, etc. I don't want to use a Library because it's more for academic purposes than anything else.
Where should I start?
Just to be clear: my program won't be connecting to anything or creating any socket, it just wants to listen to the activity in a computer.
Any thoughts (in any OS) about where to start will be appreciated.
Thanks in advance.
Take a look at libpcap/WinPcap.
You are out of luck as far as portability goes.
Although the application APIs are the same or similar for UNIX and Windows, and, both implementations are historically based on the same Berkeley Sockets BSD code, the operating system architecture and hence the ways you access low level OS functions such as network IO are completely different.
Linux has a number of 'network sniffing' tools(tcpdump, ethereal etc.) easily available. Not so sure about windows, MS provides a Network Monitor and there are some tools available -- Google 'Sysinternals TDImon' for the MS tools.