[Twisted-Python] _Win32Waker

Aron Bierbaum aronbierbaum at gmail.com
Tue Apr 21 15:35:35 EDT 2009


Sorry, forgot to attach the C++ example.

-Aron

On Tue, Apr 21, 2009 at 2:22 PM, Aron Bierbaum <aronbierbaum at gmail.com> wrote:
> I have been able to reproduce the same results with a simple C++
> example. I have been unable to find any specific reason why binding to
> "127.0.0.1" and later calling getsockname would result in a different
> address of "0.0.0.0" I did find one reference [1] that said that using
> a port number of 0 will cause the operating system to listen on all
> interfaces and that calling getsockname may not return a valid address
> until the socket is fully connected. It might be best to use the
> original patch that I sumbitted that only uses the port value from the
> getsockname call, and uses the correct "127.0.0.1" IP address
> regardless of the reported address. If the address returned is really
> undefined, then this could be causing the problem.
>
> -Aron
>
> [1] http://www.sockets.com/winsock.htm#Bind
>
> [2] Change the following line in posixbase.py
>
> client.connect(server.getsockname())
>
> to
>
> client.connect(('127.0.0.1', server.getsockname()[1]))
>
> On Mon, Apr 20, 2009 at 7:06 PM, Jean-Paul Calderone <exarkun at divmod.com> wrote:
>> On Mon, 20 Apr 2009 18:16:39 -0500, Aron Bierbaum <aronbierbaum at gmail.com>
>> wrote:
>>>
>>> I have looked into this a little more and have noticed that if I
>>> specify a port number instead of "0" it will always bind to the
>>> correct "127.0.0.1" address. I still don't know why this only occurs
>>> on certain Windows machines. I will hopefully get more time to look
>>> into this in the next couple of days.
>>
>> Hmm.  Interesting.  Perhaps we could bind a different port, then.
>> I look forward to results from your further investigations.
>>
>> Jean-Paul
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>
-------------- next part --------------
#include <stdio.h>
#include "winsock2.h"
#include <iostream>

// Try to create a socket, bind to 127.0.0.1, and get the socket name.
void createSocket()
{
   // Create a SOCKET for listening for incoming connection requests
   SOCKET ListenSocket;
   ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if (ListenSocket == INVALID_SOCKET)
   {
	  std::cout << "Error at socket(): " << WSAGetLastError() << std::endl;
      WSACleanup();
      return;
   }

   // The sockaddr_in structure specifies the address family,
   // IP address, and port for the socket that is being bound.
   sockaddr_in service;
   service.sin_family = AF_INET;
   service.sin_addr.s_addr = inet_addr("127.0.0.1");
   service.sin_port = htons(0);

   //printf("b4 bind : %d.%d.%d.%d\n", service.sin_addr.S_un.S_un_b.s_b1,
   //                                  service.sin_addr.S_un.S_un_b.s_b2,
   //                                  service.sin_addr.S_un.S_un_b.s_b3,
   //                                  service.sin_addr.S_un.S_un_b.s_b4);

   // Bind the socket.
   if (SOCKET_ERROR == bind( ListenSocket, (SOCKADDR*) &service, sizeof(service)))
   {
      printf("bind() failed.\n");
      closesocket(ListenSocket);
      return;
   }
   //listen(ListenSocket, 5);

   int name_len;
   sockaddr outname;
   name_len = sizeof (struct sockaddr_in);
   memset(&outname, 0, name_len);

   getsockname(ListenSocket, &outname, &name_len);

   // Get the port number.
   struct sockaddr_in* get_port = (sockaddr_in*)&outname;
   int port = ntohs(get_port->sin_port);

   if (outname.sa_data[2] == 0 && 0 == outname.sa_data[3]&& 0 == outname.sa_data[3]&& 0 == outname.sa_data[3])
   {
      std::cout << "Bound to wrong address." << std::endl;
      // Display the address.
      printf("Address %d.%d.%d.%d:%d\n", outname.sa_data[2], outname.sa_data[3],
                                         outname.sa_data[4], outname.sa_data[5], port);
   }

   closesocket(ListenSocket);
   return;
}

void main()
{
   // Initialize Winsock
   WSADATA wsaData;
   if (NO_ERROR != WSAStartup(MAKEWORD(2,2), &wsaData))
   {
      printf("Error at WSAStartup()\n");
   }
 
   // Try to create a socket, bind to 127.0.0.1, and get the socket name.
   for (int i = 0; i < 100; ++i)
   {
      createSocket();
   }

   // Cleanup Winsock
   WSACleanup();
}



More information about the Twisted-Python mailing list