select的TCP服務(wù)器代碼
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/time.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__); \
perror(msg);\
}while(0)
#define PORT 8888 //端口號(hào),范圍1024~49151
#define IP "192.168.43.233" //本機(jī)IP,ifconfig
int keybord_events(fd_set readfds);
int cliConnect_events(int, struct sockaddr_in*, fd_set *, int *);
int cliRcvSnd_events(int, struct sockaddr_in*, fd_set *, int* );
int main(int argc, const char *argv[])
{
//創(chuàng)建流式套接字 socket
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d\n", sfd);
//允許端口快速的被復(fù)用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允許端口快速的被復(fù)用成功\n");
//填充地址信息結(jié)構(gòu)體給bind函數(shù)綁定,
//真實(shí)的地址信息結(jié)構(gòu)體根據(jù)地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必須填A(yù)F_INET;
sin.sin_port = htons(PORT); //端口號(hào)的網(wǎng)絡(luò)字節(jié)序
sin.sin_addr.s_addr = inet_addr(IP);//本機(jī)IP
//綁定服務(wù)器的地址信息---> 必須綁定 bind
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//將套接字設(shè)置為被動(dòng)監(jiān)聽狀態(tài) listen
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//創(chuàng)建一個(gè)讀集合
fd_set readfds, tempfds;
//fd_set 本質(zhì)上是一個(gè)結(jié)構(gòu)體,結(jié)構(gòu)體中有一個(gè)整形數(shù)組。
//若不清空,則會(huì)存有隨機(jī)值,可能會(huì)隨機(jī)到有效的,但是不需要監(jiān)測(cè)的文件描述符
//清空集合
FD_ZERO(&readfds);
//將需要監(jiān)測(cè)的文件描述符添加到集合中
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int maxfd = sfd; //存儲(chǔ)最大的文件描述符
int s_res = -1;
ssize_t res = -1;
char buf[128] = "";
struct sockaddr_in saveCin[1024]; //備份連接成功的客戶端的地址信息,用下標(biāo)來(lái)對(duì)應(yīng)文件描述符
while(1)
{
tempfds = readfds;
//執(zhí)行IO多路復(fù)用函數(shù)
s_res = select(maxfd+1, &tempfds, NULL, NULL, NULL);
if(s_res < 0)
{
ERR_MSG("select");
return -1;
}
else if(0 == s_res)
{
printf("time out,,\n");
break;
}
printf("__%d__\n", __LINE__);
//能運(yùn)行到當(dāng)前位置,則代表select函數(shù)解除阻塞,即代表集合中有文件描述符準(zhǔn)備就緒
//此時(shí)集合中會(huì)只剩下產(chǎn)生事件的文件描述符,例如:
//0號(hào)準(zhǔn)備就緒,則集合中會(huì)只剩下0號(hào)
//sfd準(zhǔn)備就緒,則集合中會(huì)只剩下sfd
//0和sfd均準(zhǔn)備就緒,則集合中會(huì)剩下0和sfd;
//只需要判斷集合中剩下哪個(gè)文件描述符,走對(duì)應(yīng)處理函數(shù)即可;
for(int i=0; i<=maxfd; i++)
{
if(FD_ISSET(i, &tempfds) == 0)
continue;
//能運(yùn)行到當(dāng)前位置,則說(shuō)明i所代表的文件描述符在集合中
if(0 == i) //0在集合中
{
printf("觸發(fā)鍵盤輸入事件\n");
keybord_events(readfds);
}
else if(sfd == i) //sfd在集合中
{
printf("觸發(fā)客戶端連接事件\n");
cliConnect_events(sfd, saveCin, &readfds, &maxfd);
}
else
{
printf("觸發(fā)客戶端交互事件\n");
cliRcvSnd_events(i, saveCin, &readfds, &maxfd);
}
}
}
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
//鍵盤輸入事件
int keybord_events(fd_set readfds)
{
char buf[128] = "";
int sndfd = -1; //從終端獲取一個(gè)文件描述符,發(fā)送數(shù)據(jù)給該文件描述符對(duì)應(yīng)的客戶端
bzero(buf, sizeof(buf));
int res = scanf("%d %s", &sndfd, buf);
while(getchar() != 10);
if(res != 2) //終端輸入的數(shù)據(jù)格式錯(cuò)誤
{
printf("輸入數(shù)據(jù)的格式錯(cuò)誤,:fd string\n");
return -1;
}
if(sndfd<=2 || FD_ISSET(sndfd, &readfds)==0) //判斷文件描述符的合法性
{
printf("非法的文件描述符:sndfd=%d\n", sndfd);
return -1;
}
if(send(sndfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
return 0;
}
//客戶端連接事件
int cliConnect_events(int sfd, struct sockaddr_in saveCin[], fd_set *preadfds, int *pmaxfd)
{
int newfd = -1;
struct sockaddr_in cin; //存儲(chǔ)客戶端的地址信息
socklen_t addrlen = sizeof(cin); //真實(shí)的地址信息結(jié)構(gòu)體的大小
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("newfd");
return -1;
}
printf("[%s:%d]客戶端連接成功 newfd=%d\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
saveCin[newfd] = cin; //將cin另存到newfd對(duì)應(yīng)的下標(biāo)位置去
FD_SET(newfd, preadfds); //將newfd添加到集合中
*pmaxfd = *pmaxfd>newfd ? *pmaxfd:newfd; //更新maxfd
return 0;
}
//客戶端交互事件
int cliRcvSnd_events(int fd, struct sockaddr_in* saveCin, fd_set *preadfds, int* pmaxfd)
{
char buf[128] = "";
//清空字符串
bzero(buf, sizeof(buf)); //memset
//接收
ssize_t res = recv(fd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d]客戶端下線 newfd=%d\n", \
inet_ntoa(saveCin[fd].sin_addr), ntohs(saveCin[fd].sin_port), fd);
close(fd); //關(guān)閉文件描述符
FD_CLR(fd, preadfds); //將文件描述符從集合中剔除
//由于剔除的文件描述符可能是最大文件描述符,所以要更新maxfd
//從大往小判斷,若在集合中,則該文件描述符就是最大的文件描述符
/*
for(; *pmaxfd>=0; *pmaxfd--)
{
if(FD_ISSET(*pmaxfd, preadfds))
break;
}
*/
while(FD_ISSET(*pmaxfd, preadfds)==0 && (*pmaxfd)-->=0);
return 0;
}
printf("[%s:%d] newfd=%d : %s\n", \
inet_ntoa(saveCin[fd].sin_addr), ntohs(saveCin[fd].sin_port), fd, buf);
//發(fā)送
strcat(buf, "*_*");
if(send(fd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
return 0;
}
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-729735.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-729735.html
到了這里,關(guān)于select實(shí)現(xiàn)服務(wù)器并發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!