DOC PREVIEW
U of I CS 241 - Socket Programming

This preview shows page 1-2-3-4-5-6 out of 19 pages.

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

Unformatted text preview:

Socket ProgrammingCS241 AdministrativeConnection-oriented Communication ProtocolConnection-Oriented Communications IllustrationMultiple Clients (Serial-server Strategy)Parent-server StrategyThreaded-server StrategyWhat is a socket?Two essential types of socketsSocket-Based APIsSocket Creation in C: socketAddresses, Ports and SocketsBind FunctionSocket Structure (1)Socket Structure (2)Socket Structure: ExampleSending / Receiving Data - UDPTypical UDP Server-ClientSummaryCS 241 Spring 2007System Programming 1Socket ProgrammingLecture 35Klara Nahrstedt2CS241 AdministrativeRead Stallings Chapter 13, R&R 18.1-18.3LMP3 (Part I) Due April 23LMP3 (Part II) Due April 30Last Quiz will be on Friday, April 27 on Networking3Connection-oriented Communication Protocol Server monitors a passive end-point whose address is known to clientsListening (passive) endpoints have resources for queuing client connection requests and establishing client connectionsAction of accepting a client request creates a new endpoint for private, two-way symmetric communication with that clientClient and server communicate by using handles (file descriptors) and do not explicitly include addresses in their messagesWhen finished, client and server close their file descriptors, system releases resources associated with the connection4Connection-Oriented CommunicationsIllustration5Multiple Clients (Serial-server Strategy)6Parent-server Strategy7 Threaded-server Strategythread8What is a socket?An interface between application and networkThe application creates a socketThe socket type dictates the style of communicationreliable vs. best effortconnection-oriented vs. connectionlessOnce configured, the application canpass data to the socket for network transmissionreceive data from the socket (transmitted through the network by some other host)9Two essential types of sockets•SOCK_STREAM–a.k.a. TCP–reliable delivery–in-order guaranteed–connection-oriented–bidirectional•SOCK_DGRAM–a.k.a. UDP–unreliable delivery–no order guarantees–no notion of “connection” – app indicates dest. for each packet–can send or receiveAppsocket321Dest.Appsocket321D1D3D210Socket-Based APIs #include <sys/socket.h> int socket (int domain, int type, int protocol); int bind (int s, const struct sockaddr *address, size_t address_len); int listen (int s, int backlog); int accept (int s, struct sockaddr *restrict address, int *restrict address_len);11Socket Creation in C: socketint s = socket(domain, type, protocol);s: socket descriptor, an integer (like a file-handle)domain: integer, communication domaine.g., AF_INET typically used (Internet domain)type: communication typeSOCK_STREAM: reliable, 2-way, connection-based serviceSOCK_DGRAM: unreliable, connectionless,other values: need root permission, rarely used, or obsoleteprotocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0 (IP)NOTE: socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface!12Addresses, Ports and SocketsLike apartments and mailboxesYou are the applicationYour apartment building address is the addressYour mailbox is the portThe post-office is the networkThe socket is the key that gives you access to the right mailboxQ: How do you choose which port a socket connects to?13Bind Function int bind(int s, const struct sockaddr *address, size_t address_len); Bind function Associates the handle for a socket communication endpoint with a specific logical network connection. Note: Internet domain protocols specify logical connection by a port number s is the file descriptor returned by socket( ) address contains info about the family, port and machine address_len is the size of the structure used for the address14Socket Structure (1)The format of the address struct sockaddr is determined by the address family (domain). For AF_INET it is a struct sockaddr_in Socket structure is defined in netinet/in.h Socket structure has at least the following members in network byte order:sa_family_t sin_family; in_port_t sin_port; /* Host Port Number */struct in_addr sin_addr; /* IP Address*/15Socket Structure (2)The sin_family should be AF_INET. The sin_port should be the port number in network byte order. You can convert a port number to network byte order using: uint16_t htons(uint16_t port); For a server, the sin_addr can be set to INADDR_ANY to accept connections on any interface card. For a client, it must be filled in with the IP address of the remote machine in network byte order.sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr;16Socket Structure: Examplestruct sockaddr_in server;int sock;server.sin_family = AF_INET;server.sin_addr.s_addr = htonl(INADDR_ANY);server.sin_port = htons((short)8652);if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1)perror(“Failed to bind the socket to port”);17Sending / Receiving Data - UDPWithout a connection (SOCK_DGRAM):int count = sendto(sock, &buf, len, flags, &addr, addrlen);count, sock, buf, len, flags: same as sendaddr: struct sockaddr, address of the destinationaddrlen: sizeof(addr)int count = recvfrom(sock, &buf, len, flags, &addr, &addrlen);count, sock, buf, len, flags: same as recvname: struct sockaddr, address of the sourcenamelen: sizeof(name): value/result parameterCalls are blocking [returns only after data is sent (to socket buf) / received]18Typical UDP Server-Client19SummaryClient-Server Process Communication Connection-oriented Server StrategiesImplementation of


View Full Document

U of I CS 241 - Socket 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 Socket 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 Socket 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 Socket 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?