⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.c

📁 NetBios Network programming
💻 C
字号:
/******************************************************************************\
*       This is a part of the Microsoft Source Code Samples.
*       Copyright (C) 1997 Microsoft Corporation.
*       All rights reserved.
*       This source code is only intended as a supplement to
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the
*       Microsoft sample programs.
\******************************************************************************/
//
// Simple netbios client
//
// To compile:
//	cl /c nbcommon.c
//      cl -o client client.c nbcommon.obj netapi32.lib
//
// To run:
//      client CLIENT_NAME SERVER_NAME
//
// Desription:
//	This program enumerates and resets all LANA numbers on the computer
//  	and issues a connect (NCBCALL) on each LANA to the specified server.
//  	The NCBCALL is issued asynchronously with events.  The main thread 
//	waits for one of the events to become signaled at which time it 
//	cancels the other outstanding connections and sends data to the 
//	server.
//
//	There is a possibility of more than on connect succeeding. In this 
//	case we take the first connection whose event is signaled and cancel
//	the other connection.  The server will also receive two connects
//	but one will fail on an operation with NRC_SCLOSED. The server is
//	robust enough to handle this without a problem.
//
#include "nbcommon.h"

#define TEST_DATA	"This is a test of the emergency broadcasting system"

//**************************************************************************
// Function: Connect
//
// Description:
//	This function issues an asynchronous connect from the specified
// 	client to the specified server using the NCB structure passed in.
// 	The connection is requested on the LANA number set in the NCB 
//	structure passed into the function.
//**************************************************************************
void Connect(PNCB pncb, char *client, char *endpoint)
{
    int     dwRetCode;
    
    //
    // This is the name of our client process.
    //
    FillMemory(pncb->ncb_name, sizeof(pncb->ncb_name), ' ');
    CopyMemory(pncb->ncb_name, client, strlen(client));
    //
    // This is the name of the server we wish to connect to.
    //
    FillMemory(pncb->ncb_callname, sizeof(pncb->ncb_callname), ' ');
    CopyMemory(pncb->ncb_callname, endpoint, strlen(endpoint));

    printf("Connecting on LANA: %d\n", pncb->ncb_lana_num);

    pncb->ncb_command = NCBCALL | ASYNCH;

    dwRetCode = Netbios(pncb);

    if (pncb->ncb_retcode != NRC_PENDING)
        printf("Return code from connect: %d\n", pncb->ncb_retcode);

    return;
}

//**************************************************************************
// Function: main
//
// Description:
//	This function enumerates the LANAs, resets each LANA, and 
// 	registers the client name on each LANA. Then it issues a connect on
// 	each LANA and then blocks for the first one to connect.  The other
// 	outstanding connections are cancelled and data is sent to the server
// 	on the good connection.
//**************************************************************************
int main(int argc, char **argv)
{
    LANA_ENUM   lenum;		// LANA enumeration structure
    HANDLE     *events;		// Events for asynch NCBCALL (connects)
    NCB        *ncbs;		// NCB structs for each LANA
    int         i, 		// loop var
		conn, 		// are we connected?
		done=0;		// are we done?
    PUCHAR      data;		// Pointer to data we will read

    if (argc != 3)
    {
        printf("usage: client client-name server-name\n");
        ExitProcess(0);
    }
    // Enumerate all LANA numbers for this machine
    //
    EnumerateLANA(&lenum);
    //
    // Allocate event HANDLEs and NCB structures (one for each LANA)
    //
    events = (HANDLE *)GlobalAlloc(GMEM_FIXED, sizeof(HANDLE) *lenum.length);
    ncbs   = (NCB    *)GlobalAlloc(GMEM_FIXED, sizeof(NCB) * lenum.length);
    //
    // Loop and allocate event HANDLEs and NCB structs.  
    // Also reset each LANA number.
    //
    for(i=0; i < lenum.length ;i++)
    {
        ZeroMemory(&(ncbs[i]), sizeof(NCB));
        events[i] = CreateEvent(NULL, TRUE, FALSE, NULL);

        ncbs[i].ncb_lana_num = lenum.lana[i];

        ncbs[i].ncb_event = events[i];

        Reset(lenum.lana[i]);
    }
    //
    // Add the client process's name to each LANA number.
    // This isn't done in the above loop because under Win95 you have to
    //  do all your resets up front and then add names.  There is a bug
    //  in 95 where if you reset LANA 0, for example, it will trash the
    //  name tables of all other LANAs.
    //
    for(i=0; i < lenum.length ;i++)
        AddName(lenum.lana[i], argv[1]);
    //
    // Issue a connect (NCBCALL) on each LANA number.
    //
    for(i=0; i < lenum.length ;i++)
    {
        Connect(&(ncbs[i]), argv[1], argv[2]);
    }

    while (!done)
    {
	// Wait for one of the events to become signaled
	//
        conn = WaitForMultipleObjects(lenum.length, events, FALSE, INFINITE);
	//
	// Make sure the NCB corresponding to the signaled event has a good
	// return code.
	//
        if (ncbs[conn].ncb_retcode == NRC_GOODRET)
        {
            printf("CONNECTED! on lana: %d\n", ncbs[conn].ncb_lana_num);
            done = 1;
            //
   	    // Send the data to the server
	    //
            Send(&(ncbs[conn]), TEST_DATA);
	    //
	    // Read the data back and print it
	    //
            data = Receive(&(ncbs[conn]));
        
            printf("ECHO: %s\n", data);
	
	    Hangup(&(ncbs[conn]));
        }
        else
        {
	    // Otherwise, we got a bad return code, reset event and wait
	    //  for another connection to complete.
  	    //
            ResetEvent(events[conn]);
        }
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -