【原创】TCP Socket 简单练习 --- select同时监测多个描述符
运行方式
服务器端,可以直接运行命令,也可以带参数运行,如果不带参数运行,则程序自主获取主机ip,然后默认设定port和lisnum的值。其中port默认为6666,lisnum默认为5.
./socket_select_server IP地址(可选) 端口号(可选)监听队列大小(可选)
客户端,必须指定服务器的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, c