当服务器收到“调试”时,我正在尝试将数据发送回客户端。ATM 以下提供此错误:
不允许发送或接收数据的请求,因为未连接套接字并且(当使用 sendto 调用在数据报套接字上发送时)未提供地址。
添加了我的主要课程以帮助回答问题
static Socket newSocket;
static byte[] data;
static EndPoint tmpRemote;
static IPEndPoint sender, endpoint;
static int recv;
static void Main(string[] args)
{
data = new byte[1024];
endpoint = new IPEndPoint(IPAddress.Any, 3000);
newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newSocket.Bind(endpoint);
sender = new IPEndPoint(IPAddress.Any, 904);
tmpRemote = (EndPoint)sender;
newSocket.BeginReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote, new AsyncCallback(OperatorCallBack), data);
Console.Read();
}
private static void OperatorCallBack(IAsyncResult ar)
{
log("[" + DateTime.Now + "][New Connection] " + tmpRemote.ToString() + "");
try
{
int size = newSocket.EndReceiveFrom(ar, ref tmpRemote);
if (size > 0)
{
data = (byte[])ar.AsyncState;
string[] dataCommand = Encoding.ASCII.GetString(data, 0, size).Split(' ');
if (dataCommand[0] == "debug")
{
newSocket.Send(Encoding.ASCII.GetBytes("HA IT WORKED :)"));
log("Sent debug");
}
else
{
log("Invalid Command");
}
}
data = new byte[1024];
newSocket.BeginReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote, new AsyncCallback(OperatorCallBack), data);
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}