|
|||
|
Hi,
I have installed IPv6 Preview patch on my Windows2000 machine to enable IPv6 on it. I had setup the two IPv6 enabled hosts in a private LAN(connected via cross cables). Then ping6.exe is working fine between these two IPv6 hosts. But If u try to bind with IPv6 Linklocal address using Win32 socket programming then its throwing error like: Error : WSAEADDRNOTAVAIL (10049) Cannot assign requested address. The same bind() is allowing for IPv6 loopback address "::1". But we can't use loopback address for IPv6 socket communication between two hosts. Please help me out from this Issue. So please find the following IPv6 Win32 Server.c program here: ------------------------------------------------------------ #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "27015" #define IPV6_ADDR_STR "fe80::211:43ff:fe13:d18b" #define IPV6_LOOPBACKADDR_STR "::1" int __cdecl main(void) { WSADATA wsaData; SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET; struct addrinfo *result = NULL, hints; char recvbuf[DEFAULT_BUFLEN]; int iResult, iSendResult; int recvbuflen = DEFAULT_BUFLEN; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; // Resolve the server address and port iResult = getaddrinfo(IPV6_ADDR_STR, DEFAULT_PORT, &hints, &result); if ( iResult != 0 ) { printf("getaddrinfo failed: %d\n", iResult); WSACleanup(); return 1; } // Create a SOCKET for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { printf("socket failed: %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return 1; } // Setup the TCP listening socket iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen); printf ("\n\nWSAGetLastError(): [%d]\n\n", WSAGetLastError()); if (iResult == SOCKET_ERROR) { printf("bind failed: %d\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); return 1; } freeaddrinfo(result); iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { printf("listen failed: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } printf ("Listening to IPv6 client connection\n"); // Accept a client socket ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { printf("accept failed: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } //IN6ADDR_ANY_INIT //INADDR_ANY // No longer need server socket closesocket(ListenSocket); // Receive until the peer shuts down the connection do { iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); printf ("IPv6-Server-Received: %s\n", recvbuf); if (iResult > 0) { printf("Bytes received: %d\n", iResult); // Echo the buffer back to the sender iSendResult = send( ClientSocket, recvbuf, iResult, 0 ); if (iSendResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } printf ("IPv6-Server-Sent: %s\n", recvbuf); printf("Bytes sent: %d\n", iSendResult); } else if (iResult == 0) printf("Connection closing...\n"); else { printf("recv failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } } while (iResult > 0); // shutdown the connection since we're done iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } // cleanup closesocket(ClientSocket); WSACleanup(); return 0; } ------------------------------------------------------------- Thanks & Regards, Mohan Reply @ mohans_idea@yahoo.co.in |
![]() |
| Thread Tools | |
| Display Modes | |
|
|