4

Closed

recvFrom in the socket module always returns 0.0.0.0,0 as the remote address

description

<Thanks>MattDarg</Thanks>
<Test>needed</Test>
 
The address returned by recvFrom in the socket module is always (0.0.0.0, 0). The relevant code is:
 
  IPEndPoint remoteIPEP = new IPEndPoint(IPAddress.Any, 0);
  EndPoint remoteEP = remoteIPEP;
 
  bytesRead = socket.ReceiveFrom(buffer, (SocketFlags)flags, ref remoteEP);
 
358: Tuple remoteAddress = EndPointToTuple(remoteIPEP);
  return Tuple.MakeTuple(data, remoteAddress);
 
 
The Tuple returned by recvFrom is created using the IPEndPoint reference remoteIPEP. Although the EndPoint reference remoteEP points to this initially, it is passed in to socket.ReceiveFrom() as a ref parameter, and so can (and does) point to a new object afterwards.
 
Solution is to add the following line before line 358 as marked above:
 
  remoteIPEP = (IPEndPoint) remoteEP;
 
I've tested this solution out and it works ok.

file attachments

Closed Dec 9, 2014 at 7:54 PM by jdhardy
Migrated to GitHub.

comments

MattDarg wrote Nov 21, 2006 at 1:19 PM

Should have said it's found in version 1.0.1 and current tip.

wrote Nov 28, 2006 at 9:38 PM

wrote Jun 5, 2007 at 4:12 PM

wrote Jun 7, 2007 at 6:53 PM

wrote Jul 26, 2007 at 4:23 PM

wrote Sep 12, 2007 at 8:40 PM

wrote Nov 12, 2007 at 1:13 AM

nakagami wrote Nov 12, 2007 at 1:30 AM

diff at IronPython 2.0 A6
*** socket.cs.old Fri Nov 02 11:27:22 2007
--- socket.cs Mon Nov 12 11:29:04 2007

*** 372,378 ****
                  throw MakeException(e);
              }
              string data = StringOps.FromByteArray(buffer, bytesRead);
! PythonTuple remoteAddress = EndPointToTuple(remoteIPEP);
              return PythonTuple.MakeTuple(data, remoteAddress);
          }
--- 372,378 ----
                  throw MakeException(e);
              }
              string data = StringOps.FromByteArray(buffer, bytesRead);
! PythonTuple remoteAddress = EndPointToTuple(remoteEP);
              return PythonTuple.MakeTuple(data, remoteAddress);
          }

wrote Nov 20, 2007 at 10:54 AM

dinov wrote Feb 25, 2008 at 6:20 PM

This appears to have been fixed a while ago.

wrote Feb 25, 2008 at 6:20 PM

dfugate wrote Feb 26, 2008 at 11:29 PM

This isn't fixed. We have the address (tuple contents) reversed:

IP 2.0A8ish

import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
12
data, addr = s.recvfrom(1024)
s.close()
print 'Received', repr(data)
Received 'Hello, world'
print addr
('0.0.0.0', 0)
E:\External\Languages\IronPython20\25>C:\Python25\python.exe
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import socket
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
12

data, addr = s.recvfrom(1024)
s.close()
print 'Received', repr(data)
Received 'Hello, world'
print addr
(0, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

wrote Feb 26, 2008 at 11:29 PM

wrote Feb 26, 2008 at 11:30 PM

wrote Mar 6, 2008 at 12:09 AM

wrote Jul 8, 2008 at 11:49 PM

wrote Jul 9, 2008 at 6:25 PM

dinov wrote Jul 9, 2008 at 8:07 PM

We are doing the right thing here. The correct repro (requiring two processes) is:

Client:

import socket
HOST = 'localhost'
PORT = 80
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('127.0.0.1', 80))
s.sendto('hello world', 0, ('127.0.0.1', 80))

Server:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 80))
data, addr = s.recvfrom(1024)

We both end up getting an address that looks something like "('127.0.0.1', 64980)" back.

