Chapter 14 Application Layer and Client-Server Model.
Post on 16-Dec-2015
241 Views
Preview:
Transcript
ConcurrenciaConcurrencia
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.
Sockets used for datagramsSockets used for datagrams
ServerAddress and ClientAddress are socket addresses
Sending a message Receiving a message
bind(s, ClientAddress)
sendto(s, "message", ServerAddress)
bind(s, ServerAddress)
amount = recvfrom(s, buffer, from)
s = socket(AF_INET, SOCK_DGRAM, 0)s = socket(AF_INET, SOCK_DGRAM, 0)
Sockets used for streamsSockets used for streams
Requesting a connection Listening and accepting a connection
bind(s, ServerAddress);listen(s,5);
sNew = accept(s, ClientAddress);
n = read(sNew, buffer, amount)
s = socket(AF_INET, SOCK_STREAM,0)
connect(s, ServerAddress)
write(s, "message", length)
s = socket(AF_INET, SOCK_STREAM,0)
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 2000void 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);}
top related