Archived Forum Post

Index of archived forum posts

Question:

Socket Issue - Bytes Being Sent to Socket, But Unable to Retrieve

May 04 '17 at 11:11

I have a very simple program to listen on a particular socket and take action based on strings sent to that socket by clicking on a special link in an HTML file. It has worked fine for quite some time and now... doesn't. It connects to the socket fine, and netstat shows it as listening at that socket... but the data never comes through. If I use another port listener, the strings come through as expected - so it is not a firewall issue (and I have tried it with firewalls disabled, in Safe Mode, on different machines, etc.) I updated my Chilkat libraries back in January, which may have been when the break happened. I can't be sure as I don't use this program that often - I know it worked in December and doesn't work now. I have updated to the most recent Chilkat libraries, still have the issue and don't know what to try next. Anyone have any ideas ?

Header File

#pragma once

#include "CkSocket.h" #include "afxwin.h" #include "resource.h"

#define SERV_TIME 500 #define TIMER_S 777 #define PORT_S 666

// ServerTest dialog class ServerTest : public CDialog { DECLARE_DYNAMIC(ServerTest)

public: ServerTest( CWnd* pParent=NULL ); // standard constructor virtual ~ServerTest();

// Dialog Data enum { IDD = IDD_SERVER_TEST };

  // Socket and connection
  CkSocket       sock;             // socket class
  CkSocket*      conn;             // connection

  // Timing
  int            nTime;            // timer increment

  // Processing ?
  BOOL           bProc;            // are we processing a timer call ?

protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

   DECLARE_MESSAGE_MAP()

public:

  // Start the socket server
  BOOL StartServer( int p_nTime=SERV_TIME );

  // Stop the socket server
  void StopServer();

  // Process server calls
  BOOL ProcessServerCalls();

  afx_msg void OnDestroy();
  virtual BOOL OnInitDialog();
  afx_msg void OnTimer(UINT_PTR nIDEvent);
  afx_msg void OnClose();

};

Code File

#include "StdAfx.h" #include "ServerTest.h"

// ServerTest dialog IMPLEMENT_DYNAMIC(ServerTest, CDialog)

ServerTest::ServerTest( CWnd* pParent ) : CDialog( ServerTest::IDD, pParent ) { // Not processing bProc = false; }

ServerTest::~ServerTest() { }

void ServerTest::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }

BEGIN_MESSAGE_MAP(ServerTest, CDialog) ON_WM_DESTROY() ON_WM_TIMER() ON_BN_CLICKED(IDCANCEL, &ServerTest::OnClose) END_MESSAGE_MAP()

// // ServerTest message handlers //

void ServerTest::OnDestroy() { // Stop server StopServer();

  // Base class
  CDialog::OnDestroy();

}

BOOL ServerTest::OnInitDialog() { CDialog::OnInitDialog();

  // Start the server
  if ( !StartServer() )
  {
     // Uh-oh
     ASSERT(0);
     OnCancel();
     return true;
  }

  return TRUE;  // return TRUE unless you set the focus to a control
  // EXCEPTION: OCX Property Pages should return FALSE

}

void ServerTest::OnTimer(UINT_PTR nIDEvent) { // // Timer has been called //

  // Busy ?
  if ( bProc )
     return;

  // Set flag
  bProc = true;

  if ( nIDEvent == TIMER_S )
     // Process display server calls
     ProcessServerCalls();

  // Clear flag
  bProc = false;

  // Base class
  CDialog::OnTimer(nIDEvent);

}

BOOL ServerTest::StartServer( int p_nTime ) { // // Start the server //

  BOOL  rval = false;     // return value

  // Kill any existing timer
  KillTimer( TIMER_S );

  // Save time increment
  nTime = p_nTime;

  // No connection
  conn = NULL;

  //
  // Start the socket listener
  //

  do
  {
   // Unlock the component
     if ( !sock.UnlockComponent( "XXXXX" ) )
        break;

     // Start listening
     if ( !sock.BindAndListen( PORT_S, 10 ) )
        break;

     // Set timeout
     sock.put_MaxReadIdleMs( 50 );

     // Start the timer
     SetTimer( TIMER_S, nTime, NULL );

     // Must be ok
     rval = true;

  } while(0);

  // Return
  return rval;

}

void ServerTest::StopServer() { // // Stop the server //

  // Kill timer
  KillTimer( TIMER_S );

  // Close it
  sock.Close( 100 );

}

BOOL ServerTest::ProcessServerCalls() { // // Process server calls //

  BOOL           rval = true;         // return value
  const char*    rs;                  // received string
  CString        str;                 // string
  CkString       ckStr;               // string

  // Any data ?
  // This line gets hit all the time, so the timer part of things is working
  if ( !sock.PollDataAvailable() )
     return false;

  // Testing
  // This line never gets hit, as there is never any data available...
  ASSERT(0);

  // Not connected ?
  if ( conn != NULL && !conn->get_IsConnected() )
  {
     delete conn;
     conn = NULL;
  }

  // Look for a connection ?
  if ( conn == NULL )
     conn = sock.AcceptNextConnection( 10 );

  // No connection ?
  if ( conn == NULL )
     return false;

  // No data ?
  if ( !conn->PollDataAvailable() )
     return false;

  // Read it
  conn->put_MaxReadIdleMs( 100 );
  rs = conn->receiveString();
  if ( rs == NULL )
     return false;

  // Copy the string
  str.SetString( rs );

  // Let the browser go back to what it was doing...
  conn->SendString( "HTTP/1.1 204 Never mind\r\n\r\n" );

  // Now do some processing based on the string we just read...

  // Return
  return rval;

}

void ServerTest::OnClose() { // // Close this dialog //

  // Stop
  StopServer();

  // Close
  CDialog::OnCancel();

}

Testing Web Page

   <!doctype html public "-//w3c//dtd html 4.0 transitional//en">

<html> <head> <title>Server Test</title> </head>

  <body class="kvee">

     <br />
     <hr>
     <center>
        <h1>Server Test Page</h1>
     </center>
     <hr>

     <a href="http://localhost:666/?First String 1">First String 1</a><br /><br />
     <a href="http://localhost:666/?First String 2">First String 2</a><br /><br />

  </body>
</html>