The problem is that when you use SOCK_STREAM you have a connection oriented socket. Connection oriented sockets do not provide the address of the remote caller (because you're connected to them - you don't need to know it). CPython is returning what is a completely bogus address result when this happens. We return a TCP/IP address which is also completely useless but at least well-formed. I don't know that there's any value in matching the invalid return value because the caller knows whether they're expecting to get a useful address or not.

wrote Jul 9, 2008 at 8:07 PM

dfugate wrote Aug 28, 2008 at 7:24 PM

Added Dino's comment about socket.SOCK_STREAM to the IronPython differences Wiki.

dfugate wrote Aug 28, 2008 at 7:52 PM

The last repro mentioned in these comments actually does not seem to work under CPython or IronPython. IP gives the following:

Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue

at PythonContext.GetModuleState(Object key)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Runtime\PythonContext.cs(220)

at PythonSocket.timeout(CodeContext context)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython.Modules\socket.cs(1733)

at PythonSocket.MakeException(CodeContext context, Exception exception)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython.Modules\socket.cs(1339)

at socket.recvfrom(Int32 maxBytes, Int32 flags)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython.Modules\socket.cs(393)

at <Module>._stub_$18##13(Closure , CallSite , CodeContext , Object , Int32 )  

at MatchCaller.Call3(Func`5 target, CallSite site, Object[] args)  e:\vslrgrs\ndp\fx\src\Core\Microsoft\Scripting\Actions\MatchCaller.Generated.cs(35)

at CallSite`1.UpdateAndExecute(Object[] args)  e:\vslrgrs\ndp\fx\src\Core\Microsoft\Scripting\Actions\CallSite.cs(275)

at UpdateDelegates.Update3(CallSite site, T0 arg0, T1 arg1, T2 arg2)  e:\vslrgrs\ndp\fx\src\Core\Microsoft\Scripting\Actions\UpdateDelegates.Generated.cs(45)

at <module>$16$4.<module>$16(Closure , Scope , LanguageContext )  <stdin>

at OptimizedScriptCode.InvokeTarget(LambdaExpression code, Scope scope)  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Runtime\OptimizedScriptCode.cs(79)

at ScriptCode.Run(Scope scope)  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Runtime\ScriptCode.cs(80)

at <>c__DisplayClass1.<RunOneInteraction>b__0()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(389)

at PythonContext.DispatchCommand(Action command)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Runtime\PythonContext.cs(2467)

at PythonCommandLine.RunOneInteraction()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(401)

at PythonCommandLine.TryInteractiveActionWorker()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(346)

at PythonCommandLine.TryInteractiveAction()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(324)

at CommandLine.RunInteractiveLoop()  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\CommandLine.cs(183)

at PythonCommandLine.RunInteractive()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(293)

at CommandLine.Run()  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\CommandLine.cs(104)

at PythonCommandLine.Run()  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPython\Hosting\PythonCommandLine.cs(140)

at CommandLine.Run(ScriptEngine engine, IConsole console, ConsoleOptions options)  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\CommandLine.cs(72)

at ConsoleHost.RunCommandLine()  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\ConsoleHost.cs(370)

at ConsoleHost.ExecuteInternal()  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\ConsoleHost.cs(294)

at ConsoleHost.Execute()  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\ConsoleHost.cs(285)

at ConsoleHost.Run(String[] args)  e:\vslrgrs\Merlin\Main\Runtime\Microsoft.Scripting\Hosting\Shell\ConsoleHost.cs(199)

at PythonConsoleHost.Main(String[] args)  e:\vslrgrs\Merlin\Main\Languages\IronPython\IronPythonConsole\Console.cs(68)

Abort Retry Ignore

wrote Aug 28, 2008 at 9:16 PM

wrote Sep 9, 2008 at 6:06 PM

wrote May 22, 2009 at 1:28 AM

wrote Sep 24, 2009 at 5:28 PM

wrote Mar 23, 2010 at 6:27 PM

wrote Oct 21, 2010 at 9:51 PM

wrote Apr 19, 2011 at 7:50 AM

wrote Nov 3, 2011 at 5:13 AM

wrote Mar 4, 2012 at 10:07 AM

wrote Feb 22, 2013 at 1:16 AM

wrote Dec 9, 2014 at 7:54 PM