在套接字编程中提供了无效参数
An invalid argument was supplied in socket programming
我想在 C# 中创建一个 UDP 客户端和服务器,当我 运行 那些,在第一次发送和接收消息后,这个错误实现。我的代码是:
byte[] barray = new byte[1024];
EndPoint ipre = new IPEndPoint(IPAddress.Any, 4040);
socketClint.Bind((IPEndPoint)ipre);
int rc = socketClint.ReceiveFrom(barray, ref ipre);
if (rc > 0)
{
listBox1.Items.Add("Server: "+ BitConverter.ToInt32(barray,0));
}
此错误与 socketClint.Bind((IPEndPoint)ipre);
行有关。
错误是:
System.Net.Sockets.SocketException was unhandled
HResult=-2147467259
Message=An invalid argument was supplied
Source=System
ErrorCode=10022
NativeErrorCode=10022
StackTrace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at client1.Form1.getmsg() in c:\Users\Inspiron 1545\Desktop\UDP\client1\client1\Form1.cs:line 38
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
内部异常:
Winsock 错误 10022 是 WSAEINVAL
:
WSAEINVAL
10022
Invalid argument.
Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt
function). In some instances, it also refers to the current state of the socket — for instance, calling accept
on a socket that is not listening.
在 Winsock bind()
函数(Socket.Bind()
内部使用)的上下文中,这意味着:
WSAEINVAL
An invalid argument was supplied.
This error is returned if the socket s
is already bound to an address.
一旦套接字绑定到本地地址,就不能将它重新绑定到新的本地地址。你应该只绑定一次。
因此,在您的情况下,仅在创建 socketClint
后调用 Bind()
。不要在每次调用 socketClint.ReceiveFrom()
之前调用它。
我想在 C# 中创建一个 UDP 客户端和服务器,当我 运行 那些,在第一次发送和接收消息后,这个错误实现。我的代码是:
byte[] barray = new byte[1024];
EndPoint ipre = new IPEndPoint(IPAddress.Any, 4040);
socketClint.Bind((IPEndPoint)ipre);
int rc = socketClint.ReceiveFrom(barray, ref ipre);
if (rc > 0)
{
listBox1.Items.Add("Server: "+ BitConverter.ToInt32(barray,0));
}
此错误与 socketClint.Bind((IPEndPoint)ipre);
行有关。
错误是:
System.Net.Sockets.SocketException was unhandled
HResult=-2147467259
Message=An invalid argument was supplied
Source=System
ErrorCode=10022
NativeErrorCode=10022
StackTrace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at client1.Form1.getmsg() in c:\Users\Inspiron 1545\Desktop\UDP\client1\client1\Form1.cs:line 38
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
内部异常:
Winsock 错误 10022 是 WSAEINVAL
:
WSAEINVAL
10022Invalid argument.
Some invalid argument was supplied (for example, specifying an invalid level to thesetsockopt
function). In some instances, it also refers to the current state of the socket — for instance, callingaccept
on a socket that is not listening.
在 Winsock bind()
函数(Socket.Bind()
内部使用)的上下文中,这意味着:
WSAEINVAL
An invalid argument was supplied.
This error is returned if the socket
s
is already bound to an address.
一旦套接字绑定到本地地址,就不能将它重新绑定到新的本地地址。你应该只绑定一次。
因此,在您的情况下,仅在创建 socketClint
后调用 Bind()
。不要在每次调用 socketClint.ReceiveFrom()
之前调用它。