我修复了我的问题无法访问已处置的对象 . 在c#client&Server中
我使用的点数 .
-
用于范围限制
-
我不是封闭的套接字对象
class Client {static void Main(string [] args){Console.Title =“Client Chat”; byte [] bytes = new byte [1024]; //传入数据字符串数据的数据缓冲区= 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();
}
}
1 回答
您在循环外创建Socket处理程序对象并在循环内关闭它 . 第二次通过你的循环,你正在查看已经关闭的Socket对象 .
在完成之前,请勿关闭插座 .