Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Similar presentations


Presentation on theme: "Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."— Presentation transcript:

1 Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

2 » Suppose we want to send an item consisted of following information ˃Item number: A large integer identifying the item ˃Item description: A text string describing the item ˃Unit price: The cost per item in cents ˃Quantity: The number of units offered at that price ˃Discounted?: Whether the price includes a discount ˃In stock?: Whether the item is in stock 2 © Sekolah Tinggi Teknik Surabaya

3 public class ItemQuote { public long itemNumber; // Item identification number public String itemDescription; // String description of item public int quantity; // Number of items in quote (always >= 1) public int unitPrice; // Price (in cents) per item public Boolean discounted; // Price reflect a discount? public Boolean inStock; // Item(s) ready to ship? public ItemQuote() {} public ItemQuote( long itemNumber, String itemDescription, int quantity, int unitPrice, Boolean discounted, Boolean inStock) { this.itemNumber = itemNumber; this.itemDescription = itemDescription; this.quantity = quantity; this.unitPrice = unitPrice; this.discounted = discounted; this.inStock = inStock; } 3 © Sekolah Tinggi Teknik Surabaya

4 public override String ToString() { String EOLN = "\n"; String value = "Item# = " + itemNumber + EOLN + "Description = " + itemDescription + EOLN + "Quantity = " + quantity + EOLN + "Price (each) = " + unitPrice + EOLN + "Total Price = " + (quantity ∗ unitPrice); if (discounted) value += " (discounted)"; if (inStock) value += EOLN + "In Stock" + EOLN; else value += EOLN + "Out of Stock" + EOLN; return value; } 4 © Sekolah Tinggi Teknik Surabaya

5 » Data is a stream of bytes » An object may contains ˃String ˃Integer ˃Boolean ˃Etc. » Needs to convert an object to array of bytes 5 © Sekolah Tinggi Teknik Surabaya

6 6

7 » String of characters is translated into a sequence of bytes according to a character set ˃American Standard Code for Information Interchange (ASCII) +one-to-one mapping between a set of the most commonly used printable characters in English and binary values ˃Unicode Transformation Format (UTF) +UTF-8 +UTF-16 +UTF-32 7 © Sekolah Tinggi Teknik Surabaya

8 » Static Classes ˃ASCII ˃Unicode ˃UTF7 ˃UTF8 » Static Methods ˃GetBytes() ˃GetString() 8 © Sekolah Tinggi Teknik Surabaya

9 » Using ASCII » Using Unicode ˃Same result ˃Each character is represented in 2 bytes: the first byte is 0 9 © Sekolah Tinggi Teknik Surabaya

10 » Text representation is not efficient ˃Digit string can be represented in 4 bits » Agreements ˃Integer Size +short +int +long ˃Byte Order ˃Signed or Unsigned 10 © Sekolah Tinggi Teknik Surabaya

11 » Long (64 bits), big-endian » Long (64 bits), Little-endian Big-endian is more common 11 © Sekolah Tinggi Teknik Surabaya

12 » Write or Read Two’s-complement Representation » BinaryWriter has Write() method that accept short, int, and long » BinaryReader has ˃ ReadInt16() ˃ ReadInt32() ˃ ReadInt64() 12 © Sekolah Tinggi Teknik Surabaya

13 13 © Sekolah Tinggi Teknik Surabaya

14 14 © Sekolah Tinggi Teknik Surabaya

15 » The problem of enabling the receiver to locate the beginning and end of the message in the stream and of the fields within the message. » Do ˃Received all of the message ˃Parse it into fields. 15 © Sekolah Tinggi Teknik Surabaya

16 » Need framing information ˃Delimiter ˃Explicit Length 16 © Sekolah Tinggi Teknik Surabaya

17 public class Framer { public static byte[] nextToken(Stream input, byte[] delimiter) { int nextByte; // If the stream has already ended, return null if ((nextByte = input.ReadByte()) == -1) return null; MemoryStream tokenBuffer = new MemoryStream(); do { tokenBuffer.WriteByte((byte)nextByte); byte[] currentToken = tokenBuffer.ToArray(); if (endsWith(currentToken, delimiter)) { int tokenLength = currentToken.Length - delimiter.Length; byte[] token = new byte[tokenLength]; Array.Copy(currentToken, 0, token, 0, tokenLength); return token; } } while ((nextByte = input.ReadByte()) != -1); // Stop on EOS return tokenBuffer.ToArray(); // Received at least one byte } 17 © Sekolah Tinggi Teknik Surabaya

18 // Returns true if value ends with the bytes in the suffix private static Boolean endsWith(byte[] value, byte[] suffix) { if (value.Length < suffix.Length) return false; for (int offset=1; offset <= suffix.Length; offset++) if (value[value.Length - offset] != suffix[suffix.Length - offset]) return false; return true; } 18 © Sekolah Tinggi Teknik Surabaya

19 » Refer to the handout 19 © Sekolah Tinggi Teknik Surabaya

20 » David Makofske, Michael J. Donahoo, Kenneth L. Calvert, TCP/IP Sockets in C#: Practical Guide for Programmers, Morgan Kaufmann, 2004 » Unicode Fact, 20 © Sekolah Tinggi Teknik Surabaya


Download ppt "Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."

Similar presentations


Ads by Google