Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I can't use getsockname() to get a host IPv6 address...

730 views
Skip to first unread message

Ya-Lin Huang

unread,
Oct 4, 2004, 11:21:14 PM10/4/04
to
Hi,
i used the sample code provided by msdn (Platform SDK: Windows Sockets
2->IPv6 Guide for Windows Sockets Applications->Appendix B: IP-version
Agnostic Source Code) for testing. However, while i was using protocol
family with PF_INET6, i always got "::" from getsockname(). I'm wondering if
there's something else to do but i did not while compiling the code ??

i'm using windows XP SP2 + Microsoft Platform SDK February 2003 +
Microsoft Visual C++ 6.0

thanks for answer :)

Ya-Lin Huang


Raymond Sinnappan [MSFT]

unread,
Oct 5, 2004, 1:00:45 PM10/5/04
to
Hi: Does the socket connect call succeed? Apologies if you've checked
this, but do you have any IPv6 addresses on your machine (ipconfig /all)?
Note that you need to explictly install IPv6 on XPSP2 via the network
connections, "add protocol". Again sorry if this sounds like the obvious,
but it would be good to rule these out first.

Thanks.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Ya-Lin Huang" <hua...@csie.nctu.edu.tw> wrote in message
news:eSogepoq...@TK2MSFTNGP14.phx.gbl...

Ya-Lin Huang

unread,
Oct 6, 2004, 1:19:26 PM10/6/04
to
What followed are my steps:
1. open Microsoft Visual C++ (Visual C++R 6.0 + Microsoft Platform SDK
February 2003)
2. New a C/C++ source file and paste the content copy from msdn web page
(ipv6-enabled-client:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/ipv6_enabled_client_code_2.asp)
3. Compile
4. 'Project' -> 'Settings...' -> 'Link', add "wsock32.lib" into
'Object/library modules:' -> 'OK'
5. Build
6. Do these 5 steps above again except the source code is
ipv6-enabled-server:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/ipv6_enabled_server_code_2.asp

7. Open two command line ('cmd'), one for ipv6-enabled-server (server.exe)
and the other for ipv6-enabled-client (client.exe)
Execute server.exe first and then execute client.exe. After that i got
the result where "Using local address ::1, port 1041" showed that
getsockname() correctly retreived IP address the socket used. See below:
A. > server.exe -t TCP -p 5060
'Listening' on port 5060, protocol TCP, protocol family PF_INET6
'Listening' on port 5060, protocol TCP, protocol family PF_INET

