TCP的select服务器
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/select.h>
#include<sys/time.h>
#define IP "192.168.0.116"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建套接字:socket
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速被重复使用,当检测到该端口号没有被进程正常占用时候
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器的IP和端口:必须绑定:bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//将套接字设置为被动监听状态:lieten
if (listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
fd_set readfds,tempfds;
FD_ZERO(&readfds); //清空集合
//将需要的文件描述符添加到集合中去
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd = sfd;
struct sockaddr_in saveCin[1024-4]; //另存连接成功的客户端的地址信息
struct sockaddr_in cin; //储存连接成功的客户端信息
socklen_t addrlen = sizeof(cin);
int newfd = -1;
char buf[128] = "";
ssize_t res = 0;
int s_res = 0;
while(1)
{
tempfds = readfds;
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__);
for(int i = 0;i <= maxfd;i++)
{
if(FD_ISSET(i,&tempfds) == 0) //i所代表的文件描述符不在集中
{
continue;
}
if(0 == i)
{
printf("触发键盘输入事件>>>");
fflush(stdout);
int sndfd;
res = scanf("%d %s",&sndfd,buf);
while(getchar() != '\n');
if(res != 0)
{
fprintf(stderr,"请输入正确格式:fd string\n");
continue;
}
//若输入的sndfd非法,或者根本没有对应的客户端,则不发送
if(sndfd <= sfd || sndfd >= 1024 || !FD_ISSET(sndfd,&readfds))
{
fprintf(stderr,"sndfd = %d 是非法的文件描述符\n",sndfd);
continue;
}
if(send(sndfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send to %d success\n",sndfd);
}
else if(sfd == i)
{
printf("触发客户端连接事件>>>");
fflush(stdout);
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d 连接成功 __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
saveCin[newfd - 4] = cin;
//将newfd添加到readfds集合中
FD_SET(newfd,&readfds);
//更新maxfd
maxfd = maxfd > newfd?maxfd:newfd;
}
else
{
fprintf(stderr,"触发客户端交互事件>>>");
bzero(buf,sizeof(buf));
//接收
res = recv(i,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 ==res)
{
printf("[%s : %d] newfd = %d 客户端下线 __%d__\n",\
inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i,__LINE__);
//关闭文件描述符
close(i);
FD_CLR(i,&readfds);
int j = maxfd;
for( ; j > 0; j--);
{
if(FD_ISSET(j,&readfds))
{
break;
}
}
maxfd = j;
continue;
}
printf("[%s : %d] newfd = %d %s__%d__\n",\
inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i,buf,__LINE__);
//发送
strcat(buf,"*_*");
if(send(i,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
}
}
//关闭文件描述符
close(sfd);
return 0;
}
TCP的poll客户端
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include<sys/wait.h>
#include<signal.h>
#include<poll.h>
#define IP "192.168.0.116"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建套接字:socket
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//连接服务器
if(connect(sfd,(struct sockaddr*)&sin ,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
struct pollfd fds[2];
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = sfd;
fds[1].events = POLLIN;
char buf[128] = "";
ssize_t res = 0;
int p_res = -1;
while(1)
{
p_res = poll(fds,2,-1);
if(p_res < 0)
{
ERR_MSG("poll");
return -1;
}
else if(0 == p_res)
{
printf("time out...\n");
break;
}
if(fds[0].revents & POLLIN)
{
printf("触发键盘输入事件\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送
if(send(sfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
break;
}
printf("发送成功\n");
}
if(fds[1].revents &POLLIN)
{
printf("触发服务器交互事件\n");
bzero(buf,sizeof(buf));
//接收
res = recv(sfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线\n");
break;
}
printf(" : %s__%d__\n",buf,__LINE__);
}
}
//关闭
close(sfd);
return 0;
}
TCP的select客户端
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include<sys/wait.h>
#include<signal.h>
#include<poll.h>
#include<sys/time.h>
#include<sys/select.h>
#define IP "192.168.0.116"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建套接字:socket
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体
//该真实结构体根据地址制定
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//连接服务器
if(connect(sfd,(struct sockaddr*)&sin ,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
fd_set readfds,tempfds;
FD_ZERO(&readfds); //清空集合
FD_ZERO(&tempfds);
//将需要的文件描述符添加到集合中去
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd = sfd;
struct sockaddr_in cin; //储存连接成功的客户端信息
socklen_t addrlen = sizeof(cin);
int newfd = -1;
char buf[128] = "";
ssize_t res = 0;
int s_res = 0;
while(1)
{
tempfds = readfds;
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__);
for(int i = 0;i <= maxfd;i++)
{
if(FD_ISSET(i,&tempfds) == 0)
{
continue;
}
if(0 == i)
{
//触发键盘输入事件
bzero(buf,sizeof(buf));
printf("请输入要发送的数据\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送
if(send(sfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
break;
}
printf("发送成功\n");
}
else if(sfd == i)
{
bzero(buf,sizeof(buf));
//接收
res = recv(sfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线\n");
break;
}
printf(" : %s__%d__\n",buf,__LINE__);
}
}
}
//关闭文件描述符
close(sfd);
return 0;
}