public void Send(object data) { try { //BinaryFormatter formatter = new BinaryFormatter(); //MemoryStream rems = new MemoryStream(); //formatter.Serialize(rems, data); byteDatas = SeatManage.SeatManageComm.ByteSerializer.ObjectToByte(data); //byteDatas = data as byte[]; client.Connect(); autoConnectEvent.WaitOne(2000); client.Disconnect(); } catch { try { Thread.Sleep(5000); client.Connect(); autoConnectEvent.WaitOne(2000); client.Disconnect(); } catch { return; } } }
public void Connect() { try { Console.WriteLine("正在连接到服务器……"); client.Connect(true); Console.WriteLine("连接成功"); SeatManage.SeatManageComm.WriteLog.Write("已与服务器连接,等待终端请求。"); } catch (SocketException ex) { Console.WriteLine("连接失败,10s后重连…"); Thread.Sleep(10000); Connect(); } }
/// <summary> /// 连接到socket服务器。链接出错会重试。错误超过三次,则抛出异常。 /// </summary> /// <param name="connectTimes"></param> /// <returns></returns> private bool SocketConnect(int connectTimes) { try { return(client.Connect());//连接 } catch (Exception ex) { if (connectTimes > 3) { throw ex; } else { connectTimes++; Thread.Sleep(200); return(SocketConnect(connectTimes)); } } }
public SeatManage.ClassModel.ReaderInfo CheckAndGetReaderInfo(UserInfo user) { try { lock (this.client) { client.Connect();//连接 request = new SocketRequest(); request.MethodName = "CheckAndGetReaderInfo"; request.Parameters.Add(user); request.SubSystem = TcpSeatManageSubSystem.WeiXinSeatBespeak; request.Target = schoolNum; request.MsgType = TcpMsgDataType.Relay; if (request != null) { client.Send(SeatManage.SeatManageComm.ByteSerializer.ObjectToByte(request)); Console.WriteLine("请求客户端{0}执行{1}方法已发送,等待返回结果…", request.Target, request.MethodName); } autoConnectEvent.WaitOne(20000); if (response == null) { throw new Exception("连接超时。"); } else if (!isError && response.MethodName.Equals("CheckAndGetReaderInfo") && string.IsNullOrEmpty(response.ErrorMsg)) { return(response.Result as ReaderInfo); } else if (!string.IsNullOrEmpty(response.ErrorMsg)) { throw new Exception(response.ErrorMsg); } else { return(null); } } } catch (Exception ex) { throw ex; } finally { request = null; response = null; client.Disconnect(); } }