What are Sockets?
Socket Client-Server Model
Socket Attributes
Sockets are characterized by three attributes
– Domain
– Type
– Protocol
Socket Protocol
– Determined by socket type and domain
– Default protocol is 0
types of sockets
Three main types of sockets:
– SOCK_STREAM (TCP sockets)
* Provides a connection-oriented, sequenced, reliable, and bidirectional network communication service
* E.g., telnet, ssh, HTTP
– SOCK_DGRAM (UDP sockets)
* Provides a connectionless, unreliable, best-effort network communication service
* E.g., streaming audio/video, IP telephony
– SOCK_RAW
* Allows direct access to other layer protocols such as IP, ICMP, or IGMP
UNIX Domain Sockets
include <sys/un.h>
struct sockaddr_un
{
sa_family_t sun_family; /* AF_UNIX /
char sun_path[108]; / pathname */
};
Stream Socket
Steps in Server Process
Steps in Client Process
Creating the Socket
include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Bind to a Name (Server Side)
include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *serveraddr, socklen_t addrlen);
int listen (int sockfd, int backlog) (Server Side)
include <sys/types.h>
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Establish Connection (Server Side)
include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr* cliaddr, socklen_t * addrlen);
Establish Connection (Client Side)
include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr* servaddr, socklen_t * addrlen);
Sending Data
include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
Receiving Data
include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
Close and Shutdown
include <unistd.h></unistd.h>
int close(sockfd);
* Closes the file descriptor when done
* Will not deallocate the socket until we close the last descriptor that references it (we may have several)
int shutdown(int sockfd, int how);
* Forces a full or partial closure of a socket
* sockfd is a socket descriptor
* how can be SHUT_RD, SHUT_WR, or SHUT_RDWR
Socket Pipe
socketpair() Function
include <sys/socket.h>
int socketpair(int family, int type, int protocol, int sockfd[2]);
Layers of the IP Protocol Suite
application layer -> ftp
transport layer -> TCP, UDP
network layer -> IP
link layer -> ethernet
Internet Sockets
What is the Internet?
Physical layer
Actual electrons on a wire
Datalink layer
Frames, MAC Addresses
Network layer
Router to router, Creates network IP addresses, Variable-length packets
Transport layer
Persistent communication channels, TCP, UDP, ports
Session layer
Open, close, manage sessions; AppleTalk, SCP
Presentation layer
String encoding,encryption /decryption
Object serialization, files, compression
Application layer
HTTP POST and GET requests
Byte Ordering of Integers
Byte Ordering
Byte Ordering Problem
What would happen if two computers with different integer byte ordering communicate?
– Nothing… if they do not exchange integers!
– But… if they exchange integers, they would get the wrong order of bytes, and therefore, the wrong value!