public static void Disconnect(IErrorLogger log)//异常回调 { isOnline = false; try { binaryReader.Close(); binaryWriter.Close(); netStream.Close(); server.Close(); Console.WriteLine("Disconnect"); } catch (Exception ex) { log.HandleException(ex); } }
public static void Disconnect(IErrorLogger log) { isOnline = false; try { binaryReader.Close(); binaryWriter.Close(); netStream.Close(); server.Close(); if (parentForm != null) { parentForm.Invoke((MethodInvoker) delegate() { parentForm.Close(); }); } } catch (Exception ex) { log.HandleException(ex); } }
private static void SetupFields(IErrorLogger log, string ipAdress, int port) { if (server != null) { server.Close(); } server = new TcpClient(); try { server.Connect(ipAdress, port); netStream = server.GetStream(); binaryReader = new BinaryReader(netStream, Encoding.UTF8); binaryWriter = new BinaryWriter(netStream, Encoding.UTF8); binaryFormatter = new BinaryFormatter(); isOnline = true; } catch (Exception e) { log.HandleException(e); } }
private static void SetupFields(IErrorLogger log, string ipAdress, int port) //连接服务器 { if (server != null) //如果服务器存在 { server.Close(); //关闭线程 } server = new TcpClient(); //新建连接 try { event1 = new AutoResetEvent(false); //初始化WaitOne event2 = new AutoResetEvent(false); //初始化WaitOne server.Connect(ipAdress, port); //开始连接 netStream = server.GetStream(); //接收数据 netStream.WriteTimeout = 30 * 1000; //设置连接超时时间 binaryReader = new BinaryReader(netStream, Encoding.UTF8); //读流对象 binaryWriter = new BinaryWriter(netStream, Encoding.UTF8); //写流对象 binaryFormatter = new BinaryFormatter(); //新建一个二进制序列化数据实例 isOnline = true; //在线状态 } catch (Exception e) { log.HandleException(e);//输出异常信息,调试用 } }
public static void Stop(IErrorLogger log) { if (connectionTerminated == false) { try { isOnline = false; binaryWriter.Close(); binaryReader.Close(); netStream.Close(); client.Close(); server.Stop(); connectionTerminated = true; if (parentForm != null) { parentForm.Invoke((MethodInvoker) delegate() { parentForm.Close(); }); } } catch (Exception ex) { log.HandleException(ex); } } }
public static void Stop(IErrorLogger log) //错误回调 { if (connectionTerminated == false) //如果没终结 { try { isOnline = false; //设置状态为离线 binaryWriter.Close(); //关闭写流 binaryReader.Close(); //关闭读流 netStream.Close(); //关闭网络流 client.Close(); //关闭客户端 server.Stop(); //关闭服务端 connectionTerminated = true; //终结为真 if (parentForm != null) { parentForm.Invoke((MethodInvoker) delegate() { parentForm.Close(); });//如果出错,关闭主窗口 } } catch (Exception ex) { log.HandleException(ex);//异常日志 } } }