Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a simple implementation of UDP based client - server communication using CAsyncSocket class.

.h file:
C++
// Receiving Socket Class definition
#if _MSC_VER > 1000
#pragma once
#endif
#include <afxtempl.h>

// UdpReceiveSocket command target
class UdpReceiveSocket : public CAsyncSocket
{
      void OnReceive(int nErrorCode);
	 
public:
      UdpReceiveSocket();
      virtual ~UdpReceiveSocket();
	   void SetParent(CDialog* pWnd);
private:
	  CDialog* m_pWnd;

};


.cpp file:
C++
// Receiving Socket Class implementation
// UdpReceiveSocket.cpp : implementation file
//
#include "stdafx.h"
#include "UdpReceiveSocket.h"
#include "ServerDlg.h"


// UdpReceiveSocket
UdpReceiveSocket::UdpReceiveSocket()
	
{
      // Just specify input PORT#, local machine is assumed
      BOOL bRet = Create(MASTER_PORT, SOCK_DGRAM, FD_READ);
      if (bRet != TRUE)
      {
             UINT uErr = GetLastError();
             TCHAR szError[256];
             wsprintf(szError, "Server Receive Socket Create() failed: %d", uErr);
             AfxMessageBox(szError);
      } 
}

UdpReceiveSocket::~UdpReceiveSocket()
{
}

// UdpReceiveSocket member functions
void UdpReceiveSocket::OnReceive(int nErrorCode)   
{

  TCHAR buff[CMAXLENBUFF];
  int nRead;
  CString strSendersIp;
  UINT uSendersPort;

  // Could use Receive here if you don't need the senders address & port
  nRead = ReceiveFromEx(buff, CMAXLENBUFF, strSendersIp, uSendersPort); 

  switch (nRead)
  {
	 case 0:       // Connection was closed.
		 Close();      
		 break;
	 case SOCKET_ERROR:
		if (GetLastError() != WSAEWOULDBLOCK) 
		{
			AfxMessageBox ("UDP Read Error occurred");
			Close();
		}
		break;
	default: // Normal case: Receive() returned the # of bytes received.
		buff[nRead] = 0; //terminate the string 

		// only process it if we are using Ethernet 
		if (((CServerDlg*)m_pWnd)->commuse == 2)
		{
			((CServerDlg*)m_pWnd)->m_hcrecv.SetPing(50);		// Indicate we have received some data
			((CServerDlg*)m_pWnd)->IncomingMessage (buff, strSendersIp);
			// debug only
			CString tt;
			tt.Format ("Received %d bytes, error code: %d", nRead, nErrorCode);
			((CServerDlg*)m_pWnd)->Status (tt);
		}
  }

  CAsyncSocket::OnReceive(nErrorCode);
}


void UdpReceiveSocket::SetParent(CDialog *pWnd)
{
	m_pWnd = pWnd; // Set the member pointer for function calls
}


And I use it like that:
C++
// Implementation
protected:
	UdpReceiveSocket       m_UDPReceiveSocket;       // Receive socket for UDP
	UdpSendSocket          m_UDPSendSocket;		 // Send socket for UDP


C++
BOOL CServerDlg::UDPConnectionInitialise(void)
{
	CString con;
	
	m_UDPReceiveSocket.SetParent(this);

	con.Format("Connected via UDP port %d", MASTER_PORT); 
	
	Status(con);

	m_status.SetLed(0,1);	// Green status LED  
	return 1;
}


My 'client' is Arduino board with simple software which forwards serial comms to Ethernet port and vice versa. Ethernet port from Arduino is connected to router with Ethernet cable.

My 'server' is my laptop, connected to the same router over Wi-Fi. On the laptop I have software to read the data, using the code above.

Everything was working OK while I was using my old BT router (BT2700HGV). But yesterday I tried it with new TP-Link router (TL-WR841N) and my code isn't working. What happens is, every time new message arrives, it takes long time for ReceiveFromEx() function to return the incoming data (about 5 sec). During this time whole application is blocked. Also next message arrives in the meantime, so as soon as the ReceiveFromEx() returns, it is triggered again.

All this happens only with the new router. I tried to change various settings on it, but nothing has changed this behavior.

I ran 'UDP Test Tool' on the laptop to see if the incoming UDP packets are any different but it looks exactly the same with both routers.

Can anybody help?
Posted
Comments
Richard MacCutchan 17-Jul-15 9:50am    
Since the problem is obviously connected to your new router, exactly what help do you think we can offer?
blackbolek 20-Jul-15 8:32am    
Well - the new router only showed that there is a weak point somewhere in my code (the UDP Test Tool software receives UDP packages from new router without any problem). I was hoping that somebody will spot it, or point me in the right direction.
Richard MacCutchan 20-Jul-15 8:44am    
"a weak point somewhere in my code"
And while we appreciate that is not a lot of help, the only way to identify where, is by doing some detailed diagnostic work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900