DOC PREVIEW
U of I CS 241 - More network programming

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

More network programmingCS 241 Discussion Section Week 124/23/07 – 4/29/07Stephen KloderOutline• DNS•TCP•UDP• HTTPDNSThe Domain Name System (DNS) converts between name addresses (e.g. www.gnu.org) and number addresses (e.g. 199.232.41.10)IP uses the numbers for routing messages, but names are easier to remember.DNS example: nslookupUse nslookup to use DNS from the shell.% nslookup csil-core21.cs.uiuc.eduServer: 128.174.252.4Address: 128.174.252.4#53Name: csil-core21.cs.uiuc.eduAddress: 128.174.242.111DNS example: gethostbynameTo use DNS inside your programs, call gethostbyname.struct hostent *gethostbyname(const char *name);Similarly, use gethostbyaddress to get DNS information about a numerical address.struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);gethostbyname cont’dTo use an address for socket code,#include <netdb.h>…struct hostent *h;h = gethostbyname(host);memcpy((char *) &sa.sin_addr.s_addr, h->h_addr, h->h_length);Transport-level protocolsThere are two major protocols for sending data over the internet, that work on top of Internet Protocol (IP).Transmission Control Protocol (TCP)Sends data as a stream of bytesUser Datagram Protocol (UDP)Sends data as a set of packetsSockets code chooses its protocol through the typevalue in the socket call.SOCK_STREAM is TCP, SOCK_DGRAM is UDPTCPTCP is stream-based. It takes a stream of data, breaks it up into packets, and sends the packets to the destination.Both sender and receiver perform routines to make sure every packet arrives, in order.Calling send with TCP does not send data, but writes to the stream, which will be sent later.UDPUDP is packet-based. Every call to send sends a packet containing the inputted data.UDP contains no provisions for flow control, session management, or packet ordering.TCP vs. UDP at a glanceTCP UDPSocket type SOCK_STREAM SOCK_DGRAMForm of data transmitted Stream PacketsCalls for sending and receiving send, recv sendto, recvfromUses sessions? Yes NoOverhead for ordering packets Substantial MinimalExample Services FTP, HTTP DNS, SNMPExample 1: simple chatThis is a program to allow two users to send messages back and forth, on the same host or different hosts.This program uses a client (chat-client1.c) and a server (chat-server.c).Fill in the skeleton code, which uses TCP.Example 2: Chat with UDPConvert chat-client1.c and chat-server1.c into chat-client2.c and chat-server2.c, which use UDP instead of TCP.–SOCK_DGRAM instead of SOCK_STREAM–No accept, listen, or connect– sendto instead of send– recvfrom instead of recvsend and sendtoint send(int socket, const void *msg, int len, int flags);int sendto(int socket, const void *msg, int len, int flags, const struct sockaddr *to, socklen_t tolen);send sends along an established connection (TCP), while sendto sends to an address (UDP).The extra two parameters specify the destination.recv and recvfromint recv(int socket, const void *msg, int len, int flags);int recvfrom(int socket, const void *msg, int len, int flags, const struct sockaddr *from, socklen_t *fromlen);recv receives from an established connection (TCP), while recvfrom receives from anywhere (UDP), and saves the address.The extra two parameters specify the source.Stream vs. Packets:an exampleWith TCP, data can be sent with different breakdown than it is received, For example, we can replacesend(sock, buffer, size, 0);withfor(i=0;i<size;i++)send(sock, buffer+i,1,0);and it will be received the same way.Try this. What happens if you try it with the UDP code?UDP and packetsSince UDP is packet-based, each packet is treated separately.Therefore, if you send 100 packets with UDP, the other host will receive 100 packets.With TCP, any number of packets could be sent depending on the state of the network.Example 3: Chat with threadsThese chat programs assume alternating send/receive communication. Thus the output is counterintuitive if the users deviate from this.Therefore, chat-client3.c and chat-server.c use threads to send and receive at the same time.Fill in the skeletons.HTTP: Parsing the URLEach URL has three components:http://www.linux.org/info/faq1.htmlProtocol Host PathHTTP: Forming a requestThe HTTP client connects to the host, and sends a request in the protocol which contains the path.In our example, the client would connect to www.linux.org port 80, and send the request “GET /info/faq1.html HTTP/1.0”Example 4: basic HTTP clientweb.c is a skeleton for a program that given a URL, contacts the web server, requests the file, and saves the results.Fill in the details.HTTP clients• HTTP clients (e.g. web browser) do more than the example:– Error Handling– HTTP fields– associated images– CGI– Etc.Summary• Use DNS to get an address from a name.•Use TCP for longer, controlled data streams.•Use UDP for shorter, packet-based communications.•HTTP uses


View Full Document

U of I CS 241 - More network programming

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download More network programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More network programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More network programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?