lecture 4 sending and receiving messages erick pranata © sekolah tinggi teknik surabaya 1

20
Network Programmi ng Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Upload: terrell-willson

Post on 14-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Network Programming

Lecture 4 Sending and Receiving Messages

Erick Pranata© Sekolah Tinggi Teknik Surabaya

1

Sending an Object» 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

ItemQuote.cspublic 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

ItemQuote.cs 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

Transport Layer» 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

Representing Data© Sekolah Tinggi Teknik Surabaya

6

Text Representation» 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

System.Text in .Net» Static Classes˃ ASCII˃ Unicode˃ UTF7˃ UTF8

» Static Methods˃ GetBytes()˃ GetString()

8

© Sekolah Tinggi Teknik Surabaya

"item#=123456"» Using ASCII

» Using Unicode˃ Same result˃ Each character is represented in 2 bytes:

the first byte is 0

9

© Sekolah Tinggi Teknik Surabaya

Binary Numbers Representation» 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

"item#=12345654321"» Long (64 bits), big-endian

» Long (64 bits), Little-endian

Big-endian is more common 11

© Sekolah Tinggi Teknik Surabaya

BinaryWriter and BinaryReader» 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

Composing I/O Streams

13

© Sekolah Tinggi Teknik Surabaya

.Net I/O Classes

14

© Sekolah Tinggi Teknik Surabaya

Framing» 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

Framing» Need framing information˃ Delimiter˃ Explicit Length

16

© Sekolah Tinggi Teknik Surabaya

Framer.cspublic 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

Framer.cs // 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

Implement the Client» Refer to the handout

19

© Sekolah Tinggi Teknik Surabaya

Referensi» David Makofske, Michael J. Donahoo,

Kenneth L. Calvert, TCP/IP Sockets in C#: Practical Guide for Programmers, Morgan Kaufmann, 2004

» Unicode Fact, http://unicode.org/faq/utf_bom.html

20

© Sekolah Tinggi Teknik Surabaya