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

📄 server.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 server
//
// To compile:
//	cl /c nbcommon.c
//      cl -o server server.c nbcommon.obj netapi32.lib
//
// To run:
//      server SERVER_NAME
//
// Desription:
//	This program is a simple NetBIOS echo server.  It enumerates and 
//  	resets each LANA.  The server name is registered with each LANA 
//	number, and it then starts listening on each LANA number for 
//	client connections.
//

#include "nbcommon.h"

//
// This define is for what clients the server will allow to connect.
// The asterisk denotes that we will allow any client to connect.  If
// we specified a name (e.g. "FOO") then only a client that has 
// registered that name will be allowed to connect.
//
// These names are 16 characters long. The 16th character is special
// and should be greater that 20h.
//
#define INCOMING_CLIENTS    "*               "

//
// Global variables for the server name
//
char *endpoint=NULL, 
     *requests=NULL;

//**************************************************************************
// Function: HandleThread
//
// Description:
//	This function handles an incoming client connection. A pointer to an
// 	NCB structure is passed in which contains info about the connection 
//	(LSN and LANA, for example). It reads data from the client and then 
//	echoes it back.
//**************************************************************************
DWORD WINAPI HandleClient(LPVOID lpParam)
{
    PNCB       pncb = (PNCB)lpParam;
    PUCHAR     data;

    // Read some data.  NULL means that the session has been disconnected.
    //
    if ((data = Receive(pncb)) != NULL)
    {
        printf("READ: %s\n", data);

        Send(pncb, data);

        Hangup(pncb);
    }
    GlobalFree(data);
    GlobalFree(pncb);

    ExitThread(0);
    return 0;
}

//**************************************************************************
// Function: Listen
//
// Description:
//	This function initializes an NCB structure and posts a NCBLISTEN.
// 	When a client connects, a thread is spawned to handle the connection 
//	and the NCBLISTEN is re-posted.
//**************************************************************************
DWORD WINAPI Listen(LPVOID lpParam)
{
    NCB     ncb;
    int     dwRetCode,
	    lana=(int)lpParam;
	
    while (1)
    {
	// Initialize the NCB structure for listening.  This is important.
	//
        ZeroMemory(&ncb, sizeof(NCB));
	//
	// This is the name of the server
	//
        FillMemory(ncb.ncb_name, sizeof(ncb.ncb_name), ' ');
        CopyMemory(ncb.ncb_name, endpoint, strlen(endpoint));
	//
	// This is the name(s) of the clients we will allow to connect to us
	//
        FillMemory(ncb.ncb_callname, sizeof(ncb.ncb_callname), ' ');
        CopyMemory(ncb.ncb_callname, requests, strlen(requests));
	//
	// Set the LANA number and the command
	//
        ncb.ncb_lana_num = lana;
        ncb.ncb_command = NCBLISTEN;

        dwRetCode = Netbios(&ncb);

        if (dwRetCode != NRC_GOODRET)
        {
            printf("Error in listen: %d lana: %d\n", ncb.ncb_retcode, lana);
            return 0;
        }
        else
        {
            PNCB    lpNCB=NULL;
            HANDLE  hThread;
            DWORD   dwThreadId;

	    //
   	    // Allocate and copy an NCB structure
	    //
            lpNCB = (PNCB)GlobalAlloc(GMEM_FIXED, sizeof(NCB));
            memcpy(lpNCB, &ncb, sizeof(NCB));
	    //
   	    // Spawn a thread to handle the client
	    // 
	    printf("Got a connection on LANA: %d\n", lana);
            hThread = CreateThread(NULL, 0, HandleClient, (void *)lpNCB, 0, &dwThreadId);
            if (hThread == NULL)
            {
                printf("Unable to create thread\n");
                ExitProcess(0);
            }
            CloseHandle(hThread);
        }
    }

    return 1;
}

//**************************************************************************
// Function: main
//
// Description:
//	This function enumerates all the LANAs, resets each LANA, and
// 	starts a Listen thread on each LANA.
//**************************************************************************
int main(int argc, char **argv)
{
    HANDLE	hThread;
    LANA_ENUM   lenum;
    DWORD	dwThreadId;
    int         i;

    if (argc != 2) 
    {
        printf("usage: server server-name\n");
        ExitProcess(0);
    }
    //
    // Setup some global variables.	
    //
    endpoint = argv[1];			// name of the server
    requests = INCOMING_CLIENTS;	// name of the clients that we
					//  will allow to connect to our
					//  server. An '*' mean anyone.

    //
    // Enumerate and reset each LANA.  You always want to do resets before
    //  registering names on a LANA especially under Win95. Under Win95,
    //  a reset on one LANA trashes the name table on other LANAs as well.
    //
    EnumerateLANA(&lenum);
    for(i=0; i < lenum.length ;i++)
        Reset(lenum.lana[i]);
    //
    // Start a listen on each LANA. Create a thread for 'n-1' lanas.  
    // After this loop we will start a Listen on the n-th lana (just so
    //  we don't have to spawn n threads and have the main loop spin
    //  in a 'while (1);' loop.
    //
    for(i=0; i < lenum.length-1 ;i++)
    {
    	AddName(lenum.lana[i], argv[1]);

	hThread = CreateThread(NULL, 0, Listen, (void *)lenum.lana[i], 0, &dwThreadId);
	CloseHandle(hThread);
    }
    AddName(lenum.lana[lenum.length-1], argv[1]);

    Listen((void *)lenum.lana[lenum.length-1]);
    
    return 0;
}

⌨️ 快捷键说明

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