private void ReceiveData(object state) { TcpClient client = (TcpClient)state/*clientTcp*/; NetworkStream ns = client.GetStream(); //byte[] buffer = new byte[1024]; IPEndPoint clientEndPoint = (IPEndPoint)client.Client.RemoteEndPoint; IPHostEntry clientHostEntry = Dns.GetHostEntry(clientEndPoint.Address.ToString()); string clientHostName = clientHostEntry.HostName; string serverHostName = Dns.GetHostName(); SocketEntity entity = new SocketEntity( Command.IF, new string[]{}, serverHostName, clientHostName); BollenSocket.Send(ns, entity); string keycoder = string.Empty; while (Valid) { CurrConnSta = ConnSta.Connected; try { entity = BollenSocket.Receive(ns); if (entity == null) { continue; } switch (entity.Cmd) { case Command.RQ: keycoder = entity.Data[0]; ClientList.Add(client, keycoder); this.UpdateView(new Workstation { KeyCoder = keycoder, WorkstationName = clientHostName }); break; case Command.KA: string answer; if (entity.Data[0] == "Success") { answer = InstructMap["Success"]; //delete blacklist; DeleteBlackList(entity.Data[1]); //save issue records IssueRecEntity recEntity = Manage.GetRecEntity(entity.Data[2]); if (recEntity != null) { UpdateRoomData(recEntity.RoomNumber, "2", recEntity.GuestName, recEntity.ArrivalDate, recEntity.DepartureDate); recEntity.Workstation = entity.Data[3]; recEntity.OperateTime = entity.Data[4]; InsertIssueRec(recEntity); } else { this.CommLog("Issue record save failed"); } } else { answer = InstructMap["Failure"]; } if (InterfaceClient == null) { this.CommLog("PMS interface is broken"); break; } answer = answer.Replace("*", keycoder); try { BollenSocket.Write(InterfaceClient.NetStream,Encoding.UTF8.GetBytes(answer)); } catch { break; } break; default: break; } } catch { //error process. try { ClientList.Remove(client); } catch { } break; } } if (keycoder != string.Empty) { frmWorkstation.SafeDel(keycoder); } if (ClientList.Count == 0) { CurrConnSta = ConnSta.Listen; } }
public static void Send(NetworkStream ns, SocketEntity data) { IFormatter formatter = new SoapFormatter(); MemoryStream mem = new MemoryStream(); formatter.Serialize(mem, data); byte[] buffer = mem.GetBuffer(); int memsize = (int)mem.Length; byte[] size = BitConverter.GetBytes(memsize); ns.Write(size, 0, 4); ns.Write(buffer,0,memsize); ns.Flush(); mem.Close(); }