Netbios编程(三):异步事件客户端

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关闭连接。类似于异步事件服务器的操作。

完。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值