BOOL CMonitor::OnInitDialog()
{
BOOL bReturn = TRUE;
CDialog::OnInitDialog();
CString strTitle = m_strMachineName;
strTitle += " ";
strTitle += m_strApplicationName;
this->SetWindowText(strTitle);
WSAPROTOCOL_INFO ProtocolInfo;
int iCommand = SERVICE_START_VISUALIZER;
DWORD dwID = -1;
CString strTemp;
//CAsynch
SOCKET Sock;
// create the socket
Sock = socket(PF_INET, SOCK_STREAM, 0);
if (Sock != INVALID_SOCKET)
{
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddr.sin_port = 0;
if (bind(Sock, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
ASSERT(0);
}
strTemp = m_strMachineName;
if (strTemp.IsEmpty() || strTemp == "")
{
strTemp.Empty();
//get the local host name
gethostname(strTemp.GetBuffer(256), 256);
strTemp.ReleaseBuffer();
}
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(strTemp);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost;
lphost = gethostbyname(strTemp);
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
else
{
ASSERT(0);
//WSASetLastError(WSAEINVAL);
//return false;
}
}
sockAddr.sin_port = htons((u_short)MPI_SERVICE_PORT);
if (connect(Sock, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
{
AfxMessageBox("Connection not established");
return 0;
}
if (SOCKET_ERROR == send(Sock, (char*)&iCommand, sizeof(int), 0))
{
AfxMessageBox("Sending the iCommand Failed");
return 0;
}
if (SOCKET_ERROR == send(Sock, (char*)&m_dwProcessID, sizeof(DWORD), 0))
{
AfxMessageBox("Sending the dwProcessID Failed");
return 0;
}
DWORD dwmyProcess = GetCurrentProcessId();
if (SOCKET_ERROR == send(Sock, (char*)&dwmyProcess, sizeof(DWORD), 0))
{
AfxMessageBox("Sending my Current Process ID Failed");
return 0;
}
DWORD dwNProc = 0;
if (SOCKET_ERROR == recv(Sock, (char*)&dwNProc, sizeof(DWORD), 0))
{
AfxMessageBox("Receiving the dwNProc Failed");
return 0;
}
if (dwNProc == 0)
{
AfxMessageBox("dwNProc = 0");
}
//get all of the structures for the nprocesses
for (UINT i =0; i < dwNProc; ++i)
{
memset(&ProtocolInfo, 0, sizeof(ProtocolInfo));
recv(Sock, (char*)&ProtocolInfo, sizeof(ProtocolInfo), 0);
SOCKET tempsock = WSASocket(PF_INET, SOCK_STREAM, 0, &ProtocolInfo, 0, 0);
// create the socket
if (tempsock != INVALID_SOCKET)
m_SockArray.Add(tempsock);
else
{
int iError = WSAGetLastError();
CString strMessage, strMessageFirst;
LPTSTR pMsg = strMessage.GetBuffer(256);
translate_WSAErrorCode(iError, pMsg);
strMessage.ReleaseBuffer();
strMessageFirst = "WSASocket tried to create the structure, returned : ";
strMessageFirst += strMessage;
AfxMessageBox(strMessageFirst);
}
}