我想在标头中定义一个端口号(例如#define port 9191
),并用它来调用getaddrinfo()
。但是由于参数服务是char const*
,所以出现错误。在呼叫#define
时如何使用getaddrinfo()
d端口号?
在我#define
d端口下面,将其设置为9191
,并尝试使用char*
将其转换为sprintf()
。然后,我尝试在调用getaddrinfo()
中使用它:
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#define PORT 9191 // here the define port number
#define LEN 500
int
main(int argc, char *argv[])
{
struct addrinfo hints;
struct addrinfo *result, *rp;
struct sockaddr_in cliAddr; // the from address of a client
int sock, s, clilen;
char *service; // declared variable name
struct sockaddr_in storage;
ssize_t n;
char receive[LEN], message[LEN];
sprintf(service, "%d", PORT); // i have tried convert it to char
if (argc != 1) {
fprintf(stderr, "Usage: %s \n", argv[0]);
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, &service, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind(2).
If socket(2) (or bind(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock == -1)
continue;
if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
close(sock);
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
/* Read datagrams and echo them back to sender */
while (1)
{
//bzero(receive, LEN);
clilen = sizeof(cliAddr);
n = recvfrom(sock, receive, strlen(receive), 0, (struct sockaddr*)&cliAddr, &clilen);
if (n < 0)
{
fprintf(stderr, "error in reading\n");
exit(EXIT_FAILURE);
}
printf("The client message: %s\n ", receive);
bzero(message, LEN);
fgets(message, sizeof(message), stdin);
message[LEN] = '\0';
printf("%s\n", message);
n = sendto(sock, message, strlen(message), 0, (struct sockaddr*)&cliAddr, sizeof(cliAddr));
printf("%ld\n", n);
if (n < 0)
{
fprintf(stderr, "error in replying\n");
exit(EXIT_FAILURE);
}
int i = strncmp("Exit", receive, 4);
if (i == 0)
break;
printf("The server message: %s\n", message);
}
return 0;
}
答案 0 :(得分:0)
char *service; // declared variable name // ... sprintf(service, "%d", PORT); // i have tried convert it to char
由于service
未初始化,并且很可能包含无效的指针值,因此对sprintf()
的调用会导致未定义的行为。
要么在免费存储区上分配内存并指向service
,要么使用足够大小的数组。
答案 1 :(得分:0)
由于Swordfish已回答,您正在尝试sprintf()
到未定义的指针。例如使用
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
service = STRINGIFY(PORT);
相反。
考虑以下帮助器功能:
#define DEFAULT_HOST "*"
#define DEFAULT_PORT "9191"
/* Return a server-side UDP/IP(v4/v6) socket bound
to the specified address. If addr is non-NULL,
and *addrlen is initialized to its length and
is large enough, the bound-to address will be
stored there. Returns -1 with errno set if
an error occurs. */
int server_socket(struct sockaddr *addr, socklen_t *addrlen,
const char *host, const char *port)
{
struct addrinfo hints, *list, *curr;
int err, fd;
/* Set defaults, if NULL or empty. */
if (!host || !*host)
host = DEFAULT_HOST;
if (!port || !*port)
port = DEFAULT_PORT;
/* "" or "*" host refer to wildcard address. */
if (!host[0] || !strcmp(host, "*")
host = NULL;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
err = getaddrinfo(host, port, &hints, &list);
if (err)
switch (err) {
case EAI_ADDRFAMILY:
case EAI_FAMILY:
errno = EAFNOSUPPORT;
return -1;
case EAI_AGAIN:
errno = EAGAIN;
return -1;
case EAI_FAIL:
case EAI_NODATA:
case EAI_NONAME:
case EAI_SERVICE:
errno = EADDRNOTAVAIL;
return -1;
case EAI_MEMORY:
errno = ENOMEM;
return -1;
case EAI_SOCKTYPE:
errno = ESOCKTNOSUPPORT;
return -1;
case EAI_SYSTEM:
/* errno already set */
return -1;
default:
errno = EINVAL;
return -1;
}
/* Find first valid socket we can construct and bind to */
for (curr = list; curr != NULL; curr = curr->ai_next) {
fd = socket(curr->ai_family, curr->ai_socktype, curr->ai_protocol);
if (fd == -1)
continue;
if (bind(fd, curr->ai_addr, curr->ai_addrlen) == 0)
break; /* This one works. */
/* Cannot bind. Clean it up, and try the next one. */
close(fd);
}
if (!curr) {
/* No suitable socket found. */
errno = EADDRNOTAVAIL;
return -1;
}
/* Copy bound-to address, if enough room */
if (addrlen) {
if (!addr || *addrlen < curr->ai_addrlen) {
*addrlen = 0;
} else {
memcpy(addr, curr->ai_addr, curr->ai_addrlen);
*addrlen = curr->ai_addrlen;
}
}
/* Discard the linked list getaddrinfo() provided. */
freeaddrinfo(list);
return fd;
}
数字端口和命名服务(如getent services
所示)都可以用作默认端口和端口参数。
典型的服务会从一个或多个配置文件中读取这些内容。
如果以上server_socket()
失败,它将返回-1,其中errno
近似于错误消息。 (如果getaddrinfo()
失败,它将尽可能地将错误映射到errno
常量,而不是使用gai_strerror()
。)
如果成功,它将返回绑定套接字的描述符号。
您希望将它们作为单独的函数编写的原因主要是懒惰和易于维护。您会发现,如果将以上内容(带有必要的#includes等)编写到单独的测试程序中,则可以作为一个单元分别测试该部分:因此,unit testing带有几个不同的参数,包括空指针和无效值。使用例如
struct sockaddr_storage addr;
socklen_t addrlen;
const char *node, *service;
int fd;
addrlen = sizeof addr;
fd = server_socket(&addr, &addrlen, node, service);
if (fd == -1) {
fprintf(stderr, "Cannot create server socket: %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
报告错误。请记住,测试不仅是在给定合理参数的情况下检查函数是否正常工作;它还正在测试如果参数本身是错误的/坏的/矛盾的等等会发生什么,并验证 也是可以接受的。
这种分段测试可以节省时间和精力的原因是,当 错误发生时-无论您的程序员多么优秀,它们都会-仅占一小部分代码(需要进行单元测试的附加部分)及其整体交互逻辑(“更高级别”的逻辑),您需要进行检查并考虑以查找错误。另外,您不必在试图同时记住所有代码部分时浪费脑力;您只需忘记经过单元测试的零件,而只需记住它们的一般含义即可。
这还意味着您将需要学习编写注释,这些注释描述了程序员作为代码应该做什么而不是代码做什么的意图。后者在代码中很明显,但是只有程序员才知道前者。为了有效地发现错误,我们需要同时了解两者。 (经过二十多年的付费编程工作,我仍然自己学习这一部分。)