Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

java - in c# getting Error cannot access a disposed object object name='System.Net.Socket.Socket'

I want a chat application in C# Client and in Java Server

I go through C# Client but I got some Error when I response to Java Server I get the error@

cannot access a disposed object object name='System.Net.Socket.Socket'

class Program
{
    static void Main(string[] args)
    {
        byte[] bytes = new byte[1024];// data buffer for incoming data

        // connect to a Remote device
        try
        {
            // Establish the remote end point for the socket
            IPHostEntry ipHost = Dns.Resolve("localhost");
           IPAddress ipAddr = ipHost.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 95);
            Socket Socketsender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Connect the socket to the remote endpoint
            Socketsender.Connect(ipEndPoint);

            Console.WriteLine("

___________________Client Server Chat Application__________________________");
            Console.WriteLine("___________________________________________________________________________");
            Console.WriteLine("
Socket Connecting To Java Server...." + Socketsender.RemoteEndPoint.ToString());
           // Console.ReadLine();
            string data = null;

            while (true) 
            {
                //Recieved from Java Server Message
                int bytesRec = Socketsender.Receive(bytes);
                Console.WriteLine("
Java Server:: {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
              //  Console.ReadLine();

                    Console.Write("C# Client  ::"); // Prompt
                    string line = Console.ReadLine();

                    byte[] sendToServer = Encoding.ASCII.GetBytes(line);
                    // Send the data through the socket
                    int intByteSend = Socketsender.Send(sendToServer);
               //  Socketsender.Shutdown(SocketShutdown.Both);

                Socketsender.Close();
                               Console.WriteLine("____________________________________________________________________________");
                Console.WriteLine("_________________________End Chat___________________________________________");                  
                // Socketsender.Shutdown(SocketShutdown.Both);
              Socketsender.Close();

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.ReadLine();
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are closing your socket (Socketsender) in he while loop, and then—in the next iteration—calling Receive on it.

Once a socket is closed it is dead,1 and cannot be used for anything. You would need to create a new socket and connect it to the server.

Or better, keep the first socket open.

(You also are performing the close twice, but I'm assuming that is a transcription error.)


1 Insert dead parrot here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...