我已经解决了我的问题无法访问已处置的对象。在 C# 客户端和服务器中
以下是我使用的要点。
- 用于范围限制
- 我不是封闭的套接字对象
类客户端 { 静态无效 Main(string[] args) { Console.Title = "客户聊天"; byte[] bytes = new byte[1024];// 传入数据的数据缓冲区 string data = null;
// 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);
using (Socket Socketsender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Socketsender.Connect(ipEndPoint);
Console.WriteLine("\n\n\tSocket Connecting To Java Server...." + Socketsender.RemoteEndPoint.ToString());
while (true)
{
Console.Write("\n\n\tClient::");
string theMessage = Console.ReadLine();
byte[] msg = Encoding.ASCII.GetBytes(theMessage);
// Send the data through the socket
int bytesSent = Socketsender.Send(msg);
//Recieved from Java Server Message
int bytesRec = Socketsender.Receive(bytes);
Console.WriteLine("\n\n\tJava Server Says:: {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
}
//Socketsender.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}