TCP虽然可以实现 IO非阻塞操作,但在实际应用时会对资源是否准备完毕进行循环测试,增加了不必要的CPU占用。
为了解决这种问题,应用多路复用的知识,select()监听信号。相关知识可以参考 linux文件读写 文件锁 select poll。
服务端:
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/time.h>
-
#include <sys/ioctl.h>
-
#include <unistd.h>
-
#include <netinet/in.h>
-
-
#define PORT 4321
-
#define MAX_QUE_CONN_NM 5
-
#define MAX_SOCK_FD FD_SETSIZE
-
#define BUFFER_SIZE 1024
-
-
int main(){
-
struct sockaddr_in server_sockaddr, client_sockaddr;
-
int sin_size, count;
-
fd_set inset, tmp_inset;
-
int sockfd, client_fd, fd;
-
char buf[BUFFER_SIZE];
-
-
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
-
perror("socket");
-
exit(1);
-
}
-
-
server_sockaddr.sin_family = AF_INET;
-
server_sockaddr.sin_port = htons(PORT);
-
server_sockaddr.sin_addr.s_addr = INADDR_ANY;
-
bzero(&(server_sockaddr.sin_zero), 8);
-
-
int i = 1;
-
-
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
-
if (bind(sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr)) == -1){
-
perror("bind");
-
exit(1);
-
}
-
-
if(listen(sockfd, MAX_QUE_CONN_NM) == -1){
-
perror("listen");
-
exit(1);
-
}
-
printf("listening....\n");
-
-
FD_ZERO(&inset);
-
-
FD_SET(sockfd, &inset);
-
-
while(1){
-
tmp_inset = inset;
-
sin_size=sizeof(struct sockaddr_in);
-
memset(buf, 0, sizeof(buf));
-
printf("run while...\n");
-
-
if (!(select(MAX_SOCK_FD, &tmp_inset, NULL, NULL, NULL) > 0)){
-
perror("select");
-
close(sockfd);
-
exit(1);
-
}
-
printf("run for...\n");
-
for (fd = 0; fd < MAX_SOCK_FD; fd++){
-
if (FD_ISSET(fd, &tmp_inset) > 0) {
-
printf("fd is %d ,socket is %d\n",fd,sockfd);
-
if (fd == sockfd){
-
if ((client_fd = accept(sockfd, (struct sockaddr *)&client_sockaddr, &sin_size))== -1){
-
perror("accept");
-
exit(1);
-
}
-
-
FD_SET(client_fd, &inset);
-
printf("New connection from %d(socket)\n", client_fd);
-
}else{
-
if ((count = recv(fd, buf, BUFFER_SIZE, 0)) > 0){
-
printf("Received a message from %d: %s\n", fd, buf);
-
}else{
-
close(fd);
-
FD_CLR(fd, &inset);
-
printf("Client %d(socket) has left\n", fd);
-
}
-
}
-
}
-
}
-
}
-
-
close(sockfd);
-
exit(0);
-
}
客户端:
-
<span style="font-size:18px;">#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/ioctl.h>
-
#include <unistd.h>
-
#include <netdb.h>
-
#include <netinet/in.h>
-
-
#define PORT 4321
-
#define BUFFER_SIZE 1024
-
-
int main(int argc, char *argv[]){
-
int sockfd, sendbytes;
-
char buf[BUFFER_SIZE];
-
struct hostent *host;
-
struct sockaddr_in serv_addr;
-
-
if(argc < 3){
-
fprintf(stderr,"USAGE: ./client Hostname(or ip address) Text\n");
-
exit(1);
-
}
-
-
if ((host = gethostbyname(argv[1])) == NULL){
-
perror("gethostbyname");
-
exit(1);
-
}
-
-
memset(buf, 0, sizeof(buf));
-
sprintf(buf, "%s", argv[2]);
-
-
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
-
perror("socket");
-
exit(1);
-
}
-
-
serv_addr.sin_family = AF_INET;
-
serv_addr.sin_port = htons(PORT);
-
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
-
bzero(&(serv_addr.sin_zero), 8);
-
-
if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr))== -1){
-
perror("connect");
-
exit(1);
-
}
-
-
if ((sendbytes = send(sockfd, buf, strlen(buf), 0)) == -1){
-
perror("send");
-
exit(1);
-
}
-
sleep(30);
-
close(sockfd);
-
exit(0);
-
}
阅读(1305) | 评论(0) | 转发(0) |