I need to write some simple server Winsock code. (Yes, I understand the irony.) One thread, one connection, host A to host B, non blocking. I don’t have the expertise to take these do everything multi-threaded example classes and merge them into my MFC project. Where can I find a simple description or example?
I used a vendor starter application (MFC, C++ project template) to fetch telemetry data from the vendor A’s application. Now I need to send the telemetry data, one to three megabytes per second, directly to vendor B’s server application for display. The vendor B system is a server to multiple client display computers, but turns out that it expects my application to take the role of server to its client. (Makes sense, my app is providing the data, it is consuming the data.)
Vendor B has an example TCP class for my server end but it uses blocking mode and there are several difficulties. Their client receiving app is not robust and intentionally breaks the connection as least once, and maybe frequently. The code keeps getting hung somewhere and determining the problem is quite difficult. It seems to me that if the code on my end were non-blocking I could get it to update the dialog and tell me what it is trying to do. Vendor B is not sympathetic to my request for non-blocking advice. Wiresharking the packets to find the error in my application is proving difficult.
I can find and read about all the asynchronous calls, but don’t know how to set up the callbacks and messages needed for the Winsock class to sense problems with the connection and take action. Integrating into an existing MFC project is difficult.
Pointers to some good explanations and tutorial are requested. In the meantime, I continue searching and reading.
Edit: Oops, left out: Windows XP Pro, Visual Studio 2008, MFC, C++, TCP/IP communications
Last edited by bkelly; November 27th, 2011 at 07:57 PM.
Reason: left out information
The example posted by VictorN might be too complex for the purposes explained in the OP.
Simple socket code that should work "out of the box" under any Windows OS can be found in the Winsock Programmer's FAQ, http://tangentsoft.net/wskfaq/ . See sections 5 and 6. For one of the simpler pairings of client and server, try the "basic blocking client" and the "select()-based server". Both are console applications, so they are nothing fancy. Moreover, because they are console applications, the fact that they are blocking (your OP asked for non-blocking) is of little consequence.
I did check it out. It converted to Visual Sudio 2008 without error and compiled. There were a few instances of warning D9035, Wp64 has been deprecated, but I find that somewhat expected as I am running Windows 7 64 bit.
when running the server it shows two error messages.
1604: Failed to create listener socket on port 49153
1604: Not Owner
I ran it as administrator, same message. I have not found the error code yet. Any clues?
The re-write article is good, but quite complex. What can I do if I don't need anywhere near that much complexity. To exemplify my thoughts I refer to the Winsock Programmers FAQs
found here: http://tangentsoft.net/wskfaq/exampl...ics/index.html
In there there are three options for writing server code
1. Basic Blocking Server
3. Multithreaded Server
4. Select() Based Server.
Notice that I made it four options. Seems to me there should be an option for non blocking single thread single connection server code. I don't need multiple threads or multiple connection. Just a connection to a single IP address and port associated with a vendor's application that expectes my code to take the server role.
I want it to be non-blocking so I can put messages in the dialog and I can see the status of my server code and the socket it is driving.
For simple, singly-threaded servers with a UI in MFC, use a CAsyncSocket-derived class. Be aware that the CASyncSocket model will not support a huge number of simultaneous clients. Your practical limit might be something like 100-200 simultaneous clients.
'Webster' is the Yugo of Web servers. It offers only minimal HTTP support with no frills. 'Webster' only handles GET requests, and it doesn't do anything with most of the extra client information it receives, like the If-Modified-Since field. But 'Webster' does illustrate the basic features common to every Web server, including very powerful servers like Microsoft Internet Information Server. 'Webster' is good enough to present your very own real live Web home page, and it does run on both Windows 95 and Windows NT. (From the February 1996 issue of Microsoft Systems Journal (predecessor of MSDN Magazine))
The article is old (from 1996) but is still useful. It describes an HTTP server, and thus the first half of the article includes a long description of HTML and HTTP that you can probably ignore. The second half does a nice job of explaining how to derive a class from CAsyncSocket and then use it for a singly-threaded server with an active UI. It also explains how to keep track of multiple connected clients.
As for the clients, the Winsock FAQ includes an example of a client that uses CAsyncSocket.