Linux 中的 socket 编程:C和C++有什么不同?
我有一文件 a.c ,内容如下:
-------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
struct sockaddr_in address;
int fd;
int address_len;
int rtval;
char hwChar = '0';
fd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(1234);
address_len = sizeof(address);
rtval = connect(fd, (struct sockaddr *)&address, address_len);
if(rtval == -1) return 1;
write(fd, &hwChar, 1);
close(fd);
}
-------------------------------------------------------------------------
用命令 gcc -o a.o -c a.c 编译没任何错误。接着
cp a.c a.cxx
g++ -o axx.o a.cxx
报错信息如下:
a.cxx: In function `int main(int, char**)':
a.cxx:16: `inet_addr' undeclared (first use this function)
a.cxx:16: (Each undeclared identifier is reported only once for each function
it appears in.)
a.cxx:22: `write' undeclared (first use this function)
a.cxx:23: `close' undeclared (first use this function)
请问是什么原因?如果要在C++中进行 socket 编程,要怎么样做才行?