MSDN里的socket.send(4参数)和(5参数)示例是一样的?
http://msdn.microsoft.com/zh-cn/library/ms145162.aspx 5参数
http://msdn.microsoft.com/zh-cn/library/4t14718h.aspx 4参数
都是
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
byte[] msg = Encoding.UTF8.GetBytes("This is a test");
byte[] bytes = new byte[256];
try
{
// Blocks until send returns.
int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
Console.WriteLine("Sent {0} bytes.", byteCount);
// Get reply from the server.
byteCount = server.Receive(bytes, 0, server.Available,
SocketFlags.None);
if (byteCount > 0)
Console.WriteLine(Encoding.UTF8.GetString(bytes));
}
catch (SocketException e)
{
Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
return (e.ErrorCode);
}
return 0;
}
让我对函数重载起了疑惑....
5参数的示例也是4参数的...那如果是msdn错了?那正确的是这样的?
public static int SendReceiveTest5(Socket server)
{
byte[] msg = Encoding.UTF8.GetBytes("This is a test");
byte[] bytes = new byte[256];
SocketError errormsg;
try
{
// Blocks until send returns.
int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None,out errormsg);
Console.WriteLine("Sent {0} bytes.", byteCount);
// Get reply from the server.
byteCount = server.Receive(bytes, 0, server.Available,
SocketFlags.None);
if (byteCount > 0)
Console.WriteLine(Encoding.UTF8.GetString(bytes));
}
catch (SocketException e)
{
Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
return (e.ErrorCode);
}
return 0;
}
------解决方案--------------------楼主太较真了。显然这两个页面都用了相同的一段示例代码,SendReceiveTest4未必就一定表示“4参数”示例,或者仅仅是指“第四个”示例呢?最重要是看明白了就好
MSDN上有些内容确实有误,不过主要是翻译成中文后的问题比较多
------解决方案--------------------
例子有问题,5参数有一个out errormsg,错误信息