UDPClient构造函数不断抛出错误:10022

时间:2014-12-10 23:57:26

标签: c# sockets udp

下面的代码总是在语句中抛出异常:UdpClient receivingUdpClient = new UdpClient(); 它抛出的错误代码是10022,这是无效的参数。我不确定构造函数如何抛出此错误。

提前感谢您提供的任何帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Test_UDP_Console_program
{
    class Program
    {
        private const int listenPort = 4999;
        static void Main(string[] args)
        {
            try
            {
                UdpClient receivingUdpClient = new UdpClient();
                receivingUdpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.255"), 4999));
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
                receivingUdpClient.Close();
            }
            catch(SocketException e)
            {
                Console.WriteLine(e.ErrorCode.ToString());
            }
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,当使用套接字时,您必须出于某种原因从本地驱动器而不是网络驱动器运行程序。