Accepted connection from ::1
Received 10 bytes from client: [Message #1]
Echoing same data back to client
Client closed connection

B. > client.exe -t TCP -p 5060
Attempting to connect to: localhost
Connected to ::1, port 5060, protocol TCP, protocol family PF_INET6
Using local address ::1, port 1041
Sent 10 bytes (out of 10 bytes) of data: [Message #1]
Received 10 bytes from server: [Message #1]
Done sending
Server closed connection

8. Open two command line ('cmd'), one for ipv6-enabled-server (server.exe)
and the other for ipv6-enabled-client (client.exe)
Execute server.exe first and then execute client.exe. After that i got
the result where "Using local address ::, port 1042" showed that
getsockname() did NOT correctly retreived IP address the socket used. See
below:
> server.exe -t UDP -p 5060
'Listening' on port 5060, protocol UDP, protocol family PF_INET6
'Listening' on port 5060, protocol UDP, protocol family PF_INET
Received a 10 byte datagram from ::1: [Message #1]
Echoing same data back to client


> client.exe -t UDP -p 5060
Attempting to connect to: localhost
Connected to ::1, port 5060, protocol UDP, protocol family PF_INET6
Using local address ::, port 1042
Sent 10 bytes (out of 10 bytes) of data: [Message #1]
Received 10 bytes from server: [Message #1]
Done sending


I'm wondering what's wrong? Shouldn't getsockname() return an IP address
if my connection is UDP? Or there's something that i misunderstand??
I've tested two machine of Windows XP Professional SP2 and Windows
Server 2003 Standard Edition. Both of them are ipv6 enabled, and the result
are the same...

Thanks for answering... :)

Sincerely,
Ya-Lin Huang


"Raymond Sinnappan [MSFT]" <raym...@online.microsoft.com> 撰寫於郵件新聞:%23cLoczv...@TK2MSFTNGP10.phx.gbl...

Raymond Sinnappan [MSFT]

unread,
Oct 6, 2004, 6:20:22 PM10/6/04
to
Ah...that behavior is right because you are using (connectionless) UDP, so
the address is will only be known after a send or receive on the socket. In
the UDP case, the source address that's used to send is select only when you
do the actual send. If you print the address after sending bytes you should
see something other than "::". In TCP the two endpoints decide what their
source addresses are as soon as the connect completes, before any I/O so you
see what you expected. Here's the relevant part of the docs BTW:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getsockname_2.asp
"A Windows Sockets application must not assume that the address will be
specified unless the socket is connected. The address that will be used for
the socket is unknown unless the socket is connected when used in a
multihomed host. If the socket is using a connectionless protocol, the
address may not be available until I/O occurs on the socket."

Thanks.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Ya-Lin Huang" <hua...@csie.nctu.edu.tw> wrote in message

news:eUWghi8q...@tk2msftngp13.phx.gbl...

Ya-Lin Huang

unread,
Oct 6, 2004, 9:40:47 PM10/6/04
to
Oops, seems that i do misunderstand ... thanks :)

But I do another experiment that is pasting the source code segment of
printing out local ip address after send and receive. Using the same code
as i said previous, i added what follows right after
"ReceiveAndPrint(ConnSocket, Buffer, sizeof(Buffer));" in main program in
ipv6-enabled-client.

puts( "--> After send() and recv() something... <--" );
//
// Find out what local address and port the system picked for us.
//
AddrLen = sizeof(Addr);
if (getsockname(ConnSocket, (LPSOCKADDR)&Addr, &AddrLen) ==
SOCKET_ERROR) {
fprintf(stderr, "getsockname() failed with error %d: %s\n",
WSAGetLastError(), DecodeError(WSAGetLastError()));
} else {
if (getnameinfo((LPSOCKADDR)&Addr, AddrLen, AddrName,
sizeof(AddrName), NULL, 0, NI_NUMERICHOST) != 0)
strcpy(AddrName, "<unknown>");
printf("Using local address %s, port %d\n",
AddrName, ntohs(SS_PORT(&Addr)));
}
puts ( "" );

The same procedure as before, I got the same local ip address, "::".
A. > server.exe -t UDP -p 5060


'Listening' on port 5060, protocol UDP, protocol family PF_INET6
'Listening' on port 5060, protocol UDP, protocol family PF_INET
Received a 10 byte datagram from ::1: [Message #1]
Echoing same data back to client

B. > client.exe -t UDP -p 5060


Attempting to connect to: localhost
Connected to ::1, port 5060, protocol UDP, protocol family PF_INET6

Using local address ::, port 1031


Sent 10 bytes (out of 10 bytes) of data: [Message #1]
Received 10 bytes from server: [Message #1]

--> After send() and recv() something... <--
Using local address ::, port 1031
Done sending

With Best Regards,
Ya-Lin Huang

"Raymond Sinnappan [MSFT]" <raym...@online.microsoft.com> 撰寫於郵件新聞:e6$9vK$qEHA...@TK2MSFTNGP10.phx.gbl...

Raymond Sinnappan [MSFT]

unread,
Oct 7, 2004, 9:24:43 PM10/7/04
to
Interesting; I'll have to get back to you on this. You see this only on
PF_INET6 not PF_INET correct? Thanks.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Ya-Lin Huang" <hua...@csie.nctu.edu.tw> wrote in message

news:ub$it6ArE...@TK2MSFTNGP14.phx.gbl...

Ya-Lin Huang

unread,
Oct 7, 2004, 10:24:47 PM10/7/04
to
Yes, only on PF_INET6 with using UDP!

Thanks for help :)

Sincerely,
Ya-Lin Huang

"Raymond Sinnappan [MSFT]" <raym...@online.microsoft.com> 撰寫於郵件新聞:e7qJZWNr...@TK2MSFTNGP14.phx.gbl...

Raymond Sinnappan [MSFT]

unread,
Oct 8, 2004, 5:00:59 PM10/8/04
to
Hi Ya-Lin: This an issue with the IPV6/Winsock implementation; not by
anything wrong in what you are doing. It should be fixed for Windows 2003
SP1 but not in XPSP2. Thanks.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Ya-Lin Huang" <hua...@csie.nctu.edu.tw> wrote in message

news:uh6Q53Nr...@TK2MSFTNGP09.phx.gbl...

0 new messages