client-server model and socket programmingmqhuang/courses/3613/s2019/... · client-server model...
TRANSCRIPT
Client-Server ModelSocket Programming
Client-Server Modeland Socket Programming
Miaoqing HuangUniversity of Arkansas
1 / 27
Client-Server ModelSocket Programming
Client-Server Model
A distributed application structurePartition tasks between the providers of a resource or service,called servers, and service requesters, called clients
One of the central ideas of network computingExamples: email exchange, web access, and database access
Server
Client Client
Request
Reply
4 / 27
Client-Server ModelSocket Programming
Asymmetric Relationship
SpecializationClients specialize in user interfaceServers specialize in managing data and application logic
SharingMany clients can be supported by few serversClient predominately makes requests, server makes replies
5 / 27
Client-Server ModelSocket Programming
Email Example
ClientServer
Client
Email client sends message to server
Message is stored on POP server
Later, recipient’s email client retrieves message from server
6 / 27
Client-Server ModelSocket Programming
Chat Room Example
Client
Server
Client
Chat clients send user’s typing to server
Chat server receives typing from all users and sends to all clients
Other user’s clients display aggregated typing from chat server
7 / 27
Client-Server ModelSocket Programming
Socket vs. Telephone
Socket (aka, Internet Socket)An endpoint of a bidirectional inter-process communication flowacross an Internet Protocol-based computer networkA socket address consists of
An IP address: the location of the computerA port: mapped to the application program process
Communication protocol: TCP or UDP
TelephoneAn endpoint of a bidirectional inter-person communication flowacross a telephone networkAn address of a telephone consists of
A telephone number(Possibly) an extension
Mobile phone standard: GSM or CDMA
10 / 27
Client-Server ModelSocket Programming
IP Address and Port Number
Internet Protocol addressA numerical label assigned to each device (e.g., computer,printer) participating in a computer network that uses the InternetProtocol for communicationServe two principal functions
Host or network interface identificationLocation addressing
Current IP version: IPv4Port number
A port is an application-specific or process-specific softwareconstruct serving as a communications endpointA specific port is identified by its number, i.e., the port numberA port number is a 16-bit unsigned integer, [0, 65535]
Well-known ports: 0-1,023Registered ports: 1,024-49,151Dynamic, private or ephemeral ports: 49,152-65,535
11 / 27
Client-Server ModelSocket Programming
Two Types of Sockets
Stream socket, aka, connection-oriented socketEstablish a connection before transferring data
Reliable, in-order
Use Transmission Control Protocol (TCP)Datagram socket, aka, connectionless socket
Each packet sent or received on a Datagram socket is individuallyaddressed and routed
May arrive in any orderMay get lost
Use User Datagram Protocol (UDP)
TCP UDP
13 / 27
Client-Server ModelSocket Programming
Inter-process Communication using Datagram Socket (UDP)
agreed port
any port
sendingprocess receiving
process
computercomputer
s1 s2message
Client1 Create a socket (s1)2 Bind the socket to any available
port of the local computerThis step may be skippedwhen programming in C
3 Send message to a knownsocket address (s2)
Receive message from s1
Server1 Create a socket (s2)2 Bind the socket to an
agreed socket address3 Receive message from
the socket (s2)Send message to s1
14 / 27
Client-Server ModelSocket Programming
Inter-process Communication using Stream Socket (TCP)
agred port
any port
clientprocess
serverprocess
computer
s1
s2
s3connection
Client1 Create a socket (s1)2 Send a connection request
to a known socket address(s2)
3 Start writing and readingthrough the connectionbetween s1 and s3
Server1 Create a socket (s2)2 Bind the socket to an
agreed socket address3 Listen to and accept the
connection request from s2Create a new socket s3for the connection to s1
4 Start reading and writing15 / 27
Client-Server ModelSocket Programming
System Calls to Send/Receive Datagrams (in C language)
Following system calls are used:socket: to create a socket and get a file descriptor for itbind: to bind a socket address to the file descriptor of a socketsendto: send a message through a bound socket to a socketaddressrecvfrom: to receive a message through a socketclose: to destroy the socket when it is no longer needed
16 / 27
Client-Server ModelSocket Programming
System Calls for Stream Communication (in C language)
Following system calls are used:socket: to create a socket and get a file descriptor for itbind: to bind a socket address to the file descriptor of a socketconnect: to make a connect requestlisten: to specify how many requests should be queuedaccept: to accept a connect requestwrite: send information via connected socketsread: receive information via connected socketsclose: to destroy the socket when it is no longer needed
17 / 27
Client-Server ModelSocket Programming
Create a Socket
int socket (int domain, int type, int protocol); // prototype
int s;if(( s = socket(AF_INET, SOCK_DGRAM, 0))<0) {
perror("socket failed");return;
}
domainMust be AF_INET
typeSOCK_DGRAM: Datagram socketSOCK_STREAM: Stream socket
protocolSpecify the protocol0: system to select a suitable protocol
18 / 27
Client-Server ModelSocket Programming
Bind a Socket
int bind (int s, struct sockaddr * socketAddress, int addrlength);
struct sockaddr_in {short sin_family; // Must be AF_INETu_short sin_port; // Port numberstruct in_addr sin_addr; // IP address; INADDR_ANY: local IPchar sin_zero[8];
}
sSocket descriptor
socketAddressSpecify the socket addressUse struct sockaddr_in
addrlengthThe size of the structure in the second argument
Server has to bind the socket to an known socket address
19 / 27
Client-Server ModelSocket Programming
Close a Socket
int close (int s);
sSocket descriptor
A socket has to be closed so that it can be re-used by anotherprogram
20 / 27
Client-Server ModelSocket Programming
Send a Message using a Datagram Socket
int sendto(int s, char * msg, int len, int flags, struct sockaddr *to, int tolen);
sSocket descriptor
msgPointer to the message string
lenLength of the message
flagsNormally zero
toDestination Socket
tolenSize of the structure sockaddr
21 / 27
Client-Server ModelSocket Programming
Receive a Message using a Datagram Socket
int recvfrom(int s, char *buf, int len, int flags, struct sockaddr * from, int *fromlen);
sSocket descriptor
buf and lenThe buffer to save the received message
flagsNormally zero
from and fromlenThe structure to save the sender’s socket address
recvfrom is a blocking call
22 / 27
Client-Server ModelSocket Programming
Listen to a Socket and Accept a Connection RequestServer functions of a Stream Socket
int listen(int s, int backlog);
Listen on its socket for requests from clients for connectionsbacklog: the number of requests for connection that can bequeued at that socket
int accept(int s, struct sockaddr * clientAddress,int * clientLength);
Accepts the first connection in the queue at socket sThe result is a descriptor for a new socket that has been createdfor use as one end of the stream
How to deal with multiple connections simultaneously?Spawn a thread for each connection
23 / 27
Client-Server ModelSocket Programming
Listen to a Socket and Accept a Connection RequestServer functions of a Stream Socket
int listen(int s, int backlog);
Listen on its socket for requests from clients for connectionsbacklog: the number of requests for connection that can bequeued at that socket
int accept(int s, struct sockaddr * clientAddress,int * clientLength);
Accepts the first connection in the queue at socket sThe result is a descriptor for a new socket that has been createdfor use as one end of the stream
How to deal with multiple connections simultaneously?
Spawn a thread for each connection
24 / 27
Client-Server ModelSocket Programming
Listen to a Socket and Accept a Connection RequestServer functions of a Stream Socket
int listen(int s, int backlog);
Listen on its socket for requests from clients for connectionsbacklog: the number of requests for connection that can bequeued at that socket
int accept(int s, struct sockaddr * clientAddress,int * clientLength);
Accepts the first connection in the queue at socket sThe result is a descriptor for a new socket that has been createdfor use as one end of the stream
How to deal with multiple connections simultaneously?Spawn a thread for each connection
25 / 27
Client-Server ModelSocket Programming
Request a Connection by a Client Process
int connect(int s, struct sockaddr *server, int addrLen);
Request a connection via the socket address of the listeningprocess
26 / 27