我是新來的服務器套接字任何東西,當試圖創建一個簡單的應用程序,我遇到了這個問題。我可以很好地連接到服務器應用程序,並重新連接。但是當我第二次斷開連接時出現錯誤。這裏是我的代碼,我希望有人能幫助我理解爲什麼。斷開然後重新連接客戶端到服務器應用程序C#
private static TcpListener clientListener;
private static Socket clientSocket;
private static bool running = false;
private static Thread runThread;
static void Main(string[] args){
writeMsg(">> Server started");
waitForConnection();
}
private static void writeMsg(String msg){
Console.WriteLine(msg);
}
private static void run(){
while (running){
try{
byte[] prefBuffer = new byte[100];
int bufferSize = clientSocket.Receive(prefBuffer);
writeMsg(">> Data recieved from client");
for (int i = 0; i < bufferSize; i++){
Console.Write(Convert.ToChar(prefBuffer[i]));
}
}
catch{
writeMsg("Connection Lost");
running = false;
clientListener.Stop();
clientSocket.Close();
waitForConnection();
}
}
runThread.Abort();
}
private static void waitForConnection(){
//This is the where the error is created and it says...
//Cannot access disposed object.
clientListener = new TcpListener(IPAddress.Parse("111.111.111.111"), 7414);
clientListener.Start();
writeMsg(">> Listening for connections...");
try{
clientSocket = clientListener.AcceptSocket();
writeMsg(">> Connection established");
running = true;
startRunThread();
}
catch (Exception e){
writeMsg(String.Format(">> Connection failed\n Error: {0}", e.Message));
}
}
private static void startRunThread(){
runThread = new Thread(new ThreadStart(run));
runThread.Start();
}
正如上面的代碼出現在註釋得到一個錯誤,即使我重新初始化它,我不能訪問一個釋放的對象? 這裏是堆棧跟蹤
在System.Net.Sockets.Socket.Listen(的Int32積壓)
在System.Net.Sockets.TcpListener.Start(的Int32積壓)
在的System.Net.Sockets .TcpListener.Start()在
在Server.Program.waitForConnection()... \的Program.cs:行55
http://stackoverflow.com/questions/27182180/how-to-re-connect-to-a-socket-gracefully – Larry