1.select服务器
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP "192.168.8.101" //服务器 IP
#define PORT 8888 //端口号
#define MAX 128
#define ERROR(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, char *argv[])
{
int sfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size;
//1.创建流式套接字
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERROR("socket");
return -1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
memset(&client_addr, 0, sizeof(struct sockaddr_in));
//填充地址信息结构体
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); //端口的网络字节序
server_addr.sin_addr.s_addr = inet_addr(IP); //IP的网络字节序
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERROR("setsockopt");
return -1;
}
//2.将地址信息绑定到套接字上
if (bind(sfd, (struct sockaddr *) &server_addr,
sizeof(struct sockaddr_in)) < 0)
{
ERROR("bind");
return -1;
}
//3.将sfd设置为被动监听状态
if (listen(sfd, 128) < 0)
{
ERROR("listen");
return -1;
}
printf("服务器正在监听....\n");
client_addr_size = sizeof(struct sockaddr_in);
char buf[MAX] = "";
ssize_t res = 0;
fd_set readfds,tempfds;
struct sockaddr_in arrinfo[1024-4];
int maxfd;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
maxfd = sfd;
int newfd = 0;
while(1)
{
tempfds = readfds;
if(select(maxfd+1, &tempfds, NULL, NULL, NULL) < 0)
{
ERROR("selecr");
return -1;
}
for(int i=0; i<=maxfd; i++)
{
if(0 == FD_ISSET(i, &tempfds))
{
continue;
}
if(0 == i)
{
printf("键盘输入事件\n");
int sendfd;
res = scanf("%d %s", &sendfd, buf);
while(getchar() != '\n');
if(res != 2)
{
printf("请按照正确格式输入:文件描述符 内容\n");
continue;
}
if(strcmp(buf, "quit") == 0)
{
exit(0);
}
if(sendfd > sfd && FD_ISSET(sendfd, &readfds))
{
if(send(sendfd, buf, sizeof(buf), 0) < 0)
{
ERROR("send");
return -1;
}
printf("发送成功\n");
}
else
{
fprintf(stderr, "sendfd=%d 没有对应的客户端\n",sendfd);
}
}
else if(sfd == i)
{
printf("客户端连接成\n");
newfd = accept(sfd, (struct sockaddr*)&client_addr,&client_addr_size);
if(newfd < 0)
{
ERROR("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功\n",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port),newfd);
arrinfo[newfd-4] = client_addr;
FD_SET(newfd, &readfds);
maxfd = maxfd>newfd ? maxfd:newfd;
}
else
{
printf("客户端交互\n");
bzero(buf, sizeof(buf));
res = recv(i, buf, sizeof(buf), 0);
if(res < 0)
{
ERROR("recv");
return -1;
}
else if(0 == res)
{
printf("客户端已关闭\n");
printf("[%s:%d] newfd=%d\n", \
inet_ntoa(arrinfo[i-4].sin_addr), \
ntohs(arrinfo[i-4].sin_port), i);
close(i);
for(int j = maxfd; j>0; j--)
{
if(FD_ISSET(j, &readfds))
{
maxfd = j;
break;
}
}
}
else
{
printf("[%s:%d] newfd=%d 收到的消息是:%s \n",inet_ntoa(arrinfo[i-4].sin_addr),
ntohs(arrinfo[i-4].sin_port), i, buf);
}
}
}
}
close(sfd);
return 0;
}
2.poll客户端
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.8.101"
int main(int argc, const char *argv[])
{
//创建流式套接字
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(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_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;
ssize_t res = 0;
char buf[128] = "";
int pres = 0;
while(1)
{
pres = poll(fds, 2, -1);
if(pres < 0)
{
ERR_MSG("poll");
return -1;
}
else if(0 == pres)
{
printf("time out...\n");
break;
}
//0号文件描述符有事件产生
if(fds[0].revents & POLLIN)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//发送
if(send(sfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("sen");
return -1;
}
printf("send success\n");
}
//sfd有事件产生,即服务器发送信息给客户端
if(fds[1].revents & POLLIN)
{
//接收
res = recv(sfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器断开连接\n");
break;
}
printf(":%s\n", buf);
}
}
close(sfd);
return 0;
}