I am very new to development and have been sinking my teeth into C#. I have an application that was written in C++ which is out of my depth, and I was wondering if someone could explain how I would recreate this in c#.

The application sends message to linux service via a socket connection. The message is in the form of a struct:

struct alert
{
	unsigned short size;
	unsigned short signature;
	unsigned short bperiod;
	unsigned short bduty;
	unsigned short svolume;
	unsigned short stone;
	unsigned short sfrequency;
	unsigned short speriod;
	unsigned short sduty;
};

This app was designed to use a form, but this was of no use, so I did ammed this to recieve it's settings via the command line.

szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
 
   	sleepcount = _ttoi(szArglist[9]); 
	m_Request.size					= sizeof(m_Request);
	m_Request.signature				= ALERT_REQUEST_SIGNATURE;
	m_Request.beacon_period			= _ttoi(szArglist[2]); 
	m_Request.beacon_duty			= _ttoi(szArglist[3]); 
	m_Request.sounder_volume		= _ttoi(szArglist[4]); 
	m_Request.sounder_tone			= _ttoi(szArglist[5]); 
	m_Request.sounder_frequency		= _ttoi(szArglist[6]); 
	m_Request.sounder_period		= _ttoi(szArglist[7]);
	m_Request.sounder_duty			= _ttoi(szArglist[8]); 

	SendRequest(GetDWORDIPAddress(szArglist[1]));

The code for the SendRequest:

void CTestDialog::SendRequest(DWORD dwUnit)
{
	if (Connect(dwUnit) && ::send(m_sktUnit, reinterpret_cast<LPSTR>(&m_Request), sizeof(m_Request), 0) != sizeof(m_Request)) {
		::closesocket(m_sktUnit);
		exit(1002);
		m_sktUnit = INVALID_SOCKET;
	}
}

Thank you in advance for your help.

Edited 6 Years Ago by newworldcoder: n/a

Attachments
#ifndef _ALERTREQ_H_
#define _ALERTREQ_H_

#define ALERT_REQUEST_PORT				0x1bbf

#define ALERT_REQUEST_SIGNATURE		0x4152

struct alert_request
{
	unsigned short size;
	unsigned short signature;
	unsigned short beacon_period;
	unsigned short beacon_duty;
	unsigned short sounder_volume;
	unsigned short sounder_tone;
	unsigned short sounder_frequency;
	unsigned short sounder_period;
	unsigned short sounder_duty;
};

#endif

/* vi:set ts=3 sw=3 cin: */
#define WINVER							0x0500

#include <afxwin.h>
#include <afxcmn.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ipifcons.h>
#include <math.h>
#include "resource.h"

#include "alertreq.h"

#define APP_NAME						_T("WinTest")

#define VER_MAJOR						1
#define VER_MINOR						0

#define _STRINGIFY(x)				L#x
#define STRINGIFY(x)					_STRINGIFY(x)

#define ARRAY_SIZE(a)				(sizeof(a)/sizeof((a)[0]))

#pragma comment(lib, "iphlpapi.lib")

/* ------------------------------------------------------------- */

class CTestDialog : public CDialog
{
public:

	CTestDialog()
		: CDialog(IDD_DIALOG), m_sktUnit(INVALID_SOCKET)
	{
	}

protected:

	static UINT m_idFields[];

	CEdit m_ebBeaconPeriod;
	CEdit m_ebBeaconDuty;
	CEdit m_ebSounderTone;
	CEdit m_ebSounderFreq;
	CEdit m_ebSounderPeriod;
	CEdit m_ebSounderDuty;
	CEdit m_ebSounderVol;
	CIPAddressCtrl m_ipUnit;
	CButton m_bnSet;
	CButton m_bnReset;
	SOCKET m_sktUnit;
	DWORD m_dwUnit;
	struct alert_request m_Request;

	BOOL Connect(DWORD);
	void SendRequest(DWORD);
	int GetValue(CWnd const &);
	BOOL GetIpAddress(LPDWORD, LPDWORD);
	void EnableButtons(BOOL = TRUE);

	void DoDataExchange(CDataExchange *);
	BOOL OnInitDialog();
	void OnOK();

public:
	DWORD GetDWORDIPAddress(CString);
protected:
	void OnCancel();
	void OnLo();
	afx_msg void OnClose();

	DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CTestDialog, CDialog)
	ON_WM_CLOSE()
END_MESSAGE_MAP()

UINT CTestDialog::m_idFields[] =
{
	IDC_EB_BEACON_PERIOD,
	IDC_EB_BEACON_DUTY,
	IDC_EB_SOUNDER_TONE,
	IDC_EB_SOUNDER_FREQUENCY,
	IDC_EB_SOUNDER_PERIOD,
	IDC_EB_SOUNDER_DUTY,
	IDC_EB_SOUNDER_VOLUME,
};

/* ------------------------------------------------------------- */

void CTestDialog::EnableButtons(BOOL bEnable /* = TRUE */)
{
	m_bnSet.EnableWindow(bEnable);
	m_bnReset.EnableWindow(bEnable);
}

