Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-套接字编程客户端服务器-主机名上的连接_C_Sockets - Fatal编程技术网

C-套接字编程客户端服务器-主机名上的连接

C-套接字编程客户端服务器-主机名上的连接,c,sockets,C,Sockets,在我的服务器上,目前有以下代码: #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

在我的服务器上,目前有以下代码:

      #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;
“hostname”变量是程序开始时的参数

这是客户端错误:

 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));
   ^
有人能看到我的插座和连接失败的地方吗


目前,如果我将两边都设置为INADR\u ANY,它将工作并自动连接,

问题是
serverAddr.sin\u addr.s\u addr
uint32\u t
server->h\u addr
char*

h\u addr
字段实际上是
h\u addr\u list[0]
的别名,其中
h\u addr\u list
是一个
char**
。此字段指向地址结构数组,地址结构可以是\u addr中的
struct,也可以是\u addr
中的
struct

gethostbyname
的情况下,它将是\u addr
中的
结构,因此您需要将其强制转换为该结构,并将其分配给
serverAddr.sin\u addr
,而不是
serverAddr.sin\u addr.s\u addr

serverAddr.sin_addr = *((struct in_addr *)server->h_addr);
这不是有效的声明:

hostname[1023] = "\0";
你想要的是:

char hostname[1023] = {0};

这会将整个数组初始化为零。

问题是
serverAddr.sin\u addr.s\u addr
uint32\u t
server->h\u addr
char*

h\u addr
字段实际上是
h\u addr\u list[0]
的别名,其中
h\u addr\u list
是一个
char**
。此字段指向地址结构数组,地址结构可以是\u addr
中的
struct,也可以是\u addr
中的
struct

gethostbyname
的情况下,它将是\u addr
中的
结构,因此您需要将其强制转换为该结构,并将其分配给
serverAddr.sin\u addr
,而不是
serverAddr.sin\u addr.s\u addr

serverAddr.sin_addr = *((struct in_addr *)server->h_addr);
这不是有效的声明:

hostname[1023] = "\0";
你想要的是:

char hostname[1023] = {0};
这将把整个数组初始化为零

“\0”
是一个字符串文字,表示两个
常量字符的数组,均为零。与大多数其他上下文一样,在赋值语句中,表达式转换为指向第一个字符的指针。显然,
hostname
是一个
char
数组或
char*
,因此
hostname[1023]
是一个表示单个
char
的左值。您正试图为该字符分配一个字符指针

您需要的是字符文字:

hostname[1023] = '\0';
或者,等价地说,只是

hostname[1023] = 0;
您未能包含声明函数的标题
gethostname()
inet\u ntoa()
。在POSIX系统上,这些是

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#包括
#包括
#包括
#包括
“\0”
是一个字符串文字,表示两个
常量字符的数组,均为零。与大多数其他上下文一样,在赋值语句中,表达式转换为指向第一个字符的指针。显然,
hostname
是一个
char
数组或
char*
,因此
hostname[1023]
是一个表示单个
char
的左值。您正试图为该字符分配一个字符指针

您需要的是字符文字:

hostname[1023] = '\0';
或者,等价地说,只是

hostname[1023] = 0;
您未能包含声明函数的标题
gethostname()
inet\u ntoa()
。在POSIX系统上,这些是

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#包括
#包括
#包括
#包括

也尝试过这样做:serverAddr.sin\u addr.s\u addr=lol->h\u addr;但它说我正在从指针生成整数,我也没有尝试过:serverAddr.sin\u addr.s\u addr=lol->h\u addr;但它说我是从指针生成整数而不是强制转换