select函数调用示例
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#define BUF_SIZE 30
int main(int argc, char *argv[])
{
fd_set reads, temps;
int result;
long str_len;
char buf[BUF_SIZE];
struct timeval timeout;
FD_ZERO(&reads);
FD_SET(0, &reads);
while (1) {
temps = reads;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
result = select(1, &temps, 0, 0, &timeout);
if (result == -1) {
puts("select() error");
break;
}
else if(result == 0){
puts("time out");
}
else{
if (FD_ISSET(0, &temps)) {
str_len = read(0, buf, BUF_SIZE);
buf[str_len] = 0 ;
printf("message from console : %s", buf);
}
}
}
return 0;
}
基于Windows实现I/O复用服务器端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#define BUF_SIZE 1024
void ErrorHandling(char *message);
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET hServSock, hClntSock;
SOCKADDR_IN servAdr, clntAdr;
TIMEVAL timeout;
fd_set reads, cpyReads;
int adrSz;
int strLen, fdNum, i;
char buf[BUF_SIZE];
if (argc != 2)
{
printf("Usage : %s <port> \n", argv[0]);
exit(1);
}
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
ErrorHandling("WSAStartup() error!");
}
hServSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hServSock == INVALID_SOCKET)
{
ErrorHandling("socket() error!");
}
memset(&servAdr, 0, sizeof(servAdr));
servAdr.sin_family = AF_INET;
servAdr.sin_addr.s_addr = htonl(INADDR_ANY);
servAdr.sin_port = htons(atoi(argv[1]));
if (bind(hServSock, (SOCKADDR *) &servAdr, sizeof(servAdr)) == SOCKET_ERROR)
{
ErrorHandling("bind() error!");
}
if (listen(hServSock, 5) == SOCKET_ERROR)
{
ErrorHandling("listen() error!");
}
FD_ZERO(&reads);
FD_SET(hServSock, &reads);
while (1)
{
cpyReads = reads;
timeout.tv_sec = 5;
timeout.tv_usec = 5000;
if ((fdNum = select(0, &cpyReads, 0, 0, &timeout)) == SOCKET_ERROR)
{
break;
}
if (fdNum == 0)
{
continue;
}
for (i = 0; i < reads.fd_count; i++)
{
if (FD_ISSET(reads.fd_array[i], &cpyReads))
{
if (reads.fd_array[i] == hServSock)
{
adrSz = sizeof(clntAdr);
hClntSock = accept(hServSock, (SOCKADDR *)&clntAdr, &adrSz);
if (hClntSock == INVALID_SOCKET)
{
ErrorHandling("accept() error!");
}
FD_SET(hClntSock, &reads);
printf("connected client : %d \n", hClntSock);
}
else
{
strLen = recv(reads.fd_array[i], buf, BUF_SIZE - 1, 0);
if (strLen == 0)
{
FD_CLR(reads.fd_array[i], &reads);
closesocket(cpyReads.fd_array[i]);
printf("closed client : %d \n", cpyReads.fd_array[i]);
}
else
{
send(reads.fd_array[i], buf, strLen, 0);
}
}
}
}
}
closesocket(hServSock);
WSACleanup();
system("PAUSE");
return 0;
}
void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
基于Linux客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 1024
void error_handing(char *message);
void echo_client(int argc, const char * argv[]);
void test_gethostbyname();
int main(int argc, const char * argv[])
{
echo_client(argc, argv);
return 0;
}
void error_handing(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
void echo_client(int argc, const char * argv[])
{
int sock;
char message[BUF_SIZE];
long str_len, recv_len, recv_cnt;
struct sockaddr_in serv_adr;
if (argc != 3)
{
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
{
error_handing("socket() error");
}
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = PF_INET;
serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
serv_adr.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr)))
{
error_handing("connect() error");
}
else
{
puts("Connected..........");
}
while (1)
{
fputs("Input message(Q to quit) : ", stdout);
fgets(message, BUF_SIZE, stdin);
if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
{
break;
}
str_len = write(sock, message, strlen(message));
recv_len = 0;
while (recv_len < str_len) {
recv_cnt = read(sock, message, BUF_SIZE-1);
if (recv_cnt == -1) {
error_handing("read() error");
}
recv_len += recv_cnt;
}
message[str_len] = 0;
printf("Message from server: %s", message);
}
close(sock);
}