我正在嘗試使用支持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
所以問題是,我怎樣才能使這個工作,而無需編寫相同用不同的「罪」代碼兩次;
更改變量名稱。名字'sin'與數學庫中的sin(三角函數)函數衝突。 –
在if之前使變量'sockaddr sin'變成'if',並用你的本地'sin'在每個'if'的enc中初始化它。 –
@ThomasMatthews不,它不是。 –