420 likes | 483 Views
Chapter 14. Application Layer and Client-Server Model. Figure 14-1. Client-Server Model. Figure 14-3. Concurrencia. Type of Running in Clientes Iteratively: one-by-one Concurrently: at the same time Concurrency in Services:
E N D
Chapter 14 Application Layer and Client-Server Model
Concurrencia • Type of Running in Clientes • Iteratively: one-by-one • Concurrently: at the same time • Concurrency in Services: • Conectionless Iterative Server: from the same cliente of from different clients (i.e. UDP) • Conection-Oriented Concurrent Server: serves many clients at the same time.
Procesos • Concepto • Identificación • Creación
Chapter 24 SocketInterface
Sending a message Receiving a message s = socket(AF_INET, SOCK_DGRAM, 0) s = socket(AF_INET, SOCK_DGRAM, 0) bind(s, ClientAddress) bind(s, ServerAddress) sendto(s, "message", ServerAddress) amount = recvfrom(s, buffer, from) ServerAddress and ClientAddress are socket addresses Sockets used for datagrams
Sockets used for streams Requesting a connection Listening and accepting a connection s = socket(AF_INET, SOCK_STREAM,0) s = socket(AF_INET, SOCK_STREAM,0) bind(s, ServerAddress); listen(s,5); connect(s, ServerAddress) sNew = accept(s, ClientAddress); write(s, "message", length) n = read(sNew, buffer, amount) ServerAddress and ClientAddress are socket addresses
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #define MAXBUF 256 #define PORT 2000 void main(void) { char buf[MAXBUF]; int activeSocket; int remoteAddrLen; struct sockaddr_in remoteAddr; struct sockaddr_in localAddr; struct hostent *hptr;
activeSocket = socket(AF_INET, SOCK_DGRAM, 0); memset(&remoteAddr, 0,sizeof(remoteAddr)); remoteAddr.sin_family =AF_INET; remoteAddr.sin_port=htons(PORT); hptr=gethostbyname("a-domain-name"); memcpy((char*)&remoteAddr.sin_addr.s_addr,hptr->h_addr_list[0],hptr->h_length); connect(activeSocket, &remoteAddr, sizeof(remoteAddr)); memset(buf, 0, MAXBUF); remoteAddrLen = sizeof(remoteAddr); while { sendto(activeSocket, buf, sizeof(buf), 0, &remoteAddr,sizeof(remoteAddr$ memset(buf, 0, sizof(buf)); recvfrom(activeSocket, buf, MAXBUF, 0, &remoteAddr,&remoteAddrLen); printf("%s\n", buf); memset(buf, 0, sizeof(buf)); }; close(activeSocket); }