发送或接收数据的请求被禁止,因为套接字未连接 - 发送数据时

问题描述 投票:0回答:4

当服务器收到“调试”时,我尝试将数据发送回客户端。 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);
        }
    }
c# sockets udp
4个回答
3
投票

尝试通过慢速连接连接到套接字时,我遇到了类似的问题。我通过在任何发送/接收调用之前确保 newSocket.Connected 属性为 true 来解决这个问题。


2
投票

错误信息非常清楚。您正在未连接的套接字上调用 send() 且未提供目标地址。你要寄到哪里? UDP不知道。


2
投票

通过 ProtocolType.Udp 发送数据时不需要连接(socket.connect()) 当您不建议 UDP 消息应转发的地址时,可能会发生该错误

就您而言 这里没有提供udp Send()的地址

解决方案

尝试使用 SendTo() 代替

Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
soc.EnableBroadcast = true;
IPEndPoint ipend = new IPEndPoint(IPAddress.Broadcast, 58717);
EndPoint endp = (EndPoint)ipend;
byte[] bytes = new byte[1024];

bytes = Encoding.ASCII.GetBytes(str);

soc.SendTo(bytes,ipend);

soc.Close();

0
投票

尝试通过慢速连接连接到套接字时,我遇到了类似的问题。我在调用接收之前通过此代码解决了它:

            for (int i = 0; i < 50; i++)
            {
                if (!client.Connected)
                {
                    Thread.Sleep(50);
                }
                else
                {
                    break;
                }
            }

            if (!client.Connected)
            {
                SendMessage("Unable to connect to network!");
            }
            

            // Begin receiving the data from the remote device.  
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
© www.soinside.com 2019 - 2024. All rights reserved.