学习winsock api,参照书上写的server端的例子。
源码如下:
/*
*File:Server.cpp
*
*/
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5150
#define DEFAULT_BUFFER 4096
int iPort = DEFAULT_PORT;
BOOL bInterface = TRUE ;
BOOL bRecvOnly = FALSE;
char szAddress[128];
//Function:usage
//Description:
// Print usage information and exit
void usage()
{
printf("usage:server [-p:x] [-i:ip] [-o]/n/n");
printf(" -p:x Port number to listen on/n");
printf(" -i:str Interface to listen on/n");
printf(" -o Dont't echo the data back/n");
printf("camel 2004-8-29");
ExitProcess(1);
}
// Function:
void ValidateArgs(int argc,char **argv)
{
int i;
for (i = 1; i < argc ;i++)
{
if ((argv[i][0]=='-') || (argv[1][0]=='/') )
{
switch (tolower(argv[i][1]))
{
case 'p':
iPort = atoi(&argv[i][3]);
break;
case 'i':
bInterface = TRUE;
if(strlen(argv[i]) > 3)
strcpy(szAddress,&argv[i][3]);
break;
case 'o':
bRecvOnly = TRUE;
break;
default:
usage();
break;
}
}
}
}
//
DWORD WINAPI ClientThread(LPVOID lpParam)
{
SOCKET sock = (SOCKET)lpParam;
char szBuff[DEFAULT_BUFFER];
int ret,
nLeft,
idx;
while(1)
{
//perform a blocking recv() call
ret = recv(sock,szBuff,DEFAULT_BUFFER,0);
if (ret==0) //Graceful close
break;
else if (ret==SOCKET_ERROR)
{
printf("recv() failed: %d/n",WSAGetLastError());
break;
}
szBuff[ret] = '/0';
printf("RECV:'%S'/n",szBuff);
//
//If we selected to echo the data back,do it
//
if(!bRecvOnly)
{
nLeft = ret;
idx = 0;
//
//Make sure we write all the data
//
while(nLeft > 0)
{
ret = send(sock,&szBuff[idx],nLeft,0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
printf("send() failed: %d/n",WSAGetLastError());
break;
}
nLeft -= ret;
idx += ret;
}
}
}
return 0;
}
int main(int argc,char ** argv)
{
printf("Server.cpp/n");
WSADATA wsd;
SOCKET sListen,
sClient;
int iAddrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local,client;
ValidateArgs(argc,argv);
if (WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("Failed to load Winsock!/n");
return 1;
}
//
//Create out listening socket
//
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if (sListen == SOCKET_ERROR)
{
printf("socket() failed: %d/n",WSAGetLastError());
return 1;
}
//Select the local interface,and bind to it
//
if(bInterface)
{
local.sin_addr.s_addr = inet_addr(szAddress);
if(local.sin_addr.s_addr == INADDR_NONE)
usage();
}
else
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(iPort);
if (bind(sListen,(struct sockaddr *)&local,sizeof(local)) == SOCKET_ERROR)
{
printf("bing() failed: %d/n",WSAGetLastError());
return 1;
}
listen(sListen,8);
//
//In a continuous loop, wait for incoming clients.once one
//is detected,create a thread and pass the handle off to it.
//
while(1)
{
iAddrSize = sizeof(client);
sClient = accept (sListen,(struct sockaddr *)&client,&iAddrSize);
if(sClient == INVALID_SOCKET)
{
printf("accept() failed:%d/n",WSAGetLastError());
break;
}
printf("Accepted client:%s:%d/n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
hThread = CreateThread(NULL,0,ClientThread,(LPVOID)sClient,0,&dwThreadId);
if (hThread == NULL )
{
printf("CreateThread() failed:%d /n",GetLastError());
break;
}
CloseHandle(hThread);
}
closesocket(sListen);
WSACleanup();
return 0;
}
//--------------------------------------------------------
在VC++6.0下编译时,由于对VC++的编译器不熟。开
始出现winsock api中的函数名不能解析的错误
...
error LNK2001: unresolved external symbol _inet_addr@4
error LNK2001: unresolved external symbol _htons@4
error LNK2001: unresolved external symbol _socket@12
...
通过在工程中加入ws2_32.lib,搞定。
project-->setting-->Link-->Object/Library modules 添加 ws2_32.lib