CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2011
    Posts
    72

    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
    Last edited by bkelly; November 27th, 2011 at 08:57 PM. Reason: left out information

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: simple winsock server advice

    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    72

    Re: simple winsock server advice

    Took quick look, looks good, will work it asap.
    Thank you for your time.
    BK

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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

  5. #5
    Join Date
    Nov 2011
    Posts
    72

    Re: simple winsock server advice

    Quote Originally Posted by MikeAThon View Post
    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.

    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.

  6. #6
    Join Date
    Nov 2011
    Posts
    72

    Re: simple winsock server advice

    Quote Originally Posted by VictorN View Post
    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?

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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();
    Last edited by VictorN; November 29th, 2011 at 12:08 PM.
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2011
    Posts
    72

    Can we simplify

    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
    2. <lacking>
    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.

    Where can I look for guidance?

  9. #9
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured