simple winsock server advice
Summary
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?
Background
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
Re: simple winsock server advice
Re: simple winsock server advice
Took quick look, looks good, will work it asap.
Thank you for your time.
BK
Re: simple winsock server advice
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.
Mike
Re: simple winsock server advice
Quote:
Originally Posted by
MikeAThon
Simple socket code that should work "out of the box" under any Windows OS can be found in the Winsock Programmer's FAQ, ...
Mike
Thanks, that a good site. Thanks.
Quote:
Moreover, because they are console applications, the fact that they are blocking (your OP asked for non-blocking) is of little consequence.
Mike
As noted, I have an MFC application. Blocking console code is not helpful.
Re: simple winsock server advice
Quote:
Originally Posted by
VictorN
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?
Re: simple winsock server advice
Just a guess:
Try to allow this port in your Firewall.
// edit: try to set a breakpoint on the line " DWORD err = ::GetLastError();" of the BOOL CAsyncServerDlg::CreateListener() method:
Code:
if(!m_listensoc.Create(portnum))
{ /* failed to create */
DWORD err = ::GetLastError();
CString fmt;
fmt.LoadString(IDS_LISTEN_CREATE_FAILED);
CString * s = new CString;
s->Format(fmt, portnum);
PostMessage(UWM_INFO, (WPARAM)s, ::GetCurrentThreadId());
PostMessage(UWM_NETWORK_ERROR, (WPARAM)err, ::GetCurrentThreadId());
return FALSE;
} /* failed to create */
Then debug and see the error code returned by ::GetLastError();
Re: simple winsock server advice
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.
One example of such a server is found in this article: ""Write a Simple HTTP-based Server Using MFC and Windows Sockets" at http://www.microsoft.com/msj/archive/S25F.aspx
Quote:
Originally Posted by Microsoft Systems Journal
'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.
Mike