Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.

Similar presentations


Presentation on theme: "Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich."— Presentation transcript:

1 Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich

2 What will be covered ● Basics of networking ● Simple network client ● Simple network server ● Transform / Echo server

3 Basics of Networking ● What is a socket? – Input / Output port used for transmitting data over a network – In Unix, a socket is actually a file descriptor! (called a special file) It's one of those virtualization things ● How do I refer or address a specific socket? – A socket can be addressed by an IP address and port. – IP address is a way to find a computer on a network. – Port is a way by which the operating system directs traffic to specific applications. (EX: Port 80 is used for HTTP requests.)

4 Types of Sockets ● Datagram Socket – No connection, simply build a packet and send it to the destination. – No guarantee of completion, receipt, or message accuracy – Used in UDP protocol ● Stream Socket – A connection oriented socket. Connection is established – Messages are acknowledged when received. Acknowledge bytes received to ensure the correctness of message – In actuality, built on top of datagram socket with extra messages to maintain connection state – Used in TCP protocol

5 Basics of Networking, Cont. ● “Network Order” – You want to send data over a socket, you must put into serial format, that is, the data must be a continuous chunk of bits. – There is a specific order of bits as agreed upon on the internet is known as network order. – Operating system may have different order of bits for certain data types.... – 'htonl', 'htons', 'ntohl', 'ntohs' functions ● Basic Communication Model

6 A Simple Socket Client ● Our goal: An application that opens a socket and downloads google's webpage ● Steps – Prerequisites to make it compile – Create a socket – Lookup www.google.com IP address via DNSwww.google.com – Create a struct containing the info to connect to Google – Connect to google's HTTP port (80) – Send HTTP GET request – Recv a set of bytes

7 Create A Socket ● int socket(int domain, int type, int protocol); – Domain: – Type: SOCK_STREAM or SOCK_DGRAM for stream and datagram sockets, respectively – Protocol: use 0 to have socket automatically pick correct one – Return: an integer called the socket file descriptor. aka “sockfd” in future parts of tutorial.

8 Lookup Google via DNS ● struct hostent *gethostbyname(const char *name); – Lookup Call, returns a struct of type hostent containing the information about the host looked up ● hostent is a struct containing host entity information. – Members ● h_name

9 Create a struct with Google's address – sin_family: AF_INET (for our purposes) ● googleaddr.sin_family = AF_INET; – sin_port: port number in network order ● googleaddr.sin_port = htonl(80); – sin_addr: – sin_zero: must be set to all zero's ● memset(&(googleaddr.sin_zero), '\0', 8);

10 Connect to Google ● int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); – Sockfd: Socket file decriptor – serv_addr: constructed in previous slide – Addrlen: length of the struct serv_addr ● sizeof(struct sockaddr) – Return: -1 if error.

11 Make HTTP GET Request ● Send a message using the connected socket ● int send(int sockfd, const void *msg, int len, int flags); – Sockfd: socket file descriptor to use to send message – Msg: a pointer to a stream of bytes of a message. ● Specific byte sequence for HTTP GET: ?? – Len: the length of a message in number of bytes – Flags: 0. For our purposes.

12 Receive response from Google ● int recv (int sockfd, void *buf, int len, unsigned int flags); – sockfd: Socket file descriptor – Buf: pointer to a buffer – Len: maximum length of the buffer – Flags: 0. for our purposes. – Return: number of bytes read ● -1: error

13 Extra Special Considerations ● Serialization – You want to send data over a socket, you must put into serial format, that is, the data must be a continuous chunk of bytes. – Must thing about how to convert data structures into a serial format


Download ppt "Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich."

Similar presentations


Ads by Google