gpt4 book ai didi

c - 服务器-客户端套接字 : freeaddrinfo(3) placement

转载 作者:行者123 更新时间:2023-11-30 15:50:36 25 4
gpt4 key购买 nike

我必须在环回接口(interface)上测试这个被动 TCP 服务器。 IP 和端口由命令行给出并使用 getopt 进行解析。然后它使用 getaddrinfo(3) 设置并绑定(bind) TCP 套接字,并开始忙等待。

服务器:

#include "server_utils.h"
#define MAX_CONNECTION 10

#define EXIT_ON_ERROR_(s) { fprintf(stderr, s); exit(EXIT_FAILURE); }

int access_permissions;
int session_status;
int sock_ds, acc_sock_ds;

int main(int argc, char** argv) {
/*...*/

int opt, errsv, client_addr_l;
char *p_string;
struct sockaddr_in client_addr;
struct addrinfo hints;
struct addrinfo *result, *rp;

int check = 0;
/*Parsing command line: port-number retrieving*/
while (( opt = getopt(argc, argv, "p:")) != -1){
check = 1;
switch(opt){
case 'p':
/* String to unsigned long integer with ushort cast */
p_string = optarg;
strtoul(optarg, &optarg, 0);
if(*optarg)
EXIT_ON_ERROR_("String-integer conversion error\n");
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if(!check){
fprintf(stderr, "Usage: %s -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}

printf("Port number retrieved (%s), server is starting ...\n", p_string);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE|AI_NUMERICSERV;

if(getaddrinfo(NULL, p_string, &hints, &result) != 0)
/*freeaddrinfo(result); if get funct fails it should not be need*/
EXIT_ON_ERROR_("Socket creation error!\n");

for(rp = result; rp != NULL; rp = rp->ai_next){
sock_ds = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock_ds == -1) continue; //try next address

/*A TCP server should usualy set SO_REUSEADDR option on its listening socket*/
int optval = 1;
if( (setsockopt(sock_ds,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))) == -1 ){
EXIT_ON_ERROR_("Error on setsockopt call\n");
freeaddrinfo(result); //???
}
/*See TLPI Section 61.10*/

if(bind(sock_ds, rp->ai_addr, rp->ai_addrlen) == 0)
break; //success

close(sock_ds); //failure, continue
}
if(rp == NULL) {
freeaddrinfo(result); //?
EXIT_ON_ERROR_("Could not bind socket\n");
}
freeaddrinfo(result);

/*Server with passive socket*/
if(listen(sock_ds, MAX_CONNECTION == -1)){
fprintf(stderr, "Listen call error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

/*Initializing client_addr_l */
client_addr_l = sizeof( struct sockaddr_in);
printf("Server is ready. Waiting for client connections.\n");


/*Busy-waiting server*/
while(1){
/*...*/
}

每次服务器连接都成功。

客户:

#include "client_utils.h"

#define EXIT_ON_ERROR_(s) { fprintf(stderr, s); exit(EXIT_FAILURE); }


int main(int argc, char *argv[]) {

int sock_ds, opt, check_p, check_a;
char * ip, *port;
struct addrinfo hints;
struct addrinfo *result, *rp;

check_p = 0; check_a = 0;
/*port-number, ip address retrieval*/
printf("Command line parsing: port-number, ip-address retrieving...\n");
while ((opt = getopt(argc, argv, "a:p:")) != -1) {

switch (opt) {
case 'p':
/* strtoul only used for string check */
port = optarg;
strtoul(optarg, &optarg, 0);
if (*optarg)
EXIT_ON_ERROR_("Invalid port number string submitted\n");

check_p = 1;
break;
case 'a':
ip = optarg;
/*inet_aton only used for string check*/
if(strcmp(ip, "localhost") != 0 && inet_aton(ip, NULL) == 0)
EXIT_ON_ERROR_("Invalid ip address string submitted\n");
check_a = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s -a ip_address ('localhost' for local server)"
" -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (!check_p || !check_a) {
fprintf(stderr, "Usage: %s -a ip_address ('localhost' for local server)"
" -p port_number\n", argv[0]);
exit(EXIT_FAILURE);
}

printf("Command line parsed succesfully: <%s:%s>\n", ip, port);

/*TCP Socket binding and connection*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_next = NULL;
hints.ai_addr = NULL;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV;

if(getaddrinfo(ip, port, &hints, &result) != 0){
/*getaddrinfo(resutl) ??? (same as sever)? */
EXIT_ON_ERROR_("Socket creation error\n");
}

for(rp = result; rp != NULL; rp = rp->ai_next){
sock_ds = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock_ds == -1)
continue;

if(connect(sock_ds, rp->ai_addr, rp->ai_addrlen) != -1)
break;

close(sock_ds);
}

if(rp == NULL){
freeaddrinfo(result); //???
EXIT_ON_ERROR_("Could not connect socket to any address\n");
}
freeaddrinfo(result);

printf("Connection achieved with:%s on port:%s\n", ip, port);
/*...*/
}

我对 freeaddrinfo(3) 函数有疑问。为了完成良好的堆管理,将其安全地放置在哪里?我将其放在套接字创建操作的末尾以及致命退出之前的某些错误情况下。我不知道究竟如何以及什么获取操作 malloc 额外空间。有人可以给我澄清一下吗?

最佳答案

您的一些 freeaddrinfo(3) 被错误地放置在相应的 EXIT_ON_ERROR_() 之后,但除此之外我没有发现任何问题。

这个想法是 getaddrinfo() 构建一个 sockaddr 结构的链接列表,您可以使用它来创建套接字:一旦完成,您就不再需要该列表,因此 freeaddrinfo() 完全释放它 - 即,我不'没有发现您的代码有任何问题。

我错过了你的任何问题吗?

关于c - 服务器-客户端套接字 : freeaddrinfo(3) placement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15690303/

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