Netbios客户端的在设计上类似于异步事件服务器。步骤如下:
第一步:也是枚举本机上所有可用的LANA
LanaEnum(&lenum);
第二步:重设每个LANA,同样也是调用ResetAll();
第三步:增加客户进程的名字到每个LANA,同样也是调用AddName();
第四步:连接服务器。注意:很明显,服务器进程一定要先启动,否则连接谁呢?!O(∩_∩)O哈!
//连接函数
int Connect(PNCB pncb,int lana,char *server,char *client)
{
pncb->ncb_command=NCBCALL|ASYNCH;
pncb->ncb_lana_num=lana;
memset(pncb->ncb_name,' ',NCBNAMSZ);
strncpy((char *)pncb->ncb_name,client,strlen(client));
memset(pncb->ncb_callname,' ',NCBNAMSZ);
strncpy((char *)pncb->ncb_callname,server,strlen(server));
if(Netbios(pncb)!=NRC_GOODRET)
{
printf("Error:Netbios:NCBCONNECT :%d/n",pncb->ncb_retcode);
return pncb->ncb_retcode;
}
return NRC_GOODRET;
}
第五步:等待连接建立。
//
//Wait for at least one connection to succeed,if more than one object became signaled during call
//the return value is the array index of the signaled object with the smallest index value of all
//the signaled objects,so we should close other sessions.
//
dwIndex=WaitForMultipleObjects(lenum.length,hArray,FALSE,INFINITE);
if(dwIndex==WAIT_FAILED)
{
printf("Error:WaitForMultipleObjects:%d/n",GetLastError());
}
else
{
//
//if more than one connections succeeds,hang up the extra
//connections。if it's still pending,
//cancel it.
//
for(i=0;i<lenum.length;i++)
if(i!=dwIndex)
{
if(pncb[i].ncb_cmd_cplt==NRC_PENDING)
Cancel(&pncb[i]);//cancel a NCB command
else
Hangup(pncb[i].ncb_lana_num,pncb[i].ncb_lsn);//close a session
}
}
第六步:接受或发送数据,之后调用Hangup关闭连接。类似于异步事件服务器的操作。
完。