Exemplo n.º 1
0
		void SendTcpMethod(string[] args)
		{
			//Check for correct amount of arguments
			if (args.Length != 2)
				throw new ArgumentException("Parameters: <Destination> <Port>");

			string server = args[0];
			int servPort = Int32.Parse(args[1]);

			//Create a socket that is connected to a server on a specifed port
			TcpClient client = new TcpClient(server, servPort);
			NetworkStream netStream = client.GetStream();

			//Make an item to quote
			ItemQuote quote = new ItemQuote(12345678909876554L, "5mm Super Widgets", 1000, 1299, true, false);

			//Send text-encoded quote
			ItemQuoteEncoderText coder = new ItemQuoteEncoderText();
			byte[] codedQuote = coder.encode(quote);
			Console.WriteLine("Sening text-encoded quote (" + codedQuote.Length + " bytes): ");
			Console.WriteLine(quote);

			netStream.Write(codedQuote, 0, codedQuote.Length);

			//Receive binary-encoded quote
			ItemQuoteDecoder decoder = new ItemQuoteDecoderBin();
			ItemQuote receivedQuote = decoder.decode(client.GetStream());
			Console.WriteLine("Reveived Binary-Encode Quote:");
			Console.WriteLine(receivedQuote);

		}
Exemplo n.º 2
0
		void SendUdpMethod(string[] args)
		{
			//Check for correct amount of arguments
			if ((args.Length < 2) || (args.Length > 3))
				throw new ArgumentException("Parameters: <Destination> <Port> [<Encoding>]");

			string server = args[0];
			int destPort = Int32.Parse(args[1]);

			//Make an item to quote
			ItemQuote quote = new ItemQuote(12345678909876554L, "5mm Super Widgets", 1000, 1299, true, false);

			UdpClient client = new UdpClient();

			ItemQuoteEncoder encoder = ((args.Length == 3) ? new ItemQuoteEncoderText(args[2]) : new ItemQuoteEncoderText());
			byte[] codedQuote = encoder.encode(quote);
			
			client.Send(codedQuote, codedQuote.Length, server, destPort);
			client.Close();
		}
		public byte[] encode(ItemQuote item)
		{
			MemoryStream mem = new MemoryStream();
			BinaryWriter output = new BinaryWriter(new BufferedStream(mem));

			output.Write(IPAddress.HostToNetworkOrder(item.itemNumber));
			output.Write(IPAddress.HostToNetworkOrder(item.quantity));
			output.Write(IPAddress.HostToNetworkOrder(item.unitPrice));

			byte flags = 0;
			if (item.discounted)
				flags |= ItemQuoteBinConst.DISCOUNT_FLAG;
			if (item.inStock)
				flags |= ItemQuoteBinConst.IN_STOCK_FLAG;
			output.Write(flags);

			byte[] encodedDesc = encoding.GetBytes(item.itemDescription);
			if (encodedDesc.Length > ItemQuoteBinConst.MAX_DESC_LEN)
				throw new IOException("Item description eceeds encoded length limit (" + ItemQuoteBinConst.MAX_DESC_LEN + ")");
			output.Write((byte)encodedDesc.Length);
			output.Write(encodedDesc);

			output.Flush();

			return mem.ToArray();
		}
		public byte[] encode(ItemQuote item)
		{
			string EncodedString = item.itemNumber + " ";
			if (item.itemDescription.IndexOf('\n') != -1)
				throw new IOException("Invalid description (contains newline).");
			EncodedString += item.itemDescription + "\n";
			EncodedString += item.quantity + " ";
			EncodedString += item.unitPrice + " ";

			if (item.discounted)
				//Only include 'd' if the item is discounted
				EncodedString += "d";
			if (item.inStock)
				//Only include 's' if the item is in stock
				EncodedString += "s";
			EncodedString += "\n";

			if (EncodedString.Length > ItemQuoteTextConst.MAX_WIRE_LENGTH)
				throw new IOException("Encoded length too long.");

			byte[] buf = encoding.GetBytes(EncodedString);
			return buf;
		}