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

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Data Representation. Units & Prefixes Review kilo, mega, and giga are different in binary! bit (b) – binary digit Byte (B) – 8 binary digits KiloByte.
CSci 1130 Intro to Programming in Java
Addition 1’s to 20.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
COMP 14 Introduction to Programming
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Lecture 2b Sockets Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture - 2 Number systems and computer data formats
Introduction to Computers and Programming Lecture 7:
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 1 Data Storage. 2 Chapter 1: Data Storage 1.1 Bits and Their Storage 1.2 Main Memory 1.3 Mass Storage 1.4 Representing Information as Bit Patterns.
מבנה מחשב תרגול 2 ייצוג תווים בחומרה. A programmer that doesn’t care about characters encoding in not much better than a medical doctor who doesn’t believe.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
Pengantar Teknologi Informasi dan Ilmu Komputer Information Technology and Data Representation PTIIK- UB.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
C Tokens Identifiers Keywords Constants Operators Special symbols.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CISC1100: Binary Numbers Fall 2014, Dr. Zhang 1. Numeral System 2  A way for expressing numbers, using symbols in a consistent manner.  " 11 " can be.
Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Memory, Bits, & Bytes. Memory Part of the computer where programs and data are stored. Read and written (changed). Bit – Binary digit – Basic unit of.
Searching, Modifying, and Encoding Text. Parts: 1) Forming Regular Expressions 2) Encoding and Decoding.
FPL FAST Tech Summit London January FAST Technical Crash course public CFASTSerdes() { } /// /// Gets the presence map for a tag. /// /// CODEC being.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Lecture Coding Schemes. Representing Data English language uses 26 symbols to represent an idea Different sets of bit patterns have been designed to represent.
Unit 2.6 Data Representation Lesson 2 ‒ Characters
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Binary 1 Basic conversions.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
CSCI 198: Lecture 4: Data Representation
Chapter 6: Data Types Lectures # 10.
CSCI 161: Lecture 4: Data Representation
Binary, Decimal and Hexadecimal Numbers
Chapter 2.
Chapter 1 Data Storage.
Advanced Programming Behnam Hatami Fall 2017.
Comp Org & Assembly Lang
Presentation transcript:

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

» 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

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

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

» 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

» 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

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

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

» 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

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

» 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 © Sekolah Tinggi Teknik Surabaya

14 © Sekolah Tinggi Teknik Surabaya

» 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

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

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

// 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

» Refer to the handout 19 © Sekolah Tinggi Teknik Surabaya

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