【tcp】使用select-服務端
code:
// // author: lin yiqian
// created: 2009/09/17
// describe: tcp select 服務端
// #include
#include
#include
#pragma comment(lib, "ws2_32.lib")
typedef std::setsocket_set;
void main(void)
if (lobyte(wsadata.wversion) != 2 || hibyte(wsadata.wversion) != 2)
socket socketserver = socket(af_inet, sock_stream, ipproto_tcp);
sockaddr_in addrserver;
addrserver.sin_addr.s_un.s_addr = htonl(inaddr_any);
addrserver.sin_port = htons(9989);
addrserver.sin_family = af_inet;
bind(socketserver, (sockaddr*)&addrserver, sizeof(sockaddr));
listen(socketserver, 4);
socket socketclient;
struct fd_set fdread;
int nsize;
socket_set setsockets;
char szmsg[128];
zeromemory(szmsg, sizeof(szmsg));
int nblockmilli = 0;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
while (true)
fd_zero(&fdread);
for (socket_set::iterator iterset = setsockets.begin(); iterset != setsockets.end(); iterset++)
select(fd_setsize, &fdread, null, null, &timeout);
for (socket_set::iterator iterset = setsockets.begin(); iterset != setsockets.end(); iterset++)
}
}
for (socket_set::iterator iterset = setsockets.begin(); iterset != setsockets.end(); iterset++)
closesocket(socketserver);
wsacleanup();
return;
}
【tcp】使用select-客戶端
code:
// // author: lin yiqian
// created: 2009/09/17
// describe: tcp select 客戶端
// #include
#include
#include
#pragma comment(lib, "ws2_32.lib")
dword winapi clientsocketthread(lpvoid lpparameter);
void main(void)
if (lobyte(wsadata.wversion) != 2 || hibyte(wsadata.wversion) != 2)
system_info systeminfo;
getsysteminfo(&systeminfo);
// 開啟cpu個數2倍的執行緒
for (int i = 0; i < (systeminfo.dwnumberofprocessors * 2); ++i)
closehandle(handle);
}
while (true)
wsacleanup();
return;
}
dword winapi clientsocketthread(lpvoid lpparameter)
closesocket(socketclient);
}
網路程式設計 select模型
select 機制中提供一fd set的資料結構,實際上是一long型別的陣列,每乙個陣列元素都能與一開啟的檔案控制代碼 不管是socket控制代碼,還是其他檔案或命名管道或裝置控制代碼 建立聯絡,建立聯絡的工作由程式設計師完成,當呼叫select 時,由核心根據io狀態修改fe set的內容,由此...
linux 網路程式設計 select
include include include include include include include include include include include include typedef struct st fd def 將fd接收套接字描述符放入到fd陣列中 int add f...
網路程式設計 select介紹
一 select函式簡介 select一般用在socket網路程式設計中,在網路程式設計的過程中,經常會遇到許多阻塞的函式,網路程式設計時使用的recv,recvfrom connect函式都是阻塞的函式,當函式不能成功執行的時候,程式就會一直阻塞在這裡,無法執行下面的 這是就需要用到非阻塞的程式設...