Upload
vachel
View
21
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Sockets. Intro to Network Programming. Before the internet. Early computers were entirely isolated No Internet No network No model No external communications whatsoever. Before the internet. However, early computers did have many concepts we have already studied: File I/O Console I/O - PowerPoint PPT Presentation
Citation preview
Sockets
Intro to Network Programming
Before the internet... Early computers were entirely isolated
No Internet No network No model No external communications whatsoever
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
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().
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).
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.
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
SocketsBobAlice
client server
I am Bobport 80.
I am acceptingconnections.
Talk to Bobport 80.
I will speak toAlice port 87.
resulting TCP connection identified by(Alice:87, Bob:46)
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).
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).
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);
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);
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.
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.
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);
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.
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.
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.)
A Simple Client-Server Program
bind()
listen()
accept()
read()
write()
close()
write()
read()
connect()
socket()
socket()
close()
Client
Server
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.
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).
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.)
Server Initialization
OS
1. socket()
Web Server
2. bind(80)3. listen()
80
Listenqueue
Connecting to the Server
OS
1. socket()
Web Server
2. bind(80)3. listen()
80
Listenqueue
Client
connect()
Requestfrom (IP, port)
Busy Server Operation
OS
Web Server
80
Listenqueue
Client 1 Client 3Client 2
Client requests get queued-up in the listen queue First-come first-served
The accept() Call
OS
Web Server
80
Listenqueue
Client 1 Client 3Client 2
Client requests get queued-up in the listen queue First-come first-served
accept()Connectedsocket
A final note... You now know the calls to use
sockets, but you’ll find you need one more set of calls…
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.
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().
A final note... You’ll often find code:
ip.offset = ntohs(ip.offset);ip.length = ntohs(ip.length);ip.id = ntohs(ip.id);
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).
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. 18th @8:00am