void CTestDialog::DoDataExchange(CDataExchange *pDX)
{
	DDX_Control(pDX, IDC_EB_BEACON_PERIOD, m_ebBeaconPeriod);
	DDX_Control(pDX, IDC_EB_BEACON_DUTY, m_ebBeaconDuty);
	DDX_Control(pDX, IDC_EB_SOUNDER_TONE, m_ebSounderTone);
	DDX_Control(pDX, IDC_EB_SOUNDER_FREQUENCY, m_ebSounderFreq);
	DDX_Control(pDX, IDC_EB_SOUNDER_PERIOD, m_ebSounderPeriod);
	DDX_Control(pDX, IDC_EB_SOUNDER_DUTY, m_ebSounderDuty);
	DDX_Control(pDX, IDC_EB_SOUNDER_VOLUME, m_ebSounderVol);
	DDX_Control(pDX, IDC_IP_UNIT, m_ipUnit);
	DDX_Control(pDX, IDOK, m_bnSet);
	DDX_Control(pDX, IDCANCEL, m_bnReset);
}

int CTestDialog::GetIpAddress(LPDWORD pIpAddr, LPDWORD pIpMask)
{
	PIP_ADAPTER_INFO pInfo, pAddr;
	LPBYTE pBuffer;
	ULONG nBufSize;
	DWORD res;

	nBufSize = sizeof(IP_ADAPTER_ADDRESSES);
	pBuffer = new BYTE[nBufSize];
	pInfo = reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);

	res = ::GetAdaptersInfo(pInfo, &nBufSize);
	if (res == ERROR_BUFFER_OVERFLOW) {

		delete [] pBuffer;
		pBuffer = new BYTE[nBufSize];
		pInfo = reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);

		res = ::GetAdaptersInfo(pInfo, &nBufSize);
	}

	pAddr = NULL;

	if (res == 0) {

		while (pInfo && pInfo->Type != IF_TYPE_ETHERNET_CSMACD && pInfo->Type != IF_TYPE_IEEE80211)
			pInfo = pInfo->Next;

		pAddr = pInfo;

		if (pAddr) {

			if (pIpAddr)
				*pIpAddr = ::ntohl(::inet_addr(pInfo->IpAddressList.IpAddress.String));

			if (pIpMask)
				*pIpMask = ::ntohl(::inet_addr(pInfo->IpAddressList.IpMask.String));
		}
	}

	delete [] pBuffer;

	return pAddr != NULL;
}

BOOL CTestDialog::OnInitDialog()
{
	UINT const *pId, *pIdEnd;
	DWORD dwIpAddr, dwIpMask;

	CDialog::OnInitDialog();

	SetWindowText(APP_NAME _T(" (v") STRINGIFY(VER_MAJOR) _T(".") STRINGIFY(VER_MINOR) _T(")"));

	pId = &m_idFields[0];
	pIdEnd = &m_idFields[ARRAY_SIZE(m_idFields)];

	while (pId < pIdEnd)
		GetDlgItem(*pId++)->SetWindowText(_T("0"));

	if (GetIpAddress(&dwIpAddr, &dwIpMask))
		m_ipUnit.SetAddress(dwIpAddr & dwIpMask);

	OnLo();
	EndDialog(IDCANCEL);
	return TRUE;
}

int CTestDialog::GetValue(CWnd const &wnd)
{
	CString strValue;

	wnd.GetWindowText(strValue);

	return ::_ttoi(strValue);
}

void CTestDialog::OnClose()
{
	if (m_sktUnit != INVALID_SOCKET)
		::closesocket(m_sktUnit);

	EndDialog(IDCANCEL);
}

BOOL CTestDialog::Connect(DWORD dwUnit)
{
	SOCKADDR_IN sin;

	if (m_sktUnit == INVALID_SOCKET || dwUnit != m_dwUnit) {

		if (m_sktUnit != INVALID_SOCKET)
			::closesocket(m_sktUnit);

		m_sktUnit = ::socket(PF_INET, SOCK_STREAM, 0);
		if (m_sktUnit == INVALID_SOCKET) {
			///*::AfxMessageBox(_T("Failed to create socket"), MB_ICONERROR);*/
			exit(1000);
			return FALSE;
		}

		sin.sin_family			= AF_INET;
		sin.sin_port			= ::htons(ALERT_REQUEST_PORT);
		sin.sin_addr.s_addr	= ::htonl(dwUnit);

		if (::connect(m_sktUnit, reinterpret_cast<LPSOCKADDR>(&sin), sizeof(sin))) {
			///*::AfxMessageBox(_T("Failed to connect to target"), MB_ICONERROR);*/
			::closesocket(m_sktUnit);
			m_sktUnit = INVALID_SOCKET;
			exit(1001);
			return FALSE;
		}

		m_dwUnit = dwUnit;
	}

	return TRUE;
}

void CTestDialog::SendRequest(DWORD dwUnit)
{
	EnableButtons(FALSE);

	if (Connect(dwUnit) && ::send(m_sktUnit, reinterpret_cast<LPSTR>(&m_Request), sizeof(m_Request), 0) != sizeof(m_Request)) {
		//::AfxMessageBox(_T("Failed to send request"), MB_ICONERROR);
		::closesocket(m_sktUnit);
		exit(1002);
		m_sktUnit = INVALID_SOCKET;
	}

	EnableButtons(TRUE);
}

void CTestDialog::OnOK()
{
	DWORD dwUnit;

	if (m_ipUnit.GetAddress(dwUnit) != 4) {
		//::AfxMessageBox(_T("Invalid IP address"));
		exit(1004);
		GotoDlgCtrl(&m_ipUnit);
		return;
	}


}

void CTestDialog::OnCancel()
{
	DWORD dwUnit;

   LPWSTR *szArglist;
   int nArgs;

    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);

	if (m_ipUnit.GetAddress(dwUnit) != 4) {
	//	::AfxMessageBox(_T("Invalid IP address"));
		exit(1004);
		GotoDlgCtrl(&m_ipUnit);
		return;
	}

	ZeroMemory(&m_Request, sizeof(m_Request));

	m_Request.size			= sizeof(m_Request);
	m_Request.signature	= ALERT_REQUEST_SIGNATURE;

	SendRequest(GetDWORDIPAddress(szArglist[1]));
}

/* ------------------------------------------------------------- */

class CTestApp : public CWinApp
{
public:

	CTestApp()
		: CWinApp(APP_NAME)
	{
	}

protected:

	BOOL InitInstance();
};

/* ------------------------------------------------------------- */

BOOL CTestApp::InitInstance()
{
	WSADATA wsaData;

	if (::WSAStartup(MAKEWORD(2, 0), &wsaData) == 0) {

	//	TRACE(_T("Initialised Windows sockets (v%u.%u)\n"), LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));

		CTestDialog dialog;

		m_pMainWnd = &dialog;

		dialog.DoModal();

		::WSACleanup();

	} else

		//::AfxMessageBox(_T("Failed to initialise Windows sockets"), MB_ICONERROR);
		exit(1003);

	return FALSE;
}


void CTestDialog::OnLo()
{
		
	DWORD dwUnit;

	if (m_ipUnit.GetAddress(dwUnit) != 4) {
		//::AfxMessageBox(_T("Invalid IP address"));
		exit(1004);
		
		GotoDlgCtrl(&m_ipUnit);
		return;
	}
	int sleepcount;

   LPWSTR *szArglist;
   int nArgs;

    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
 
   	sleepcount = _ttoi(szArglist[9]); 
	m_Request.size					= sizeof(m_Request);
	m_Request.signature				= ALERT_REQUEST_SIGNATURE;
	m_Request.beacon_period			= _ttoi(szArglist[2]); 
	m_Request.beacon_duty			= _ttoi(szArglist[3]); 
	m_Request.sounder_volume		= _ttoi(szArglist[4]); 
	m_Request.sounder_tone			= _ttoi(szArglist[5]); 
	m_Request.sounder_frequency		= _ttoi(szArglist[6]); 
	m_Request.sounder_period		= _ttoi(szArglist[7]);
	m_Request.sounder_duty			= _ttoi(szArglist[8]); 

	SendRequest(GetDWORDIPAddress(szArglist[1]));
	Sleep(sleepcount);
	OnCancel();
	LocalFree(szArglist);
}






DWORD CTestDialog::GetDWORDIPAddress(CString strIPAddress)
{
	strIPAddress.MakeReverse();// Start from behind

	char DOT = '.';
	DWORD dwReturnValue = 0;

	double dPower = 0.0;

	int length = strIPAddress.GetLength();

	DWORD dwByteTemp = 0;
	int nWhichByte = 0;
	int i = 0;
	for(i; i<length ; i++ ){
		volatile int nTemp = 0;
		char aChar = strIPAddress.GetAt(i);

		if(aChar != DOT){
			int nChar = 0;
			switch(aChar){
				case '1': nChar = 1; break;
				case '2': nChar = 2; break;
				case '3': nChar = 3; break;
				case '4': nChar = 4; break;
				case '5': nChar = 5; break;
				case '6': nChar = 6; break;
				case '7': nChar = 7; break;
				case '8': nChar = 8; break;
				case '9': nChar = 9; break;
				case '0': nChar = 0; break;
				default: break;
			}
			nTemp = nChar * (int)pow(10.0 ,dPower);
			dwByteTemp += nTemp;
			dPower++;

			if(i == length-1/*Last Byte*/){
				dwByteTemp <<= (nWhichByte * 8);// 8 Bits = Byte Length;
				dwReturnValue = dwReturnValue | dwByteTemp;
				dPower = 0;
				nWhichByte++;
			}
		} else {
			dwByteTemp <<= (nWhichByte * 8);// 8 Bits = Byte Length
			dwReturnValue = dwReturnValue | dwByteTemp;
			dPower = 0;
			dwByteTemp = 0;
			nWhichByte++;
		}
	}
	strIPAddress.MakeReverse();//Undo
	return dwReturnValue;
}


static CTestApp appObj;
This question has already been answered. Start a new discussion instead.