在我的服务器我眼前这个代码:Ç - socket编程的客户端 - 服务器 - 主机名连接
#define h_addr h_addr_list[0]
serverAddr.sin_port = htons(port);
/* Set IP address to localhost */
hostname[1023] = "\0";
gethostname(hostname, 1023);
printf("HostName: %s\n", hostname); // this one prints correctly
my_hostent = gethostbyname(hostname);
printf("Host: %s\n", my_hostent->h_addr);
printf("IP: %c\n", inet_ntoa(my_hostent->h_addr));
serverAddr.sin_addr.s_addr = *hostname;
而在客户端,我拥有它,你必须写主机参数,以便我可以在这个例子中写-h www.abc.com我让自己说我的服务器也在www.abc.com上,但是他们现在从来没有通信过,但是当我打印主机名时它说的是一样的。
客户端代码。
#define h_addr h_addr_list[0]
struct hostent *server;
server = gethostbyname(hostname);
serverAddr.sin_addr.s_addr = server->h_addr;
“主机名”变量是程序启动时的参数。
这是客户端错误:
warning: assignment makes integer from pointer without a cast
serverAddr.sin_addr.s_addr = server->h_addr;
这是服务器错误:
server.c:42:18: warning: assignment makes integer from pointer without a cast
hostname[1023] = "\0";
^
server.c:43:3: warning: implicit declaration of function ‘gethostname’ [-Wimplicit-function-declaration]
gethostname(hostname, 1023);
^
server.c:48:3: warning: implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
printf("IP: %c\n", inet_ntoa(lol->h_addr));
^
有人能看到我的失败就是用插座,并将它们连接在一起?
在如果我设置双方INADDR_ANY它会工作,并自动连接的那一刻,
还试图这样做的: serverAddr.sin_addr.s_addr = lol-> H_ADDR; 但它说我正在从指针进行整型而不投射 –