gpt4 book ai didi

c - 如何将程序中定义的默认端口号添加到 getaddrinfo()

转载 作者:行者123 更新时间:2023-11-30 19:29:23 26 4
gpt4 key购买 nike

我想在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com