作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 header 中定义一个端口号(例如 #define port 9191
)并使用它来调用 getaddrinfo()
。但我收到错误,因为参数服务是 char const*
。如何使用#define
d 调用 getaddrinfo()
时的端口号?
下面我#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;
}
最佳答案
char *service; // declared variable name
// ...
sprintf(service, "%d", PORT); // i have tried convert it to char
由于 service
未初始化并且很可能包含无效的指针值,因此对 sprintf()
的调用会导致未定义的行为。
要么在自由存储上分配内存并让service
指向它,要么使用足够大小的数组。
关于c - 如何将程序中定义的默认端口号添加到 getaddrinfo(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52885074/
我是一名优秀的程序员,十分优秀!