服务器端:
#include
#include
#include
#define PORT 5010
#define MSGSIZE 1024
int BytesSum = 0;
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParameter);
int main()
{
WSADATA wsaData;
SOCKET sListen, sClient;
SOCKADDR_IN local, client;
DWORD dwThreadId;
// Initialize Windows socket library
WSAStartup(0x0202, &wsaData);
// Create listening socket
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Bind
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
bind(sListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
// Listen
listen(sListen, 3);
// Create worker thread
CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
while (TRUE)
{
// Accept a connection
int iaddrSize = sizeof(client);
sClient = accept(sListen, (sockaddr*)&client, &iaddrSize);
printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// Add socket to g_CliSocketArr
g_CliSocketArr[g_iTotalConn++] = sClient;
}
return 0;
}
DWORD WINAPI WorkerThread(LPVOID lpParam)
{
int i = 0;
fd_set fdread;
int ret = 0;
struct timeval tv = {1, 0};
char szMessage[MSGSIZE];
while (TRUE)
{
FD_ZERO(&fdread);
for (i = 0; i < g_iTotalConn; i++)
{
FD_SET(g_CliSocketArr[i], &fdread);//把所有的socket都塞到fd_set中
}
// We only care read event
ret = select(0, &fdread, NULL, NULL, &tv);//检测是否有数据可读
if (ret == 0)
{
// Time expired
continue;
}
for (i = 0; i < g_iTotalConn; i++)
{
memset(szMessage,0,MSGSIZE);
if (FD_ISSET(g_CliSocketArr[i], &fdread))//对有数据可读的那个套接字 进行处理
{
// A read event happened on g_CliSocketArr
ret = recv(g_CliSocketArr[i], szMessage, MSGSIZE, 0);
if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))
{//处理发生错误的时候
// Client socket closed
printf("Client socket %d closed.\n", g_CliSocketArr);
closesocket(g_CliSocketArr[i]);
if (i < g_iTotalConn - 1)
{
g_CliSocketArr[i--] = g_CliSocketArr[--g_iTotalConn];
}
}
else
{
szMessage[ret] = '\0';
printf("BytesSum(%d) read Data:%s\n",BytesSum, szMessage);
//send(g_CliSocketArr[i], szMessage, ret, 0);
}
}
}
}//while
return 0;
}
/**
* main.c
* socket client
* @date : 2011/11/27
* @author : MJN
*/
#include
#include
#pragma comment(lib, "ws2_32")
void CleanUpSocket( SOCKET clientSocket )
{
printf("SocketError:%d\n",WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
}
int BytesSum = 0;
int main()
{
int iResult;
WORD wVersionRequested;
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in clientService;
wVersionRequested = MAKEWORD(2, 2);
/* initialize windows socket */
iResult = WSAStartup(wVersionRequested, &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return -1;
}
else
{
printf("WSAStartup succeeded!\n");
}
/* create client tcp socket */
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET)
{
printf("create socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return -1;
} else
{
printf("create socket(%d) succeeded!\n", clientSocket);
}
/* connect to server */
clientService.sin_family = AF_INET;
clientService.sin_port = htons(5010);
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
iResult = connect(clientSocket, (SOCKADDR *) &clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR)
{
CleanUpSocket(clientSocket);
return -1;
}
else
{
printf("connect succeed!\n");
}
char lBuf[100] = "Hello Kitty!";
int i = 0;
while(++i < 20)
{
int lResult = send(clientSocket, lBuf, strlen(lBuf)+1,0);
if (SOCKET_ERROR == lResult)
{
CleanUpSocket(clientSocket);
return 0;
}
else
{
BytesSum += lResult;
printf("BytesSum(%d) Bytes Send:%d\n",BytesSum,lResult);
}
}
closesocket(clientSocket);
WSACleanup();
return 0;
}