private void send(int _src, int _dst, String _fun, String _data) { Packet packet = new Packet(); packet.headpacket.destination = _dst; packet.headpacket.source = _src; packet.headpacket.function = _fun; packet.data = _data; packet.headpacket.size = packet.data.Length; byte[] Datasend = getBytes(packet.headpacket); clientStream.Write(Datasend, 0, Datasend.Length); clientStream.Flush(); byte[] data = new byte[packet.headpacket.size]; data = Encoding.ASCII.GetBytes(packet.data); clientStream.Write(data, 0, data.Length); }
/// <summary> /// read from server /// </summary> /// <returns></returns> protected override serverReturnEventArgs receive() { try { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len - 4); clientStream.Flush(); String headdata = System.Text.Encoding.UTF8.GetString(receiveData); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); String data = System.Text.Encoding.UTF8.GetString(receiveData); return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)); } catch (System.OutOfMemoryException) { // Server gone wild :/ } catch (System.IO.IOException) { //disconected because you try to reach an other serv } catch (ObjectDisposedException) { //disconected because you try to reach an other serv } return new serverReturnEventArgs(0,0,"EERR", 2, "KO"); }
private serverReturnEventArgs receive() { Packet packet = new Packet(); int len = Marshal.SizeOf(new HeadPacket()); byte[] receiveData = new byte[len]; HeadPacket headpacket = new HeadPacket(); clientStream.Read(receiveData, 0, len); clientStream.Flush(); headpacket = fromBytes(receiveData); packet.headpacket = headpacket; receiveData = new byte[packet.headpacket.size]; clientStream.Read(receiveData, 0, packet.headpacket.size); clientStream.Flush(); return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination, packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)); }