select_ser.c:
// TCP[select]
// socket >>> setsockopt >>> {sin}bind >>> listen >>> select{accept recv send}
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define IP "192.168.8.45"
#define PORT 6688
#define MAX_CONNECTION 128
int ret = 0;
ssize_t res = 0;
int main(int argc, char const *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0){perror("socket"); return -1;}
else{printf("创建套接字成功...\n");}
int reuse = 1;
ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if(ret < 0){perror("setsockopt"); return -1;}
else{printf("允许快速启用端口成功...\n");}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
ret = bind(sfd, (struct sockaddr *)&sin, sizeof(sin));
if(ret < 0){perror("bind"); return -1;}
else{printf("连接套接字成功...\n");}
ret = listen(sfd, MAX_CONNECTION);
if(ret < 0){perror("listen"); return -1;}
else{printf("监听套接字成功...\n");}
fd_set readfds, tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int maxfd = sfd;
int newfd = -1;
char buf[128] = "";
struct sockaddr_in cin;
cin.sin_family = AF_INET;
socklen_t addrlen = sizeof(cin);
struct sockaddr_in *cinList = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)*(getdtablesize()));
while(1)
{
tempfds = readfds;
ret = select(maxfd+1, &tempfds, NULL, NULL, NULL);
if(ret < 0){perror("select"); continue;}
else if(0 == ret){printf("超时...\n"); continue;}
else
{
for(int i = 0; i < maxfd+1; i++)
{
if(!FD_ISSET(i, &tempfds)){continue;}
if(0 == i)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
for(int j = maxfd; j > sfd; j--)
{
if(FD_ISSET(j, &readfds))
{
res = send(j, buf, sizeof(buf), 0);
if(res < 0){perror("send"); continue;}
else{printf("已通知%d{%s[%d]}\n", \
j, inet_ntoa(cinList[j].sin_addr), htons(cinList[j].sin_port));}
}
}
if(0 == strcasecmp(buf, "quit"))
{
break;
}
}
else if(sfd == i)
{
newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
if(newfd < 0){perror("accept"); continue;}
else
{
FD_SET(newfd, &readfds);
maxfd = maxfd > newfd ? maxfd : newfd;
cinList[newfd] = cin;
printf("客户端{%s[%d]}连接成功...\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
}
}
else
{
bzero(buf, sizeof(buf));
res = recv(i, buf, sizeof(buf), 0);
if(res < 0){perror("recv"); continue;}
else if(0 == res || 0 == strcasecmp(buf, "quit"))
{
printf("客户端{%s[%d]}离开...\n", \
inet_ntoa(cinList[i].sin_addr), htons(cinList[i].sin_port));
FD_CLR(i, &readfds);
if(maxfd == i)
{
int j = -1;
for(j = maxfd-1; j >= 0; j--)
{
if(FD_ISSET(j, &readfds))
{
maxfd = j;
break;
}
}
if(-1 == j){maxfd = -1;}
}
}
else
{
printf("收到{%s[%d]}: %s\n", \
inet_ntoa(cinList[i].sin_addr), htons(cinList[i].sin_port), buf);
}
bzero(buf, sizeof(buf));
}
}
if(0 == strcasecmp(buf, "quit"))
{
printf("服务器离开...\n");
break;
}
}
}
free(cinList);
for(int i = sfd; i < maxfd+1; i++)
{
if(FD_ISSET(i, &readfds)){close(i);}
}
return 0;
}
select_cli.c:
// TCP[select]
// socket >>> bind[omit] >>> {sin}connect >>> select
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/select.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define IP "192.168.8.45"
#define PORT 6688
int ret = 0;
ssize_t res = 0;
int main(int argc, char const *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0){perror("socket"); return -1;}
else{printf("绑定套接字成功...\n");}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = ntohs(PORT);
ret = connect(cfd, (struct sockaddr *)&sin, sizeof(sin));
if(ret < 0){perror("connect"); return -1;}
else{printf("连接套接字成功\n");}
fd_set readfds, tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0, &readfds); // send
FD_SET(cfd, &readfds); // recv
char buf[128] = "";
while(1)
{
tempfds = readfds;
ret = select(cfd+1, &tempfds, NULL, NULL, NULL);
if(ret < 0){perror("select"); break;}
else if(0 == ret){printf("超时...\n"); break;}
else
{
if(FD_ISSET(0, &tempfds))// send
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
res = send(cfd, buf, sizeof(buf), 0);
if(res < 0){perror("send"); continue;}
else{printf("发送{%s}成功...\n", buf);}
if(0 == strcasecmp(buf, "quit"))
{
break;
}
}
if(FD_ISSET(cfd, &tempfds))// recv
{
bzero(buf, sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0){perror("recv"); continue;}
else{printf("收到: %s\n", buf);}
if(0 == strcasecmp(buf, "quit"))
{
printf("服务器离开...\n");
break;
}
}
}
}
close(cfd);
printf("客户端离开...\n");
return 0;
}
poll_cli.c:
// TCP[pool]
// socket >>> bind[omit] >>> {sin}connect >>> poll
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.45"
#define PORT 6688
int ret = 0;
ssize_t res = 0;
int main(int argc, char const *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0){perror("socket"); return -1;}
else{printf("创建套接字成功...\n");}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = ntohs(PORT);
ret = connect(cfd, (struct sockaddr *)&sin, sizeof(sin));
if(ret < 0){perror("connect"); return -1;}
else{printf("连接套接字成功...\n");}
struct pollfd fds[2];
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = cfd;
fds[1].events = POLLIN;
char buf[128] = "";
while(1)
{
ret = poll(fds, 2, -1);
if(ret < 0){perror("poll"); continue;}
else if(0 == ret){printf("超时...\n"); continue;}
else
{
if(fds[0].revents & POLLIN)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
res = send(cfd, buf, sizeof(buf), 0);
if(res < 0){perror("send"); continue;}
else{printf("已发送: %s\n", buf);}
if(0 == strcasecmp(buf, "quit"))
{
break;
}
}
if(fds[1].revents & POLLIN)
{
bzero(buf, sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0){perror("recv"); continue;}
else{printf("收到: %s\n", buf);}
if(0 == strcasecmp(buf, "quit"))
{
printf("服务器离开...\n");
break;
}
}
}
}
close(cfd);
printf("客户端退出...\n");
return 0;
}