gpt4 book ai didi

c++ - 声明具有相同名称但类型不同的变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:17 24 4
gpt4 key购买 nike

我正在尝试制作支持 IPV4 或 IPV6 的东西。这两个的代码相似,所以我尝试了这样的事情。由于稍后代码中的唯一区别取决于这个 sin 变量,我怎么能只编译其中一个。让我们假设 IF 语句中的“ipv”变量是真还是假取决于用户输入

//FOR IPV4
//struct sockaddr_in sin;
//FOR IPV6
//struct sockaddr_in6 sin;
//IPV4


// IPV4 ---------------------------------------
if (ipv){
struct sockaddr_in sin;
if ( (s = socket(AF_INET, SOCK_STREAM, 0 ) ) < 0) {
perror("Chyba pri vytvareni socketu");
return -1;
}


sin.sin_family = AF_INET;
sin.sin_port = htons(port_number);
sin.sin_addr.s_addr = INADDR_ANY;
}

// IPV6 ---------------------------------------
else{

struct sockaddr_in6 sin;
if ( (s = socket(AF_INET6, SOCK_STREAM, 0 ) ) < 0) {
perror("Chyba pri vytvareni socketu");
return -1;
}

sin.sin6_family = AF_INET6;
sin.sin6_port = htons(port_number);
sin.sin6_addr = in6addr_any;
sin.sin6_flowinfo = 0;
}

if (bind(s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ) {
printf("error on bind\n"); return -1;
}

if (listen(s, 5)) {
printf ("error on listen\n");
return -1;
}

sinlen = sizeof(sin);
pid_t pid;

while (1) {


/* accepting new connection request from client,
socket id for the new connection is returned in t */
if ( (t = accept(s, (struct sockaddr *) &sin, &sinlen) ) < 0 ) {
printf("error on accept\n"); /* accept error */
return -1;
}
continues .... not important

这段代码会给我:

server.cpp: In function ‘int main(int, char**)’:  
server.cpp:132:35: error: ‘sin’ was not declared in this scope
if (bind(s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ) {
^
server.cpp:145:19: error: ‘sin’ was not declared in this scope
sinlen = sizeof(sin);
^
make: *** [all] Error 1

所以问题是,如果不使用不同的“罪恶”两次编写相同的代码,我怎样才能做到这一点;

最佳答案

我通过使用 sockaddr_storage 来做到这一点并且它运行良好

struct sockaddr_storage sin;
struct sockaddr_in *sin4;
struct sockaddr_in *sin6;
// IPV4 ---------------------------------------
if (ipv == true){
sin4 = (struct sockaddr_in*)&sin;
if ( (s = socket(AF_INET, SOCK_STREAM, 0 ) ) < 0) {
perror("Chyba pri vytvareni socketu");
return -1;
}


sin4->sin_family = AF_INET;
sin4->sin_port = htons(port_number);
sin4->sin_addr.s_addr = INADDR_ANY;
}

// IPV6 ---------------------------------------
else{

sin6 = (struct sockaddr_in6*)&sin;
if ( (s = socket(AF_INET6, SOCK_STREAM, 0 ) ) < 0) {
perror("Chyba pri vytvareni socketu");
return -1;
}

sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(port_number);
sin6->sin6_addr = in6addr_any;
sin6->sin6_flowinfo = 0;
}

if (bind(s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ) {
printf("error on bind\n"); return -1;
}

if (listen(s, 5)) {
printf ("error on listen\n");
return -1;
}

sinlen = sizeof(sin);
pid_t pid;

while (1) {


/* accepting new connection request from client,
socket id for the new connection is returned in t */
if ( (t = accept(s, (struct sockaddr *) &sin, &sinlen) ) < 0 ) {
printf("error on accept\n"); /* accept error */
return -1;
}
continues .... not important

关于c++ - 声明具有相同名称但类型不同的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085120/

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