运行方式
服务器端,可以直接运行命令,也可以带参数运行,如果不带参数运行,则程序自主获取主机ip,然后默认设定port和lisnum的值。其中port默认为6666,lisnum默认为5.
- ./socket_select_server IP地址(可选) 端口号(可选)监听队列大小(可选)
- ./socket_select_client 172.18.229.60 6666
服务器代码
- /*************************************************************************
- > File Name: socket_select_server.c
- > Author: genglut
- > Mail: genglut@163.com
- > Created Time: 2014年12月22日 星期一 18时06分26秒
- ************************************************************************/
- /*
- struct sockaddr_in
- {
- short int sin_family; // 地址协议
- in_port_t sin_port; // 端口号
- struct in_addr sin_addr; // IP地址
- unsigned char sin_zero[8]; // 预留位
- };
- struct in_addr
- {
- _u32 s_addr; // 32位地址
- };
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <net/if.h>
- #include <sys/ioctl.h>
- #define MAXBUF 1024
- void get_ip(char * str, char *ip);//获取本地IP地址
- int main(int argc, char *argv[])
- {
- //用于测试命令行输入的值的情况
- //printf("argv[0] = %s\n", argv[0]);
- //printf("argv[1] = %s\n", argv[1]);
- //printf("argv[2] = %s\n", argv[2]);
- //printf("argv[3] = %s\n", argv[3]);
- int sockfd, newfd;
- socklen_t len;
- struct sockaddr_in server_addr, client_addr;//结构体sockaddr_in
- unsigned int server_port, lisnum;//用int也可以
- char buf[MAXBUF + 1];
- //下面是select用到的变量的定义
- fd_set rfds;
- struct timeval tv;
- int retval;
- int maxfd = -1;
- if(argv[1] && argv[2])//要根据argv[1]的情况来判断argv[2]的情况,否则会出错
- server_port = atoi(argv[2]);
- else
- server_port = 6666;
- if(argv[1] && argv[2] && argv[3])//与上面道理相同
- lisnum = atoi(argv[3]);
- else
- lisnum = 5;
- bzero(&server_addr, sizeof(server_addr));//也可以用memset
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(server_port);//转换为网络字节序
- if(argv[1])
- //将ip地址转换为32位网络地址 inet_addr 也可以用inet_aton
- //inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr);
- server_addr.sin_addr.s_addr = inet_addr(argv[1]);
- else
- {
- char ip[128];
- get_ip("eno16777736", ip);
- server_addr.sin_addr.s_addr = inet_addr(ip);
- }
- //建立sockfd
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- //输出ip和port信息,用于测试
- printf("server_ip = %s\nserver_port = %d\nlisnum = %d\n", inet_ntoa(server_addr.sin_addr), server_port, lisnum);
- //绑定sockfd和服务器的IP地址server_addr
- if(bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
- {
- perror("bind");
- exit(EXIT_FAILURE);
- }
- //监听sockfd
- if(listen(sockfd, lisnum) == -1)
- {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- while(1)
- {
- printf("\n---- wait for client connect ---\n");
- //等待接收客户端的连接
- //连接成功后,客户端地址信息存储在client_addr中
- //新建立的socket描述符存储在newfd中
- len = sizeof(struct sockaddr);
- if((newfd = accept(sockfd, (struct sockaddr *)&client_addr, &len)) == -1)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
- //打印客户端地址信息 inet_ntoa ntohs
- printf("server: got connection from %s, port %d, socket %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), newfd);
- while(1)
- {
- FD_ZERO(&rfds);//初始化rfds为空
- FD_SET(0, &rfds);//将标准输入的描述符0加入到集合rfds中
- FD_SET(newfd, &rfds);//将newfd加入到集合rfds中
- maxfd = newfd + 1;
- tv.tv_sec = 1;//阻塞等待时间为1s
- tv.tv_usec = 0;
- retval = select(maxfd, &rfds, NULL, NULL, &tv);//多路复用,同时监测描述符0和newfd
- if(retval == -1)//select函数执行出错
- {
- perror("select");
- exit(EXIT_FAILURE);
- }
- else if(retval == 0)//select函数执行超时
- continue;
- else//有描述符引起异常
- {
- if(FD_ISSET(0, &rfds))//判断是不是标准输入0引起的异常
- {
- bzero(buf, sizeof(buf));//清空buf
- fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入
- if(!strncasecmp(buf, "quit", 4))//判断是否为退出
- {
- printf("i will close the connect!\n");
- break;
- }
- len = send(newfd, buf, strlen(buf)-1, 0);//向客户端发送消息
- if(len > 0)
- {
- printf ("send successful,%d byte send!\n",len);
- }
- else
- {
- printf("message '%s' send failure !\n", buf);
- printf("errno code is %d, errno message is '%s'\n", errno, strerror(errno));
- break;
- }
- }
- if(FD_ISSET(newfd, &rfds))//判断是不是newfd引起的异常
- {
- bzero(buf, sizeof(buf));
- len = recv(newfd, buf, sizeof(buf)-1, 0);//从客户端接收消息
- if(len > 0 )
- printf("message recv successful : '%s', %d Byte recv\n", buf, len);
- else if(len < 0)
- {
- printf("recv failure !\nerrno code is %d, errno message is '%s'\n", errno, strerror(errno));
- break;
- }
- else//如果客户端已关闭
- {
- printf("the other one close quit\n");
- break;
- }
- }
- }
- }
- close(newfd);
- printf("need other connection ? (no -> quit) : ");
- fflush(stdout);
- bzero(buf, sizeof(buf));//清空buf
- fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入
- if(!strncasecmp(buf, "no", 2))//判断是否继续等待连接
- {
- printf("quit!\n");
- break;
- }
- }
- close(sockfd);
- return 0;
- }
- void get_ip(char * str, char *ip)
- {
- int inet_sock;
- struct ifreq ifr;
- inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
- strcpy(ifr.ifr_name, str);//#include <net/if.h>
- if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)//#include <sys/ioctl.h>
- {
- perror("ioctl");
- exit(EXIT_FAILURE);
- }
- sprintf(ip,"%s", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
- close(inet_sock);
- }
客户端代码
- /*************************************************************************
- > File Name: socket_select_client.c
- > Author: genglut
- > Mail: genglut@163.com
- > Created Time: 2014年12月22日 星期一 18时06分06秒
- ************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #define MAXBUF 1024
- int main(int argc, char *argv[])
- {
- int sockfd;
- socklen_t len;
- struct sockaddr_in server_addr;
- char buf[MAXBUF + 1];
- //下面是select用到的变量的定义
- fd_set rfds;
- struct timeval tv;
- int retval;
- int maxfd = -1;
- if(argc != 3)
- {
- printf("error failure, it must be:\n\t\t%s IP port \n", argv[0]);
- exit(EXIT_FAILURE);
- }
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- bzero(&server_addr, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(atoi(argv[2]));
- server_addr.sin_addr.s_addr = inet_addr(argv[1]);
- if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
- {
- perror("connect");
- exit(EXIT_FAILURE);
- }
- printf("already connected to server %s\n", argv[1]);
- while(1)
- {
- FD_ZERO(&rfds);//初始化rfds为空
- FD_SET(0, &rfds);//将标准输入的描述符0加入到集合rfds中
- FD_SET(sockfd, &rfds);//将newfd加入到集合rfds中
- maxfd = sockfd + 1;
- tv.tv_sec = 1;//阻塞等待时间为1s
- tv.tv_usec = 0;
- retval = select(maxfd, &rfds, NULL, NULL, &tv);//多路复用,同时监测描述符0和newfd
- if(retval == -1)//select函数执行出错
- {
- perror("select");
- exit(EXIT_FAILURE);
- }
- else if(retval == 0)//select函数执行超时
- continue;
- else//有描述符引起异常
- {
- if(FD_ISSET(0, &rfds))//判断是不是标准输入0引起的异常
- {
- bzero(buf, sizeof(buf));//清空buf
- fgets(buf, sizeof(buf)-1, stdin);//从终端接收输入
- if(!strncasecmp(buf, "quit", 4))//判断是否为退出
- {
- printf("i will quit!\n");
- break;
- }
- len = send(sockfd, buf, strlen(buf)-1, 0);//向客户端发送消息
- if(len > 0)
- {
- printf ("send successful,%d byte send!\n",len);
- }
- else
- {
- printf("message '%s' send failure !\n", buf);
- printf("errno code is %d, errno message is '%s'\n", errno, strerror(errno));
- break;
- }
- }
- if(FD_ISSET(sockfd, &rfds))//判断是不是newfd引起的异常
- {
- bzero(buf, sizeof(buf));
- len = recv(sockfd, buf, sizeof(buf)-1, 0);//从客户端接收消息
- if(len > 0 )
- printf("message recv successful : '%s', %d Byte recv\n", buf, len);
- else if(len < 0)
- {
- printf("recv failure !\nerrno code is %d, errno message is '%s'\n", errno, strerror(errno));
- break;
- }
- else//如果客户端已关闭
- {
- printf("the other one close, quit\n");
- break;
- }
- }
- }
- }
- close(sockfd);
- printf("i quited!\n");
- return 0;
- }