【问题标题】:Socket losses connection after "Socket.Receive" function“Socket.Receive”函数后套接字丢失连接
【发布时间】:2019-06-24 15:21:59
【问题描述】:

我有一个 C# 客户端。客户端的任务是登录服务器。

问题是: 当我想登录时,我使用初始化“wpf”窗口时创建的 TCP 套接字。使用套接字发送一次数据后,一切正常,但是当我想再次使用相同的套接字发送数据时,会弹出此异常:

System.ObjectDisposedException: '无法访问已处置的对象。 对象名称:'System.Net.Sockets.Socket'。'

经过一番测试,我发现问题是由Socket.Receive函数引起的。我在函数调用前检查了socket,socket已经连接(Socket.Connected == true),从函数返回后,socket没有连接(Socket.Connected == false)

private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
// Get host related information.
IPHostEntry hostEntry = Dns.GetHostEntry(server);

// Loop through the AddressList to obtain the supported AddressFamily.
foreach (IPAddress address in hostEntry.AddressList)
{       
    //attempting to connect.
    IPEndPoint ipe = new IPEndPoint(address, port);
            //making a temp socket to check the connection (if something went wronge/ the server isnt active)
            Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    try
    {
        // attempting connection.
        tempSocket.Connect(ipe);
            }
            catch (Exception)
            {
                Console.WriteLine("Request timed out");
            }

            //if we connected to the server, we are ok to continue.
            if (tempSocket.Connected)
            {
                s = tempSocket;
                break;
            }
            //else the connection isnt successful (the server might not respond) we need to try again.
            else
            {
                continue;
            }
}
Globals.SOCKET = s;
return s;
}

//This func will send a request to the server and returns the server's response.
public static string SocketSendReceive(string server, int port, string request, Socket socket = null)
{
    Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
    Byte[] bytesReceived = new Byte[256];
    string response = "";
    // Create a socket connection with the specified server and port.
    if (socket == null)
        socket = ConnectSocket(server, port);
    using (socket)
    {
        // If the connection faild and couldnt maintain a socket.
               if (socket.Connected == false)
            return ("Connection failed");

        // Send request to the server.
        socket.Send(bytesSent, bytesSent.Length, 0);

                // Receiving the packet from the server.
                int bytes = socket.Receive(bytesReceived, bytesReceived.Length,0);//***The problem occures Here***
               response = response + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
    }
    return response;
}

第二个函数是从服务器发送和接收数据的函数。 第一个只是连接并创建一个套接字

感谢您的帮助! -安东尼

【问题讨论】:

  • using(socket) 表示套接字将被放置在using 块的末尾...您可能不应该是using(socket),因为该函数不“拥有”套接字你传入了。这将在第一次调用时起作用,所有后续调用(以及socket 的使用)都将失败。

标签: c# sockets


【解决方案1】:

改变

socket.Receive(bytesReceived, bytesReceived.Length,0)

socket.Receive(bytesReceived, 0, bytesReceived.Length)

【讨论】:

  • 感谢您的回答。问题是“使用(套接字){}”范围。如果你删除它,一切正常。
  • 这不是真的,你可以看看MSDN。 func(char[] buffer, int size, sokcetFlag)
猜你喜欢
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 2023-03-10
  • 2019-08-16
相关资源
最近更新 更多