DOC PREVIEW
U of I CS 241 - Networking and Sockets

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 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 28 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 28 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 28 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 28 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 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Networking and SocketsCS241 Discussion Section Week 114/16/07 – 4/22/07OutlineBrief intro to networkingHTTP ExampleSocket ProgrammingLibrary FunctionsTCP Client and Server ExamplesNetworkingAllows computers and other networked devices to talk to each otherHow can we tell what the packet we just received means?Interactions between applications on different machines are governed by protocolsProtocols dictate the format and sequence of the information exchangeNames and AddressesA network address identifies a specific computer on the networkSeveral kinds of addresses exist: MAC address (for LANs), IP address (for the Internet), etc.Domain or DNS names are used for convenience, so we don’t have to remember numerical addressesPortsPorts are numbers that represent an end-point for communicationPorts are logical, not physical, entitiesAll packets arrive to the same physical interface, and are then differentiated based on the port number (and other contents of the packet header)Usually, distinct ports are used for communication via different protocolsE.g., port 80 is for HTTP, 22 is for SSH, etc.See /etc/services for a listExample: HTTPHow the Web really worksHTTPHypertext Transfer ProtocolDelivers virtually all files and resources on the World Wide WebUses Client-Server ModelHTTP transactionClient opens a connection and sends a request message to the serverServer returns a response messageHTTP (continued)RequestGET /path/to/file.html HTTP/1.0Other request methods possible (POST, HEAD)ResponseHTTP/1.0 200 OKCommon Status Codes200 OK404 Not Found500 Server ErrorSample HTTP exchangeScenarioClient wants to retrieve the document at the following URL: http://www.cs.uiuc.edu/What the client doesClient opens a connection to the host www.cs.uiuc.edu, port 80telnet www.cs.uiuc.edu 80Client sends the following message, requesting document “/”GET / HTTP/1.0From: [email protected]: MyBrowser[other parameters…][blank line here]Sample HTTP exchangeWhat the server doesServer responds with:HTTP/1.0 200 OKDate: Mon, 16 Apr 2007 23:59:59 GMTContent-Type: text/htmlContent-Length: 1354<html><body>SocketsStandard API for sending and receiving data across computer networksCan also be used for interprocess communication on a single machineIntroduced by BSD operating systems in 1983POSIX incorporated 4.3BSD sockets in 2001Using Sockets in C#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <unistd.h>On csil-core:gcc –o test test.cOn some systems, e.g., Solaris:gcc –o test test.c –lsocket -lnslTCP Client/Server ExampleRun the provided test-server and test-client executablestest-client sends the string “Hello World!” to IP address 127.0.0.1 port 10000test-server listens on port 10000 and prints out any text receivedLet’s try to replicate this behaviorGeneric TCP Client & Server ScriptClientsocket()connect()while (…) {send()/recv()}close()Serversocket()bind()listen()while (…) {accept()send()/recv()}close()Let’s take it one step at a time, starting with the client…socketint socket(int domain, int type, int protocol);Creates a communication endpointLocal action only; no communication takes placeParametersdomain: AF_INET (IPv4)type: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)protocol: 0 (socket chooses the correct protocol based on type)Returns a nonnegative integer corresponding to a socket file descriptor if successful, -1 with errno set if unsuccessfulconnectint connect(int socket, const struct sockaddr*address, socklen_t address_len);Establishes a link to a well-known port of the remote serverInitiates the TCP 3-way handshake (exchange packets with the remote machine to mutually set up a connection)Returns 0 if successful, -1 with errno set if unsuccessfulstruct sockaddrstruct sockaddr_in used for addresssa_family_t sin_family; /* AF_INET */in_port_t sinport; /* port number */struct in_addr sin_addr; /* IP address */For example:sa.sin_family = AF_INET;sa.sin_port = htons(80);sa.sin_addr = inet_addr(“127.0.0.1”);send 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, socklet_t tolen);sends data pointed by msgsendto is used for unconnected datagram sockets. If used in connection-mode, last two parameters are ignored.Returns the number of bytes actually sent out if successful, -1 with errno set if unsuccessfulclose and shutdownint close(int socket);int shutdown(int socket, int how);closePrevents any more reads and writessame function as for file systemsshutdownprovides a little more controlhow0 – Further receives are disallowed1 – Further sends are disallowed2 – same as closeReturns 0 if successful, -1 with errno set if unsuccessfulWhat about the server?First call socket() as with the client. Then…bindint bind(int socket, const structsockaddr *address, socklen_taddress_len);Associates the socket with a port on your local machineLocal action; no communication takes place yetReturns 0 if successful, -1 with errno set if unsuccessfulCaution!Exiting or crashing after calling bind() but before close() willcause problems!Cannot bind() the same port for a few minutesPrevent a different application from accidentally receiving our connectionsUnless SO_REUSEADDR option is used with setsockopt() Permits the server to be restarted immediatelysetsockoptint sock;int true = 1;if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)exit(EXIT_FAILURE);if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true,sizeof(true)) == -1) {error = errno;while ((close(sock) == -1) && (errno == EINTR));errno = error;exit(EXIT_FAILURE);}listenint listen(int socket, int backlog);Puts the socket into the passive state to accept incoming requestsInternally, it causes the network infrastructure to allocate queues to hold pending requestsbacklog: number of connections allowed on the incoming queuebind() should have been called beforehandReturns 0 if successful, -1 with errno set if unsuccessfulacceptint accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);Accepts the pending connection requests into the incoming queue*address is used to return the information about the client making the connection. sin_addr.s_addr holds the Internet addresslisten() should have been called beforehandReturns nonnegative file descriptor corresponding to the accepted socket if successful, -1 with errno set if unsuccessfulrecv and recvfromint recv(int socket, void *buf,


View Full Document

U of I CS 241 - Networking and Sockets

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 Networking and Sockets
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 Networking and Sockets 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 Networking and Sockets 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?