我已经建立了这个UDP客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDP_Client
{
class Program
{
static void Main(string[] args)
{
Program go = new Program();
go.Client();
}
void Client()
{
byte[] msg = new byte[1000];
Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint x = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
int PacketSize = sck.Receive(msg); // <- The exception is thrown here, when the client tries to receive a packet
Console.WriteLine(Encoding.ASCII.GetString(msg));
Console.ReadKey();
}
}
}
所以,问题是,每当我尝试使用客户端接收消息时,它都会抛出SocketException(提供了无效的参数)。
这是什么问题?在收到之前我忘了做某事吗?
我不会在这里使用Bind,因为它是客户端而不是服务器。