UDP套接字sendto()函数(UDP socket sendto() functions)
如果我想像这样在我的udp套接字上写入,我会得到一个错误。 根据文件,应该不会有问题。 我不明白为什么bind()以同样的方式工作,但sendto()失败。
udp_port = 14550 udp_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp_server.bind(('127.0.0.1', udp_port)) udp_clients = {}
错误:
udp_server.sendto('', ('192.0.0.1', 14550) ) socket.error: [Errno 22] Invalid argument
I get an error if I want to write on my udp socket like this. According to the docu there shouldn't be a problem. I don't understand why bind() works well in the same way but sendto() fails.
udp_port = 14550 udp_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp_server.bind(('127.0.0.1', udp_port)) udp_clients = {}
Error:
udp_server.sendto('', ('192.0.0.1', 14550) ) socket.error: [Errno 22] Invalid argument
原文:https://stackoverflow.com/questions/30268008
最满意答案
using System.Text;
意味着我正在使用名为“System.Text”的单个命名空间,或者我正在使用“System”命名空间中嵌套的“Text”命名空间?后者。 您正在使用嵌套在“System”命名空间内的“Text”命名空间。
请注意,当你说
namespace Foo.Bar.Blah { class C { ...
这只是一种简短的写作方式
namespace Foo { namespace Bar { namespace Blah { class C { ...
命名空间只有“简单”的名字。
我们应该为每个我们使用的命名空间都有一个明确的'using'指令吗?
通常你会,是的。 有些情况下你可能不想这样做。 例如,如果您有两个名称空间都包含一个相同名称的类型,并且您想使用两个名称空间中的类型, 在这种情况下,使用两个名称空间可能会令人困惑。 另外,“使用”带来了扩展方法; 如果有不想绑定的扩展方法,请不要包含包含它们的名称空间。
如果“文本”嵌套在“系统”中,为什么只有“使用系统”包含所有的名称空间?
我不明白这个问题的措辞。 我想也许你在问:
为什么
using System;
不是让简单的名字Text
解析为嵌套的命名空间?也就是说,为什么这不起作用:
namespace Foo { namespace Bar { class Blah {} } }
在另一个文件中:
using Foo; class Abc : Bar.Blah {}
using Foo;
只包括直接在Foo中声明的类型 。 它没有引入直接在Foo中声明的命名空间 。 C#的设计者认为把命名空间“纳入范围”是太混乱了; 通常人们使用指令将类型 “纳入范围”。Does
using System.Text;
mean that I'm using a single namespace named as "System.Text" or that I'm using the "Text" namespace nested inside "System" namespace?The latter. You are using the "Text" namespace that is nested inside the "System" namespace.
Note that when you say
namespace Foo.Bar.Blah { class C { ...
That is just a short form way of writing
namespace Foo { namespace Bar { namespace Blah { class C { ...
Namespaces only have "simple" names.
should we have an explicit 'using' directive for every namespace we use?
Typically you do, yes. There are some situations in which you might want to not do so. For example, if you have two namespaces that both contain a type of the same name and you want to use types from both namespaces; in that case it might be too confusing to have a "using" of two namespaces. Also, "using" brings extension methods into play; if there are extension methods that you do not want to bind to, do not include the namespace that contains them.
if "Text" is nested inside "System" why only "using System" includes all the namespaces?
I do not understand the question the way it is worded. I think maybe you are asking:
Why does
using System;
not make the simple nameText
resolve to the nested namespace?That is, why does this not work:
namespace Foo { namespace Bar { class Blah {} } }
In a different file:
using Foo; class Abc : Bar.Blah {}
The
using Foo;
only includes the types declared directly inside Foo. It does not bring in namespaces declared directly inside Foo. The designers of C# thought that it was too confusing to bring namespaces "into scope" like this; typically people useusing
directives to bring types "into scope".
相关问答
更多-
您可以使用名称空间别名限定符 (通常为global::来引用默认/ root名称空间: global::Foo.Uber You can do this using a namespace alias qualifier (typically global::) to refer to the default / root namespace: global::Foo.Uber
-
请参阅以下SO讨论以及如何由SO用户之一处理C ++名称空间建议 http://www.adamjamesnaylor.com/2012/10/23/NestedNamespacesInC.aspx See the following SO discussion and how this was handled by one of the SO users C++ namespaces advice http://www.adamjamesnaylor.com/2012/10/23/NestedNamesp ...
-
元素的名称空间是http://www.some-site.com 。 xmlsn:xxx表示以xxx为前缀的名称:(如soap:Body)具有该命名空间。 xmlns本身意味着这是没有任何前缀的名称的默认命名空间。 The namespace of the
element is http://www.some-site.com. xmlsn:xxx means that names prefixed by xxx: (like soap:Body) have that namesp ...
-
C#命名空间问题(C# namespace questions)[2023-08-03]
他们是一样的。 如果您在.NET Reflector中查看此代码: namespace A { namespace B{ namespace C{ public class AA{ } } } } 你得到这个: namespace A.B.C { public class AA { // Methods public AA(); } } 两种方法都被编译为完全相同的中间语言代码。 They're the same. If you look at ... -
谷歌第一眼看到你可能需要添加命名空间: Aspose.Pdf.Facades 看看这里 ,看看左边的树。 并且不要忘记在顶部查看我的评论,如果我找不到它,它总是有助于找到类的命名空间。 希望能帮助到你! A first sight at google told me that you might need to add namespace: Aspose.Pdf.Facades Check out here and see the tree at the left. and don;t forget to ...
-
在Visual Studio 2015 / C中自动更新命名空间#(Updating the namespace automatically in Visual Studio 2015/ C#)[2022-03-07]
Resharper是处理遗留代码的工具。 尝试使用此工具。 右键单击文件夹>转到重构,然后选择调整命名空间 Resharper is a tool to work on legacy code. Try to use this tool. Right click on the folder> go to refactor and then select adjust Namespaces -
C#4.0规范的第9.2节指出: 名称空间声明的合格标识符可以是单个标识符或由“。”标记分隔的一系列标识符。 后一种形式允许程序定义一个嵌套名称空间,而不用词汇嵌套几个名称空间声明。 例如, namespace N1.N2 { class A {} class B {} } 在语义上等同于 namespace N1 { namespace N2 { class A {} class B {} } } 你说: 我在这里假定C#使用像ABC这样的名称空间不一定需要 ...
-
using System.Text; 意味着我正在使用名为“System.Text”的单个命名空间,或者我正在使用“System”命名空间中嵌套的“Text”命名空间? 后者。 您正在使用嵌套在“System”命名空间内的“Text”命名空间。 请注意,当你说 namespace Foo.Bar.Blah { class C { ... 这只是一种简短的写作方式 namespace Foo { namespace Bar { namespace ...
-
C#是区分大小写的,所以你可能需要这个: TextReader tr = new StreamReader("date.txt"); 除此之外,你在你的问题中提到你会使用正确的“using”语句 ,但是显然你并没有处理/关闭StreamReader 。 您也只读取文件的一行。 // The using statement also closes the StreamReader. using(var sr = new StreamReader("date.txt")) { String line; ...
-
C#名称空间搜索(C# namespace search)[2023-05-15]
您在寻找.NET类库: http : //msdn.microsoft.com/en-us/library/ms229335.aspx Are you looking for the .NET Class Library: http://msdn.microsoft.com/en-us/library/ms229335.aspx