Beispiel #1
0
 public virtual byte[] deliver_and_wait_feedback(IAddress theDest, byte[] data)
 {
     byte[] buffer = null;
     try
     {
         _client = new TcpClient(theDest.Dest(), theDest.Port());
         Console.WriteLine("Requestor: TCPClient {0}:{1}", theDest.Dest(), theDest.Port());
         _stream = _client.GetStream();
         _stream.WriteByte((byte)(data.Length >> 8));
         _stream.WriteByte((byte)(data.Length & 0xFF));
         _stream.Write(data, 0, data.Length);
         _stream.Flush();
         var val = _stream.ReadByte() << 8;
         val   |= _stream.ReadByte();
         buffer = new byte[val];
         _stream.Read(buffer, 0, buffer.Length);
     }
     catch (SocketException)
     {
         Console.WriteLine("IOException in deliver_and_wait_feedback");
     }
     finally
     {
         _stream.Close();
         _client.Close();
     }
     return(buffer);
 }
Beispiel #2
0
        public virtual void receive_transform_and_send_feedback(IByteStreamTransformer t)
        {
            try
            {
                _server = new TcpListener(_myAddr.Port());
                Console.WriteLine("Replyer TCPListener on port {0}", _myAddr.Port());

                _server.Start();
                _client = _server.AcceptTcpClient();
                Console.WriteLine("Replyer accept: TCPClient {0}:{1}", _myAddr.Dest(), _myAddr.Port());
                _stream = _client.GetStream();
                //correction performed for objects bigger than 256 bytes
                var val = _stream.ReadByte() << 8;
                val |= _stream.ReadByte();
                var buf = new byte[val];
                _stream.Read(buf, 0, val);
                byte[] data = t.Transform(buf);
                _stream.WriteByte((byte)(data.Length >> 8));
                _stream.WriteByte((byte)(data.Length & 0xFF));
                _stream.Write(data, 0, data.Length);
                _stream.Flush();
            }
            catch (SocketException)
            {
                Console.WriteLine("IOException in receive_transform_and_feedback");
            }
            finally
            {
                _stream.Close();
                _client.Close();
                _server.Stop();
            }
        }