[英]C - socket programming client server - connection on host name
On my server I have this code at the moment: 在我的服务器上,我现在有以下代码:
#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;
And on client side, I have it that you must write the host as parameter so I could write -h www.abc.com in this example I let myself say that my server hosts on www.abc.com also, but they never communicate at the moment, but when I print the hostname it says the same. 在客户端,我必须将主机作为参数写入,以便在此示例中可以写-h www.abc.com。我自己说,我的服务器也位于www.abc.com上,但它们从未通信目前,但是当我打印主机名时,它表示相同。
client code. 客户代码。
#define h_addr h_addr_list[0]
struct hostent *server;
server = gethostbyname(hostname);
serverAddr.sin_addr.s_addr = server->h_addr;
the "hostname" variable is the parameter from program start. “主机名”变量是程序启动时的参数。
this is client error: 这是客户端错误:
warning: assignment makes integer from pointer without a cast
serverAddr.sin_addr.s_addr = server->h_addr;
this is server errors: 这是服务器错误:
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));
^
Can anyone see what my failure is with sockets and connecting them together? 谁能看到我的失败与套接字并将它们连接在一起?
At the moment if I set both sides to INADDR_ANY it will work and automatically connect, 目前,如果我将两端都设置为INADDR_ANY,它将可以正常工作并自动连接,
The problem is that serverAddr.sin_addr.s_addr
is a uint32_t
and server->h_addr
is a char *
. 问题是
serverAddr.sin_addr.s_addr
是uint32_t
而server->h_addr
是char *
。
The h_addr
field is actually an alias for h_addr_list[0]
, where h_addr_list
is a char **
. h_addr
字段实际上是h_addr_list[0]
的别名,其中h_addr_list
是char **
。 This field points to an array of address structures, which can be either struct in_addr
or struct in6_addr
. 该字段指向一个地址结构数组,该地址结构可以是
struct in_addr
或struct in6_addr
。
In the case of gethostbyname
, it will be a struct in_addr
, so you need to cast it to that and assign it to serverAddr.sin_addr
instead of serverAddr.sin_addr.s_addr
: 对于
gethostbyname
,它将是一个struct in_addr
,因此您需要将其serverAddr.sin_addr
转换为该结构并将其分配给serverAddr.sin_addr
而不是serverAddr.sin_addr.s_addr
:
serverAddr.sin_addr = *((struct in_addr *)server->h_addr);
This is not a valid declaration: 这不是有效的声明:
hostname[1023] = "\0";
What you want is this: 您想要的是:
char hostname[1023] = {0};
This will initialize the whole array to zeros. 这会将整个数组初始化为零。
server.c:42:18: warning: assignment makes integer from pointer without a cast hostname[1023] = "\\0"; ^
The "\\0"
is a string literal representing an array of two const char
s, both zero. "\\0"
是一个字符串文字,它表示两个const char
的数组,两者均为零。 In your assignment statement, as in most other contexts, the expression is converted to a pointer to the first char. 与大多数其他上下文一样,在您的赋值语句中,该表达式将转换为指向第一个字符的指针。 Evidently,
hostname
is a char
array or char *
, so that hostname[1023]
is an lvalue representing a single char
. 显然,
hostname
是一个char
数组或char *
,因此hostname[1023]
是一个表示单个char
的左值。 You are trying to assign a char pointer to that char. 您正在尝试将char指针分配给该char。
You want a char literal instead: 您需要一个char文字:
hostname[1023] = '\0';
or, equivalently, just 或者,等效地,只是
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)); ^
You have failed to #include
the headers declaring functions gethostname()
and inet_ntoa()
. 您未能
#include
声明函数gethostname()
和inet_ntoa()
的标头。 On a POSIX system, those would be 在POSIX系统上,这些将是
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.