写一个简单的Linux C TCP socket服务端程序,当监听到客户端的连接请求时接受连接,并打印出客户端的IP和端口信息:
...
printf("Client [%s-%d] connected.\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
...
编译的时候报出下图的警告(warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]):
明明inet_ntoa函数的返回值是char *类型啊,见鬼了。原因找来找去,最后发现是出现在头文件上面,加上#include <arpa/inet.h> 就正常了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>