raziel_king
03-07-2007, 20:59
Buonasera a tutti chiedo lumi su una spinosa questione:help: ...Stiamo lavorando ad un progetto in C per il quale è richiesto che client e server effettuino una connessione tcp indipendente dal protocollo di livello network (ipv4 o ipv6). La soluzione che abbiamo implementato funziona perfettamente con ipv4 ma con ipv6 abbiamo 2 problemi:
1- con l'indirizzo di loopback (::1)otteniamo l'errore 10061 connection refused
2- con qualsiasi altro indirizzo ipv6 otteniamo l'errore 10065 host unreachable
Questo è il codice che abbiamo scritto
CLIENT
SOCKET s = INVALID_SOCKET;
char aport[0x40];
sprintf(aport, "%d", port);
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, aport, &hints, &info)) {
PRINT("unknown host: %s\n", host);
TRACE("1 %d\n", WSAGetLastError());
sock_exit();
return -1;
}
for (struct addrinfo *ai = info; ai; ai = ai->ai_next) {
ASSERT(ai->ai_socktype == SOCK_STREAM);
s = socket(ai->ai_family, SOCK_STREAM, ai->ai_protocol);
if (s == INVALID_SOCKET) {
continue;
}
if (connect(s, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) {
VERIFY(!closesocket(s));
s = INVALID_SOCKET;
continue;
}
}
freeaddrinfo(info);
if (s == INVALID_SOCKET) {
PRINT("unknown host: %s\n", host);
TRACE("2 %d\n", WSAGetLastError());
sock_exit();
return -1;
}
printf("connected to %s\n", host);
SERVER
SOCKET lstn_create() {
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
char port[0x40];
sprintf(port, "%d", tcpport);
if (getaddrinfo(NULL, port, &hints, &info)) {
return INVALID_SOCKET;
}
SOCKET listener = INVALID_SOCKET;
for (struct addrinfo *ai = info; ai; ai = ai->ai_next) {
ASSERT(ai->ai_socktype == SOCK_STREAM);
listener = socket(ai->ai_family, SOCK_STREAM, ai->ai_protocol);
if (listener == INVALID_SOCKET) {
continue;
}
if (bind(listener, ai->ai_addr, ai->ai_addrlen)) {
VERIFY(!closesocket(listener));
listener = INVALID_SOCKET;
continue;
}
}
freeaddrinfo(info);
if (listener == INVALID_SOCKET) {
TRACE("no suitable network interface\n");
return INVALID_SOCKET;
}
if (listen(listener, SOMAXCONN) == SOCKET_ERROR) {
#ifdef __WIN32
if (WSAGetLastError() == WSAEADDRINUSE) {
#else // __WIN32
if (errno == EADDRINUSE) {
#endif // __WIN32
PRINT("another process is listening on port %d\n", tcpport);
}
VERIFY(!closesocket(listener));
return INVALID_SOCKET;
}
return listener;
}
Grazie spero possiate darci una mano:O
1- con l'indirizzo di loopback (::1)otteniamo l'errore 10061 connection refused
2- con qualsiasi altro indirizzo ipv6 otteniamo l'errore 10065 host unreachable
Questo è il codice che abbiamo scritto
CLIENT
SOCKET s = INVALID_SOCKET;
char aport[0x40];
sprintf(aport, "%d", port);
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, aport, &hints, &info)) {
PRINT("unknown host: %s\n", host);
TRACE("1 %d\n", WSAGetLastError());
sock_exit();
return -1;
}
for (struct addrinfo *ai = info; ai; ai = ai->ai_next) {
ASSERT(ai->ai_socktype == SOCK_STREAM);
s = socket(ai->ai_family, SOCK_STREAM, ai->ai_protocol);
if (s == INVALID_SOCKET) {
continue;
}
if (connect(s, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) {
VERIFY(!closesocket(s));
s = INVALID_SOCKET;
continue;
}
}
freeaddrinfo(info);
if (s == INVALID_SOCKET) {
PRINT("unknown host: %s\n", host);
TRACE("2 %d\n", WSAGetLastError());
sock_exit();
return -1;
}
printf("connected to %s\n", host);
SERVER
SOCKET lstn_create() {
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
char port[0x40];
sprintf(port, "%d", tcpport);
if (getaddrinfo(NULL, port, &hints, &info)) {
return INVALID_SOCKET;
}
SOCKET listener = INVALID_SOCKET;
for (struct addrinfo *ai = info; ai; ai = ai->ai_next) {
ASSERT(ai->ai_socktype == SOCK_STREAM);
listener = socket(ai->ai_family, SOCK_STREAM, ai->ai_protocol);
if (listener == INVALID_SOCKET) {
continue;
}
if (bind(listener, ai->ai_addr, ai->ai_addrlen)) {
VERIFY(!closesocket(listener));
listener = INVALID_SOCKET;
continue;
}
}
freeaddrinfo(info);
if (listener == INVALID_SOCKET) {
TRACE("no suitable network interface\n");
return INVALID_SOCKET;
}
if (listen(listener, SOMAXCONN) == SOCKET_ERROR) {
#ifdef __WIN32
if (WSAGetLastError() == WSAEADDRINUSE) {
#else // __WIN32
if (errno == EADDRINUSE) {
#endif // __WIN32
PRINT("another process is listening on port %d\n", tcpport);
}
VERIFY(!closesocket(listener));
return INVALID_SOCKET;
}
return listener;
}
Grazie spero possiate darci una mano:O