Constructing MessagesTA: Awad A YounisClass: CS457Fall 2014
Outline Encoding Data Byte Ordering Formatting Project1 Packet Alignment and Padding Review HW22
Encoding Data TCP/IP protocols transport USER DATA without examination/modification. It simply counts bytes. We need to be careful about what we send (Application Protocol). How the data can be formatted by the sender? (Why?) Message made up of fields Each field contains specific peace of information. Example:Deposit, withdraw (It is all about numbers)Type: integer is (4 bytes) and short is (2 bytes) How can we encode them in C/C ? As string: Character encoding? As a value?3
Encoding Data1.As string: Character encoding? Sequence of bytes whose values are determined using character encodingsuch as ASCII. Example:x 17,998,720 and y 47,034,61549#define BUFSIZE 132char msgBuf[BUFSIZE];int x,y;sprintf(msgBuffer, “%d %d ”, x, y);send(clientSocket, msgBuffer, strlen(msgBuffer), 0);4
Encoding Data Disadvantages: msgBuffer must be large enough, Strlen counts only the bytes of the message We can not manipulate numbers in this encoding style (c does not have that operator) Each digit (0-9) can be represented using 4 bits, instead of one byte(?) Converting from string to integer is constrained by the native binaryrepresentation (32-bit [2,147,483,647], 64-bit)2. As a value?typedef struct {int x,y;} msgStruct;msgStruct.x x;msgStruct.y y;send(clientSock, &msgStruct, sizeof(msgStruct), 0);5
Encoding Data Disadvantage What type of encoding is usedo two’s complement,o sign/magnitude,o Unsigned. Native integer format: different between different machines’ representation e26
Byte Ordering Numbers are stored as integersint x,y; Problem: different machines (OS) uses different word orderings little-endian: lower bytes first big-endian: higher bytes first Solution: Network Byte Ordering Host Byte-Ordering: the byte ordering used by a host (big or little) Network Byte-Ordering: the byte ordering used by the network –always big-endian htonl(x); vs ntohl(x); htons(x); vs ntohs(x);7
Byte Ordering Example: The sender:msgStruct.dep htonl(deposits);msgStruct, wd htonl(withdrawals) ;send(s, &msgStruct, sizeof(msgStruct), 0); The receiver: needs to convert the values before using them, that is, to callntohl() on each integer value before using it.8
Formatting Project one PacketHeader#define MAXBUF 1000struct packet{u int16 t version;u int16 t length;} mypk;mypk.version htons(457);mypk.length htons(strlen(msg));char buf[MAXBUF];memcpy(buf, &mypk, 4);Message#define MAXDATASIZE 140#define MAXBUF 1000char buf[MAXBUF];char msg[MAXDATASIZE 1];memcpy(buf 4, msg, strlen(msg));if (send(mysock, buf, strlen(msg) 4, 0) -1)perror("send");9
Formatting Project one PacketFilling in the Messagewhile(1){write(1, "You:", 4);memset(msg, 0, sizeof msg); //empty msg before use//check the input msg's length,if input too long, re-inputwhile((n read(0, msg, MAXBUF)) MAXDATASIZE 1){printf("Error:Input too long!\n");fflush(stdin);write(1, "You:", 4);}msg[n-1] '\0';//add a '\0', replace the end "enter" .}10
Alignment and Padding When a message contains MULTIPLE binary-encoded fields: Alignment must be considered What is Alignment problem?struct {int centsDeposited;unsigned short numDeps;int centsWithdrawn;unsigned short numWds;} msgBuf;/* assign values to fields -- convert byte order! */send(s, &msgBuf, sizeof(msgBuf), 0);The size of the message should be 12 ( 4 2 4 2) bytes On some machines this code will not produce 12-byte message but rather a 14-bytemessage (why?)11
Alignment and Padding Two extra padding bytes added by the complier to aligns the fields of structures tocertain word boundaries based on their type To understand alignment: gnment-padding-and-data-packing/) Data structures (c structu) are maximally aligned, that is, their addresses will bedivisible by the size of the largest native integer. Other multibyte fields are aligned to their size, that is, a four-byte integer's addresswill be divisible by four, and a two-byte integer's address will be divisible by two. Solution?1. Include padding to data structure (How?)2. Reorder fields (How?)12
Alignment and Padding Solution?1. Include padding to data structuretypedef struct {int x;short x2;char pad[2];int y;short y2;} msgStruct;2. Reorder fieldstypedef struct {int x; int y;short x2; short y2;} msgStruct;13
Thank YouReference Pocket Guide to TCP/IP Socket, by Michael J. Donahoo and KennethL. Calvert Beej’s Guide to Network Programming Using Internet Sockets, byBrian "Beej" Hall. (http://www.cs.columbia.edu/ 14
Byte Ordering Numbers are stored as integers int x,y; Problem: different machines (OS) uses different word orderings little-endian: lower bytes first big-endian: higher bytes first Solution: Network Byte Ordering Host Byte-Ordering: the byte ordering used by a host (big or little) Network Byte-Ordering: the byte orderin
Communication between objects is depicted using messages. The messages appear in a sequential order on the lifeline. We represent messages using arrows. Lifelines and messages form the core of a sequence diagram. Messages can be broadly classified into the following categories: 1. Synchronous & Asynchronous messages 2. Create & Delete Message 3.
9 Filing Options- Dealing in Bulk From your inbox, you can select: A single message A collection of messages (click the box to the left of the star) Select a range of messages (shift click) Select all messages on the page (above first message) Select all messages in the Category Deselect some of the selected messages Deselect all of the selected messages
5. Planning Business Messages (Week 10) 6. Writing Business Messages (Week 10) 7. Completing Business Messages (Week 11) 9. Writing Routine and Positive Messages (Week 11) 10. Writing Negative Messages (Week 12) 11. Writing Persuasive Messages (Week 12) Final Exam Late Policy: None of the course assignments are eligible for late submission .
4 Planning Business Messages 99 5 Writing Business Messages 129 6 Completing Business Messages 159 PART 3 Digital, Social, and Visual Media 187 7 Digital Media 189 8 Social Media 215 9 Visual Media 247 PART 4 Brief Messages 279 10 Writing Routine and Positive Messages 281 11 Writing Negative Messages 307 .
Constructing Perpendicular Lines Step 4 –completed this is what your paper should look like. Constructing Perpendicular Lines Draw a line through The intersection and The point not on the line. Constructing Perpendicular Lines Yo
defines how an email client interacts with a server in order to download messages, determines how many messages are waiting and defines a method to retrieve the messages. POP3 is a simple protocol, and usually downloads all messages in your inbox as text streams; normally, it does not allow you to specify which messages to download.
10 NextGen Patient Portal Patient User Guide Inbox View messages sent by your health care team. You can sort incoming messages by the following: Date - messages will display by date, either newest to oldest or oldest to newest From - messages will be grouped together by sender Type - messages will be grouped together by message subject
The recovery process acts as a layer around the application process, and filters aII messages going into and coming out of the application process. Messages originating in the application process are called application messages, and messages originating in the recovery process are called recovery messages.