select1

 

// Select模型.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// write by larry
// 2009-8-20
// This is server using select model.
#include "stdafx.h"
#include <winsock.h>
#include <stdio.h>
#define PORT  5150
#define MSGSIZE  1024
#pragma comment(lib, "ws2_32.lib")
int g_iTotalConn = 0;
SOCKET g_CliSocketArr[FD_SETSIZE];
DWORD WINAPI WorkerThread(LPVOID lpParam);
int main(int argc, char* argv[])
{
 WSADATA wsaData;
 SOCKET sListen, sClient;
 SOCKADDR_IN local, client;
 int iAddrSize = sizeof(SOCKADDR_IN);
 DWORD dwThreadId;
 // Initialize windows socket library
 WSAStartup(0x0202, &wsaData);
 // Create listening socket
 sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 // Bind
 local.sin_family = AF_INET;
 local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
 local.sin_port = htons(PORT);
 bind(sListen, (sockaddr*)&local, sizeof(SOCKADDR_IN));
 // Listen
 listen(sListen, 3);
 // Create worker thread
 CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
 while (TRUE)
 {
  // Accept a connection
  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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值