📄 nbdgram.c
字号:
else
AddGroupName(lenum.lana[i], szLocalName, &dwNum[i]);
}
}
// We are sending datagrams
//
if (bSender)
{
// Broadcast sender
//
if (bBroadcast)
{
if (bOneLana)
{
// Broadcast the message on the one LANA only
//
for(j = 0; j < dwNumDatagrams; j++)
{
wsprintf(szMessage,
"[%03d] Test broadcast datagram", j);
if (DatagramSendBC(dwOneLana, dwNum[0],
szMessage, strlen(szMessage))
!= NRC_GOODRET)
return 1;
Sleep(dwDelay);
}
}
else
{
// Broadcast the message on every LANA on the local
// machine
//
for(j = 0; j < dwNumDatagrams; j++)
{
for(i = 0; i < lenum.length; i++)
{
wsprintf(szMessage,
"[%03d] Test broadcast datagram", j);
if (DatagramSendBC(lenum.lana[i], dwNum[i],
szMessage, strlen(szMessage))
!= NRC_GOODRET)
return 1;
}
Sleep(dwDelay);
}
}
}
else
{
if (bOneLana)
{
// Send a directed message to the one LANA specified
//
for(j = 0; j < dwNumDatagrams; j++)
{
wsprintf(szMessage,
"[%03d] Test directed datagram", j);
if (DatagramSend(dwOneLana, dwNum[0],
szRecipientName, szMessage,
strlen(szMessage)) != NRC_GOODRET)
return 1;
Sleep(dwDelay);
}
}
else
{
// Send a directed message to the each LANA on the
// local machine
//
for(j = 0; j < dwNumDatagrams; j++)
{
for(i = 0; i < lenum.length; i++)
{
wsprintf(szMessage,
"[%03d] Test directed datagram", j);
printf("count: %d.%d\n", j,i);
if (DatagramSend(lenum.lana[i], dwNum[i],
szRecipientName, szMessage,
strlen(szMessage)) != NRC_GOODRET)
return 1;
}
Sleep(dwDelay);
}
}
}
}
else // We are receiving datagrams
{
NCB *ncb=NULL;
char **szMessageArray = NULL;
HANDLE *hEvent=NULL;
DWORD dwRet;
// Allocate an array of NCB structure to submit to each recv
// on each LANA
//
ncb = (NCB *)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
sizeof(NCB) * lenum.length);
//
// Allocate an array of incoming data buffers
//
szMessageArray = (char **)GlobalAlloc(GMEM_FIXED,
sizeof(char *) * lenum.length);
for(i = 0; i < lenum.length; i++)
szMessageArray[i] = (char *)GlobalAlloc(GMEM_FIXED,
MAX_DATAGRAM_SIZE);
//
// Allocate an array of event handles for
// asynchronous receives
//
hEvent = (HANDLE *)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
sizeof(HANDLE) * lenum.length);
for(i = 0; i < lenum.length; i++)
hEvent[i] = CreateEvent(0, TRUE, FALSE, 0);
if (bBroadcast)
{
if (bOneLana)
{
// Post synchronous broadcast receives on
// the one LANA specified
//
for(j = 0; j < dwNumDatagrams; j++)
{
if (DatagramRecvBC(&ncb[0], dwOneLana, dwNum[0],
szMessageArray[0], MAX_DATAGRAM_SIZE,
NULL) != NRC_GOODRET)
return 1;
FormatNetbiosName(ncb[0].ncb_callname, szSender);
printf("%03d [LANA %d] Message: '%s' "
"received from: %s\n", j,
ncb[0].ncb_lana_num, szMessageArray[0],
szSender);
}
}
else
{
// Post asynchronous broadcast receives on each LANA
// number available. For each command that succeeded
// print the message otherwise cancel the command.
//
for(j = 0; j < dwNumDatagrams; j++)
{
for(i = 0; i < lenum.length; i++)
{
dwBytesRead = MAX_DATAGRAM_SIZE;
if (DatagramRecvBC(&ncb[i], lenum.lana[i],
dwNum[i], szMessageArray[i],
MAX_DATAGRAM_SIZE, hEvent[i])
!= NRC_GOODRET)
return 1;
}
dwRet = WaitForMultipleObjects(lenum.length,
hEvent, FALSE, INFINITE);
if (dwRet == WAIT_FAILED)
{
printf("WaitForMultipleObjects failed: %d\n",
GetLastError());
return 1;
}
for(i = 0; i < lenum.length; i++)
{
if (ncb[i].ncb_cmd_cplt == NRC_PENDING)
Cancel(&ncb[i]);
else
{
ncb[i].ncb_buffer[ncb[i].ncb_length] = 0;
FormatNetbiosName(ncb[i].ncb_callname,
szSender);
printf("%03d [LANA %d] Message: '%s' "
"received from: %s\n", j,
ncb[i].ncb_lana_num,
szMessageArray[i], szSender);
}
ResetEvent(hEvent[i]);
}
}
}
}
else
{
if (bOneLana)
{
// Make a blocking datagram receive on the specified
// LANA number.
//
for(j = 0; j < dwNumDatagrams; j++)
{
if (bRecvAny)
{
// Receive data destined for any NetBIOS name
// in this process's name table.
//
if (DatagramRecv(&ncb[0], dwOneLana, 0xFF,
szMessageArray[0], MAX_DATAGRAM_SIZE,
NULL) != NRC_GOODRET)
return 1;
}
else
{
if (DatagramRecv(&ncb[0], dwOneLana,
dwNum[0], szMessageArray[0],
MAX_DATAGRAM_SIZE, NULL)
!= NRC_GOODRET)
return 1;
}
FormatNetbiosName(ncb[0].ncb_callname, szSender);
printf("%03d [LANA %d] Message: '%s' "
"received from: %s\n", j,
ncb[0].ncb_lana_num, szMessageArray[0],
szSender);
}
}
else
{
// Post asynchronous datagram receives on each LANA
// available. For all those commands that succeeded,
// print the data, otherwise cancel the command.
//
for(j = 0; j < dwNumDatagrams; j++)
{
for(i = 0; i < lenum.length; i++)
{
if (bRecvAny)
{
// Receive data destined for any NetBIOS
// name in this process's name table.
//
if (DatagramRecv(&ncb[i], lenum.lana[i],
0xFF, szMessageArray[i],
MAX_DATAGRAM_SIZE, hEvent[i])
!= NRC_GOODRET)
return 1;
}
else
{
if (DatagramRecv(&ncb[i], lenum.lana[i],
dwNum[i], szMessageArray[i],
MAX_DATAGRAM_SIZE, hEvent[i])
!= NRC_GOODRET)
return 1;
}
}
dwRet = WaitForMultipleObjects(lenum.length,
hEvent, FALSE, INFINITE);
if (dwRet == WAIT_FAILED)
{
printf("WaitForMultipleObjects failed: %d\n",
GetLastError());
return 1;
}
for(i = 0; i < lenum.length; i++)
{
if (ncb[i].ncb_cmd_cplt == NRC_PENDING)
Cancel(&ncb[i]);
else
{
ncb[i].ncb_buffer[ncb[i].ncb_length] = 0;
FormatNetbiosName(ncb[i].ncb_callname,
szSender);
printf("%03d [LANA %d] Message: '%s' "
"from: %s\n", j, ncb[i].ncb_lana_num,
szMessageArray[i], szSender);
}
ResetEvent(hEvent[i]);
}
}
}
}
// Clean up
//
for(i = 0; i < lenum.length; i++)
{
CloseHandle(hEvent[i]);
GlobalFree(szMessageArray[i]);
}
GlobalFree(hEvent);
GlobalFree(szMessageArray);
}
// Clean things up
//
if (bOneLana)
DelName(dwOneLana, szLocalName);
else
{
for(i = 0; i < lenum.length; i++)
DelName(lenum.lana[i], szLocalName);
}
GlobalFree(dwNum);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -