简体   繁体   English

System.NotSupportedException:不允许在未连接的套接字上执行该操作

[英]System.NotSupportedException: The operation is not allowed on non-connected sockets

I am creating a C# application in which I send files over a LAN connection via sockets, but I encounter an "System.NotSupportedException" when I call the Send function 我正在创建一个C#应用程序,在其中我通过套接字通过LAN连接发送文件,但是在调用Send函数时遇到“ System.NotSupportedException”

This is the function of the receiver of the file. 这是文件接收者的功能。

    Socket _newSocket;
    List<byte> endBuffer;

    // Used to connect to the server
    private void button1_Click(object sender, EventArgs e)
    {
        _newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _newSocket.Connect(IPAddress.Parse("127.0.0.1"), _PORT);
    }

    // Used to Receive the file
    private void button3_Click(object sender, EventArgs e)
    {
        _newSocket.Receive(endBuffer.ToArray(), SocketFlags.None);
        MessageBox
            .Show(Encoding.ASCII.GetString(endBuffer.ToArray(), 0, endBuffer.Count));
    }

This is the function I used to start the server. 这是我用来启动服务器的功能。

void SetupServer(object sender)
    {
        Button temp = (Button)sender;
        temp.Enabled = false;
        _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _serverSocket.Bind(new IPEndPoint(IPAddress.Parse(txtConnectToIP.Text), _PORT));
        _serverSocket.Listen(5);
        _serverSocket.BeginAccept(AcceptCallback, null);
    }

    void AcceptCallback(IAsyncResult AR)
    {
        Socket socket;

        try
        {
            socket = _serverSocket.EndAccept(AR);
        }
        catch
        {
            MessageBox.Show("Not Working!");
            return;
        }

        _clientSockets.Add(socket);
        int id = _clientSockets.Count - 1;
        IPAddress ip = IPAddress.Parse(_serverSocket.LocalEndPoint.ToString().Split(':')[0]);
        string HostName = Dns.GetHostEntry(ip).HostName.Split('.')[0];
        string finalName = HostName + " (" + ip.ToString() + ")";
        AddClientForSelectionCallback(finalName);
    }

This is the send function. 这是发送功能。

byte[] postBuffer = Encoding.ASCII.GetBytes("Sending Complete!");

    void Send(string filePath)
    {
        try
        {
            _serverSocket.SendFile(filePath, null, postBuffer, TransmitFileOptions.ReuseSocket);
        }
        catch (Exception ec)
        {
            MessageBox.Show("Failed with error message:\n   "+ ec);
        }
    }

Lastly, how would Receive the file sent because _newSocket.Receive() does not seem to work. 最后,由于_newSocket.Receive()似乎不起作用,如何接收发送的文件。 I have looked at storing its result in a var, but Receive returns nothing and I will try using ConnectAsync. 我已经看过将其结果存储在var中,但是Receive不返回任何内容,我将尝试使用ConnectAsync。

Help would really be appreciated. 帮助将不胜感激。

In your program, _serverSocket is the socket that accepts incoming connections, you cannot send data to it. 在您的程序中, _serverSocket是接受传入连接的套接字,您无法向其发送数据。

As soon as a connection is accepted, you get the socket for that connection: 接受连接后,您将获得该连接的套接字:

socket = _serverSocket.EndAccept(AR);

You can use this socket instance to communicate with that specific client. 您可以使用此套接字实例与该特定客户端进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM