📄 nbserver.cpp
字号:
//
// Sample: Non-Blocking IPv4/IPv6 Server
//
// Files:
// nbserver.cpp - this file
// resolve.cpp - routines for resovling addresses, etc.
// resolve.h - header file for resolve.c
//
// Description:
// This sample illustrates simple blocking IO for TCP and UDP for
// both IPv4 and IPv6. This sample uses the getaddrinfo/getnameinfo
// APIs which allows this application to be IP agnostic. That is the
// desired address family (AF_INET or AF_INET6) can be determined
// simply from the string address passed via the -l command.
//
// This sample is single threaded! For TCP, a listening socket is
// created for each available address family which then enters the
// select loop. If the listening sockets are signaled for read
// notification then a client connection has been accepted. The socket
// is accepted and then added the the FD_SET. From there its a matter
// of checking for which events occured on what sockets and taking
// the appropriate action. To handle the receive data, these buffers
// are added to a queue for each socket connection. When a client socket
// is signaled for write, any data pending on the queue is written.
//
// For UDP the principle is the same except there are no listning sockets,
// just a single socket for each available address family.
//
// For example:
// If this sample is called with the following command lines:
// nbserver.exe -l fe80::2efe:1234 -e 5150
// nbserver.exe -l ::
// Then the server creates an IPv6 socket as an IPv6 address was
// provided.
//
// On the other hand, with the following command line:
// nbserver.exe -l 7.7.7.1 -e 5150
// nbserver.exe -l 0.0.0.0
// Then the server creates an IPv4 socket.
//
// Compile:
// cl -o nbserver.exe nbserver.cpp resolve.cpp ws2_32.lib
//
// Usage:
// nbserver.exe [options]
// -a 4|6 Address family, 4 = IPv4, 6 = IPv6 [default = IPv4]
// -b size Size of send/recv buffer in bytes
// -e port Port number
// -l addr Local address to bind to [default INADDR_ANY for IPv4 or INADDR6_AN
// -p proto Which protocol to use [default = TCP]
// tcp Use TCP protocol
// udp Use UDP protocol
//
// define this before include winsock2.h to up the allowed size
#define FD_SET_SIZE 1024
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "resolve.h"
#define DEFAULT_BUFFER_SIZE 4096 // default buffer size
int gAddressFamily = AF_UNSPEC, // default to unspecified
gSocketType = SOCK_STREAM, // default to TCP socket type
gProtocol = IPPROTO_TCP, // default to TCP protocol
gBufferSize = DEFAULT_BUFFER_SIZE;
char *gBindAddr = NULL, // local interface to bind to
*gBindPort = "5150"; // local port to bind to
//
// Allocated for each receiver posted
//
typedef struct _BUFFER_OBJ
{
char *buf; // Data buffer for data
int buflen; // Length of buffer or number of bytes contained in buffer
SOCKADDR_STORAGE addr; // Address data was received from (UDP)
int addrlen; // Length of address
struct _BUFFER_OBJ *next; // Used to maintain a linked list of buffers
} BUFFER_OBJ;
//
// Allocated for each socket handle
//
typedef struct _SOCKET_OBJ
{
SOCKET s; // Socket handle
int listening; // Socket is a listening socket (TCP)
int closing; // Indicates whether the connection is closing
SOCKADDR_STORAGE addr; // Used for client's remote address
int addrlen; // Length of the address
BUFFER_OBJ *pending, // List of pending buffers to be sent
*pendingtail; // Last entry in buffer list
struct _SOCKET_OBJ *next, // Used to link socket objects together
*prev;
} SOCKET_OBJ;
SOCKET_OBJ *gSocketList=NULL, // Linked list of all sockets allocated
*gSocketListEnd=NULL; // End of socket list
int gSocketCount=0; // Number of socket objects in list
//
// Statistics counters
//
volatile LONG gBytesRead=0,
gBytesSent=0,
gStartTime=0,
gBytesReadLast=0,
gBytesSentLast=0,
gStartTimeLast=0,
gCurrentConnections=0;
//
// Function: usage
//
// Description:
// Prints usage information and exits the process.
//
void usage(char *progname)
{
fprintf(stderr, "usage: %s [-a 4|6] [-e port] [-l local-addr] [-p udp|tcp]\n",
progname);
fprintf(stderr, " -a 4|6 Address family, 4 = IPv4, 6 = IPv6 [default = IPv4]\n"
" -b size Buffer size for send/recv [default = %d]\n"
" -e port Port number [default = %s]\n"
" -l addr Local address to bind to [default INADDR_ANY for IPv4 or INADDR6_ANY for IPv6]\n"
" -p tcp|udp Which protocol to use [default = TCP]\n",
gBufferSize,
gBindPort
);
ExitProcess(-1);
}
//
// Function: GetBufferObj
//
// Description:
// Allocate a BUFFER_OBJ. Each receive posted by a receive thread allocates
// one of these. After the recv is successful, the BUFFER_OBJ is queued for
// sending by the send thread. Again, lookaside lists may be used to increase
// performance.
//
BUFFER_OBJ *GetBufferObj(int buflen)
{
BUFFER_OBJ *newobj=NULL;
// Allocate the object
newobj = (BUFFER_OBJ *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BUFFER_OBJ));
if (newobj == NULL)
{
fprintf(stderr, "GetBufferObj: HeapAlloc failed: %d\n", GetLastError());
ExitProcess(-1);
}
// Allocate the buffer
newobj->buf = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BYTE) *buflen);
if (newobj->buf == NULL)
{
fprintf(stderr, "GetBufferObj: HeapAlloc failed: %d\n", GetLastError());
ExitProcess(-1);
}
newobj->buflen = buflen;
newobj->addrlen = sizeof(newobj->addr);
return newobj;
}
//
// Function: FreeBufferObj
//
// Description:
// Free the buffer object.
//
void FreeBufferObj(BUFFER_OBJ *obj)
{
HeapFree(GetProcessHeap(), 0, obj->buf);
HeapFree(GetProcessHeap(), 0, obj);
}
//
// Function: GetSocketObj
//
// Description:
// Allocate a socket object and initialize its members. A socket object is
// allocated for each socket created (either by socket or accept). The
// socket objects mantain a list of all buffers received that need to
// be sent.
//
SOCKET_OBJ *GetSocketObj(SOCKET s, int listening)
{
SOCKET_OBJ *sockobj=NULL;
sockobj = (SOCKET_OBJ *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SOCKET_OBJ));
if (sockobj == NULL)
{
fprintf(stderr, "GetSocketObj: HeapAlloc failed: %d\n", GetLastError());
ExitProcess(-1);
}
// Initialize the members
sockobj->s = s;
sockobj->listening = listening;
sockobj->addrlen = sizeof(sockobj->addr);
return sockobj;
}
//
// Function: FreeSocketObj
//
// Description:
// Frees a socket object along with any queued buffer objects.
//
void FreeSocketObj(SOCKET_OBJ *obj)
{
BUFFER_OBJ *ptr=NULL,
*tmp=NULL;
ptr = obj->pending;
while (ptr)
{
tmp = ptr;
ptr = ptr->next;
FreeBufferObj(tmp);
}
HeapFree(GetProcessHeap(), 0, obj);
}
//
// Function: InsertSocketObj
//
// Description:
// Insert a socket object into the list of socket objects. Note that
// no synchronization is performed because this app is single threaded!
//
void InsertSocketObj(SOCKET_OBJ *sock)
{
sock->next = sock->prev = NULL;
if (gSocketList == NULL)
{
// List is empty
gSocketList = gSocketListEnd = sock;
}
else
{
// Non-empty; insert at the end
sock->prev = gSocketListEnd;
gSocketListEnd->next = sock;
gSocketListEnd = sock;
}
gSocketCount++;
}
//
// Function: RemoveSocketObj
//
// Description:
// Remove a socket object from the list of sockets. No synchronization is
// is performed since this app is single threaded.
//
void RemoveSocketObj(SOCKET_OBJ *sock)
{
if (sock->prev)
{
sock->prev->next = sock->next;
}
if (sock->next)
{
sock->next->prev = sock->prev;
}
if (gSocketList == sock)
gSocketList = sock->next;
if (gSocketListEnd == sock)
gSocketListEnd = sock->prev;
gSocketCount--;
}
//
// Function: EnqueueBufferObj
//
// Description:
// Queue up a receive buffer for this connection.
//
void EnqueueBufferObj(SOCKET_OBJ *sock, BUFFER_OBJ *obj, BOOL AtHead)
{
if (sock->pending == NULL)
{
// Queue is empty
sock->pending = sock->pendingtail = obj;
}
else if (AtHead == FALSE)
{
// Put new object at the end
sock->pendingtail->next = obj;
sock->pendingtail = obj;
}
else
{
// Put new object at the head
obj->next = sock->pending;
sock->pending = obj;
}
}
//
// Function: DequeueBufferObj
//
// Description:
// Remove a BUFFER_OBJ from the given connection's queue for sending.
//
BUFFER_OBJ *DequeueBufferObj(SOCKET_OBJ *sock)
{
BUFFER_OBJ *ret=NULL;
if (sock->pendingtail != NULL)
{
// Queue is non empty
ret = sock->pending;
sock->pending = sock->pending->next;
if (sock->pendingtail == ret)
{
// Item is the only item in the queue
sock->pendingtail = NULL;
}
}
return ret;
}
//
// Function: ValidateArgs
//
// Description:
// Parses the command line arguments and sets up some global
// variables.
//
void ValidateArgs(int argc, char **argv)
{
int i;
for(i=1; i < argc ;i++)
{
if (((argv[i][0] != '/') && (argv[i][0] != '-')) || (strlen(argv[i]) < 2))
usage(argv[0]);
else
{
switch (tolower(argv[i][1]))
{
case 'a': // address family - IPv4 or IPv6
if (i+1 >= argc)
usage(argv[0]);
if (argv[i+1][0] == '4')
gAddressFamily = AF_INET;
else if (argv[i+1][0] == '6')
gAddressFamily = AF_INET6;
else
usage(argv[0]);
i++;
break;
case 'b': // buffer size for send/recv
if (i+1 >= argc)
usage(argv[0]);
gBufferSize = atol(argv[++i]);
break;
case 'e': // endpoint - port number
if (i+1 >= argc)
usage(argv[0]);
gBindPort = argv[++i];
break;
case 'l': // local address for binding
if (i+1 >= argc)
usage(argv[0]);
gBindAddr = argv[++i];
break;
case 'p': // protocol - TCP or UDP
if (i+1 >= argc)
usage(argv[0]);
if (_strnicmp(argv[i+1], "tcp", 3) == 0)
{
gProtocol = IPPROTO_TCP;
gSocketType = SOCK_STREAM;
}
else if (_strnicmp(argv[i+1], "udp", 3) == 0)
{
gProtocol = IPPROTO_UDP;
gSocketType = SOCK_DGRAM;
}
else
usage(argv[0]);
i++;
break;
default:
usage(argv[0]);
break;
}
}
}
}
//
// Function: ReceivePendingData
//
// Description:
// Receive data pending on the socket into a SOCKET_OBJ buffer. Enqueue
// the buffer into the socket object for sending later. This routine returns
// -1 indicating that the socket is no longer valid and the calling function
// should clean up (remove) the socket object. Zero is returned for success.
//
int ReceivePendingData(SOCKET_OBJ *sockobj)
{
BUFFER_OBJ *buffobj=NULL;
int rc,
ret;
// Get a buffer to receive the data
buffobj = GetBufferObj(gBufferSize);
ret = 0;
if (gProtocol == IPPROTO_TCP)
{
rc = recv(
sockobj->s,
buffobj->buf,
buffobj->buflen,
0
);
}
else
{
rc = recvfrom(
sockobj->s,
buffobj->buf,
buffobj->buflen,
0,
(SOCKADDR *)&buffobj->addr,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -