private static AccreditRsp AccreditResult(TcpClient tcpClient) { AccreditRsp result = null; BinaryReader br; try { byte[] byteArray = new byte[10000]; NetworkStream clientStream = tcpClient.GetStream(); br = new BinaryReader(clientStream); int len = br.Read(byteArray, 0, byteArray.Length); byte[] data = new byte[len]; Array.Copy(byteArray, data, len); MemoryStream ms = new MemoryStream(data); result = Serializer.Deserialize <AccreditRsp>(ms); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } finally { } return(result); }
public void Init(string name, string password) { string serverIp = "119.23.12.116"; try { IPAddress remoteIP = IPAddress.Parse(serverIp); TcpClient mainServerTcpClient = new TcpClient(); mainServerTcpClient.Connect(new IPEndPoint(remoteIP, 13333)); SendToServer(mainServerTcpClient, name, password); LoginRsp result = LoginResult(mainServerTcpClient); if (LoginRsp.ResultCode.FAIL.Equals(result.resultCode)) { MessageBox.Show("用户名或密码错误,请重新输入"); mainServerTcpClient.Close(); return; } else if (LoginRsp.ResultCode.LOGGEDIN.Equals(result.resultCode)) { MessageBox.Show("该账号已登录"); mainServerTcpClient.Close(); return; } else if (LoginRsp.ResultCode.NOTMAINPHONE.Equals(result.resultCode)) { MessageBox.Show("该设备不是主设备"); Window loading = null; Thread t = new Thread(new ThreadStart(delegate { App.Current.Dispatcher.Invoke((Action)(() => { loading = new LoadingWindow(); loading.ShowDialog(); })); })); t.IsBackground = true; t.Start(); Console.WriteLine("waite result"); AccreditRsp accreditResult = AccreditResult(mainServerTcpClient); if (AccreditRsp.ResultCode.CANLOGIN.Equals(accreditResult.resultCode)) { SendToServer(mainServerTcpClient, name, password); MessageBox.Show("主设备同意登录"); App.Current.Dispatcher.Invoke((Action)(() => { if (loading != null) { loading.Close(); } })); MessageBox.Show("授权成功"); util.config.ConfigUtil.SetValue("longconnect", "true"); util.config.ConfigUtil.SetValue("username", name); util.config.ConfigUtil.SetValue("password", password); App.Current.Dispatcher.Invoke((Action)(() => { this.Close(); })); MainWindow.main.Status = "长连接开启"; } else if (AccreditRsp.ResultCode.FAIL.Equals(accreditResult.resultCode)) { App.Current.Dispatcher.Invoke((Action)(() => { if (loading != null) { loading.Close(); } })); MessageBox.Show("授权被拒绝"); MainWindow.main.Status = "长连接失败"; mainServerTcpClient.Close(); return; } } else if (LoginRsp.ResultCode.MAINPHONEOUTLINE.Equals(result.resultCode)) { MessageBox.Show("主设备不在线"); MainWindow.main.Status = "长连接失败"; mainServerTcpClient.Close(); return; } else if (LoginRsp.ResultCode.SUCCESS.Equals(result.resultCode)) { MessageBox.Show("登录成功"); util.config.ConfigUtil.SetValue("longconnect", "true"); util.config.ConfigUtil.SetValue("username", name); util.config.ConfigUtil.SetValue("password", password); App.Current.Dispatcher.Invoke((Action)(() => { this.Close(); })); // can also be any object you'd like to use as a parameter MainWindow.main.Status = "长连接已开启"; //启动长连接 ClientPCNew2.StartService(mainServerTcpClient); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }