Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送或接收数据的请求被拒绝,因为套接字未连接-在发送数据时_C#_Sockets_Udp - Fatal编程技术网

C# 发送或接收数据的请求被拒绝,因为套接字未连接-在发送数据时

C# 发送或接收数据的请求被拒绝,因为套接字未连接-在发送数据时,c#,sockets,udp,C#,Sockets,Udp,当服务器接收到“调试”时,我试图将数据发送回客户端。ATM下面提供了此错误: 不允许发送或接收数据的请求,因为套接字未连接,并且(使用sendto调用在数据报套接字上发送时)未提供地址 添加了我的主类以帮助回答问题 static Socket newSocket; static byte[] data; static EndPoint tmpRemote; static IPEndPoint sender, endpoint; static int rec

当服务器接收到“调试”时,我试图将数据发送回客户端。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);
        }
    }

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

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

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

在您的情况下

解决方案

改为尝试SendTo()


你不需要先打开一个套接字吗?@BlessedGeek如果是这个问题,错误消息会有所不同。@BlessedGeek我已将我的主要方法添加到代码中,以帮助回答任何问题。我相信套接字是打开的。我已经将我的主要方法添加到代码中,以帮助回答任何问题。我相信套接字是打开的。@AndrewZak套接字当然是打开的,但它没有连接,而且您还没有通过调用
sendmsg()
提供目标地址。你必须做其中一件事,通常是后者。
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();