通过命令行指定监听的IP和端口
实现:
int s, con; struct sockaddr_in cliaddr, servaddr; if ( argc != 3 ) { printf( "uasage: httpd <IPaddress> <Port>"); exit( 0 ); } s = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; inet_pton( AF_INET, argv[1], &(servaddr.sin_addr) ); servaddr.sin_port = htons( atoi(argv[2]) );
第一:开始的时候把*argv[2]直接当整型用,错误
第二:知识点
#define AF_INET 2 /* Internet IP Protocol */ #define AF_INET6 10 /* IP version 6 */ #define INADDR_ANY ((unsigned long int) 0x00000000)