Presentation is loading. Please wait.

Presentation is loading. Please wait.

Onward with Chat! Networking CS 3470, Section 1.

Similar presentations


Presentation on theme: "Onward with Chat! Networking CS 3470, Section 1."— Presentation transcript:

1 Onward with Chat! Networking CS 3470, Section 1

2 Chat v1 Handing back grading sheets Average score = A- Common issues
Makefile Argument checking Hardcoded port number Even though taking it in as a parameter!

3 Makefile # This is a comment. Don’t use C-style comments CC=gcc CFLAGS=-Wall all: chat chat: chat.c $(CC) -o chat chat.c $(CFLAGS) clean: rm chat

4 Argument Checking Part #1
int main(int argc, char **argv) { /* Are we invoking the server or client? Let's figure it out based * on the number of arguments. */ if(argc > 1) client(argc, argv); else server(); return 0;

5 Argument Checking Part #2
int client(int argc, char **argv) { struct hostent *hp; /* Checking parameters */ /* There had better be exactly 5, else we are outa here. */ if (argc!=5) { print_usage(); exit(1); }

6 Argument Checking Part #3
/* There should be a flag in the first and third positions. */ if(strcmp(argv[1], "-p")==0) { port=atoi(argv[2]); host=argv[4]; } else if(strcmp(argv[3], "-p")==0) { port=atoi(argv[4]); host=argv[2]; else { /* Something is off */ print_usage(); exit(1);

7 Helpful Functions atoi – converts a string to an int
strcmp or strncmp – compares two strings

8 Any other questions?

9 Onward Next program: full chat! Back and forth chatting
Pretty formatting like on the original project specification Packet formatting Version header Src IP header Dst IP header Checksum field Length of message (always 140) Bad chat For testing!

10 Back and Forth Chatting
Do this first! Both send() and rcv() must be in the client and server function’s while loops

11 Packet Formatting Your packet should look like this
(Field sizes coming soon) Version Src IP Dest IP Data Checksum

12 Packet Formatting Version = 2 Src IP = IP of sender
Dest IP = IP of receiver Data = chat message, always 140 bytes/characters long Checksum Version Src IP Dest IP Data Checksum

13 Packet Formatting How do I do this?
Create a struct packet that holds all those fields Send the struct packet over the socket

14 Creating a Struct Packet
Assign 2 to version field Get source and destination IP from socket, assign to src and dest fields Get user-typed chat message from fgets(), use strcpy (“string copy”) to copy it into data field Use checksum function on page 95 of your book to compute checksum of all previous fields

15 Checksum Sender creates checksum, copies it to end of packet, and sends packet through socket Receiver receives packet, creates checksum, and checks to see that computed checksum matches checksum found in packet If matches, it shouldn’t do anything If it doesn’t match, it should display an error to the user.

16 Bad Chat How can we test our checksum routines?
We can invoke a bad checksum routine on chat by using a “–b” flag with either the client or the server This will invoke a second “bad checksum” function that appends an incorrect checksum to our packet Computing checksum on receiver side should always use “good” checksum function

17 Other Things Cannot have any pointers in struct packet before sending it over the socket Why?

18 Other Things Must be aware of byte ordering!

19 Byte Ordering High-order byte Low-order byte MSB 16-bit value LSB
Increasing memory address Address A+1 Address A Little-endian byte order High-order byte Low-order byte MSB bit value LSB Consider a 16-bit integer that is made up of 2 bytes: there are two ways to store the 2 bytes in memory, with low order byte at the starting address, known as little endian byte order, or with the high order byte at the starting address, known as big endian byte order Big-endian byte order Low-order byte High-order byte Address A+1 Address A Increasing memory address

20 Implications of Byte Order
Unfortunately there is no standard between these two byte orderings and we encounter systems that use both formats We refer to the byte ordering used by a given system as host byte order The sender and the receiver must agree on the order in which the bytes of these multi-byte field transmitted: specify network byte order, which is big-endian byte ordering

21 Byte Order Functions #include <netinet.h> /* Host to network */ uint16_t htons(uint16_t host16bitvalue) Converts a 16-bit integer from host to network byte order uint32_t htonl(uint32_t host32bitvalue) Converts a 32-bit integer from host to network byte order Both return: value in network byte order /* Network to host */ uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohl(uint32_t net32bitvalue) Both return: value in host byte order

22 When do we use hton/ntoh functions?
Use hton the port number in struct sockaddr_in If we create a custom struct to hold our headers and data Sending our data through send() and recv() functions E.g., if our first struct member is a 2-byte header, and sender/receiver have different memory orderings, number would look very different to each machine

23 Address Conversion Functions
#include <arpa/inet.h> in_addr_t inet_addr(const char *strptr); /* return 32-bit binary network byte ordered IPv4 address; INADDR_NONE if error, deprecated and replaced by inet_aton() */ char *inet_ntoa(struct in_addr inaddr); /* returns: pointer to dotted-decimal string */


Download ppt "Onward with Chat! Networking CS 3470, Section 1."

Similar presentations


Ads by Google