Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.

Similar presentations


Presentation on theme: "Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications."— Presentation transcript:

1 Sockets Intro to Network Programming

2 Before the internet... Early computers were entirely isolated No Internet No network No model No external communications whatsoever

3 Before the internet... However, early computers did have many concepts we have already studied: File I/O Console I/O Shared memory Inner-process communication Solution: extend existing concepts to allow for remote communication

4 Before the internet... Solution: sockets A socket is nothing more than an end-point of a bidirectional inner-process communication flow. A socket, in a Linux operation system, is a file descriptor – the exact same thing you get form a call to open().

5 Sockets Sockets are often described as a door, gateway, or portal. The sender sends information through the portal. The receiver receives the information. To a programmer, the mechanism to get the information from the sender to receiver is abstracted away (TCP/IP, UDP/IP, etc).

6 Sockets Potential Problem: Many programs could possibly be communicating externally at once. Web browser loading a page E-mail client checking e-mail Streaming radio/music in the background …and a download going on.

7 Sockets Solution: UDP and TCP both provide ports. A port is a 16-bit number to describe which program on a computer the information is intended to be received by. Example: By default, web servers always listen for connections form clients on port 80. Written as: cs.illinois.edu:80

8 Sockets Bob Alice clientserver I am Bob port 80. I am accepting connections. Talk to Bob port 80. I will speak to Alice port 87. resulting TCP connection identified by (Alice:87, Bob:46)

9 Programming with sockets… To create a socket in C, you make a call to socket(). Definition: int socket(int domain, int type, int protocol); Alternatively, accept() will return a socket file descriptor (we’ll see this later).

10 Programming with sockets… Definition: int socket(int domain, int type, int protocol); domain: Specifies a communication domain (eg: IPv4 v. IPv6). type: Specifies what mechanism of communication is used (eg: stream v. datagram). protocol: Specifies what protocol should be used (eg: TCP v. UDP).

11 Programming with sockets… If you wish to allow for others to connect to your socket, you will need to bind() it to a port on the operating system. int bind(int socket, const struct sockaddr *address, socklen_t address_len);

12 Programming with sockets… Now that you’ve created your socket and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog);

13 Programming with sockets… Now that you’ve created your socket and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog); backlog: defines the maximum length for the queue of pending connections. For most programs that will be constantly accept()’ing connections, a smallish value (5 or so) works great.

14 Programming with sockets… When you begin to listen() on your socket: …you’ve told the operating system to allow remote systems to connect at the specified port using the specified protocol. …you haven’t had a way to actually get a socket to communicate with that remote system.

15 Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

16 Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); addr: The address of the system connect()ing to your system is written to the struct you pass in.

17 Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); Return value: A new socket file descriptor to communicate with the remote system. Your original socket is still listening and more connections can be accept()’d.

18 Programming with sockets… If you’ve created a socket that you want to connect() rather than listen(), you simply create the socket and connect(). int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); (If you’re connect()’ing, you won’t want to listen() or bind() on that socket.)

19 A Simple Client-Server Program bind() listen() accept() read() write() close() write() read() connect() socket() close() Client Server

20 A Simple Client-Server Program In the diagram: read() write() close() …you’ll notice you’ve seen these calls before. These file I/O calls are socket I/O calls, too.

21 Programming with sockets… There are a few calls that are socket specific… ssize_t send(int socket, const void *buffer, size_t length, int flags); flags: Provides access to socket-specific parameters. A call to write() is equivalent to send() with no flags (eg: flags == 0).

22 Programming with sockets… For completeness, there are a whole set of send()/recv() calls: send(), sendto(), sendmsg() recv(), recvfrom(), recvmsg() (In CS 241, we won’t need any of the flag parameters.)

23 Server Initialization OS 1. socket() Web Server 2. bind(80) 3. listen() 80 Listen queue

24 Connecting to the Server OS 1. socket() Web Server 2. bind(80) 3. listen() 80 Listen queue Client connect() Request from (IP, port)

25 Busy Server Operation OS Web Server 80 Listen queue Client 1Client 3Client 2 Client requests get queued-up in the listen queue First-come first- served

26 The accept() Call OS Web Server 80 Listen queue Client 1Client 3Client 2 Client requests get queued-up in the listen queue First-come first- served accept() Connected socket

27 A final note... You now know the calls to use sockets, but you’ll find you need one more set of calls…

28 A final note... Various different architectures store 16- bit and 32-bit numbers differently (big- endian v. little-endian). When configuring port numbers and IP addresses, we need to talk in an architecture-independent way.

29 A final note... In C, functions are provided to convert between your local architecture and network architecture. htonl(): Converts a long from host to network byte order. htons(): Converts a short from host to network byte order. …and the inverse: ntohl() and ntohs().

30 A final note... You’ll often find code: ip.offset = ntohs(ip.offset); ip.length = ntohs(ip.length); ip.id = ntohs(ip.id);

31 Speaking of code... We’ll take a look at actual C code to run a client/server in section tomorrow. Tomorrow will be the very last section of CS 241 (next Thursday is reading week).

32 CS 241 At this point, we’ve covered every topic in CS 241! Congratulations! Remember: Prof. Caccamo will be back on Monday HW #2: Due IN-CLASS @11:00am Monday Solutions will be discussed during Monday’s lecture MP #7: Due on SVN on Wednesday Final Exam: Dec. 18 th @8:00am


Download ppt "Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications."

Similar presentations


Ads by Google