Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights.

Similar presentations


Presentation on theme: "CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights."— Presentation transcript:

1 CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights Reserved Jack Lange University of Pittsburgh 1

2 2: Application Layer 22 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented a host-local, application-created, OS-controlled interface (a “ door ” ) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets

3 2: Application Layer 23 Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet

4 2: Application Layer 24 Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that welcomes client ’ s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client  allows server to talk with multiple clients  source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes ( “ pipe ” ) between client and server application viewpoint

5 2: Application Layer 25 Client/server socket interaction: TCP wait for incoming connection request connectionSocket = accept(listenSocket) create socket, port=x, for incoming request: listenSocket = socket() close connectionSocket close clientSocket Server (running on hostid ) Client read reply from clientSocket send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup create socket, connect to hostid, port=x clientSocket = socket() connect to hostid, port=x connect(clientSocket, dst)

6 2: Application Layer 26 Client process client TCP socket Stream jargon r A stream is a sequence of characters that flow into or out of a process. r An input stream is attached to some input source for the process, e.g., keyboard or socket. r An output stream is attached to an output source, e.g., monitor or socket. r fgets(), fputs(), getchar(), putchar()

7 2: Application Layer 27 Socket programming with TCP Example client-server app: 1) client reads line from standard input, sends to server via socket 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket

8 2: Application Layer 28 Example: C client (TCP) int main(const int argc, const char** argv) { int s, port, len, res; char buf[MAX_LINE]; struct hostent *hp; struct sockaddr_in saddr; if (argc 65535)) error processing; if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) error processing; if ((hp = gethostbyname(argv[1])) == NULL) error processing; saddr.sin_family = AF_INET; memcpy(&saddr.sin_addr.s_addr, hp->h_addr, hp->length); saddr.sin_port = htons(port); if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) error processing; create socket name lookup connect to server parameter checking

9 2: Application Layer 29 Example: C client (TCP) if (fgets(buf, sizeof(buf), stdin) == NULL) error processing; if ((res = write(s, buf, len)) <= 0) error processing; if ((res = read(s, buf, sizeof(buf)-1) <= 0) error processing; buf[res] = 0; printf(“received: %s”, buf); close(s); return 0; } Send the line to server Receive the line from server Print out the line from server Close socket Read a line from stdin

10 2: Application Layer 30 Example: C server (TCP) int main(const int argc, const char** argv) { int i, s, c, len, pos, res, port; char buf[MAX_LINE]; struct sockaddr_in saddr; if (argc 65535)) error processing; if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) error processing; memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(port); if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) error processing; if (listen(s, 32) < 0) error processing; Create socket Reserve the port on interfaces Declare it is is a server socket Create a client connection queue

11 2: Application Layer 31 Example: C server (TCP), cont while ((c = accept(s, NULL, NULL)) >= 0) { if ((len = read(c, buf, sizeof(buf) -1)) <= 0) error processing; buf[len] = 0; for (i = 0; i < len; i++) { if (islower(buf[i])) buf[i] = toupper(buf[i]); } if ((res = write(c, buf, len)) <= 0) error processing; close(c); } close(s); return(0); } Read in line from socket Accept a new client connection Write out line to socket End of while loop, loop back and wait for another client connection

12 2: Application Layer 32 Example: What ’ s not covered? r Error handling  read() == 0 ? r Socket buffer issue  A kernel-level buffer to hold the user-level content before sending/receiving  Two buffers per socket: send/receive buffers  Implication: write(s, buf, len) < len  What about read? r Feel free to reuse the code for assignment 1  Read socket programming primer linked at the course page

13 2: Application Layer 33 Socket programming with UDP UDP: no “ connection ” between client and server r no handshaking r sender explicitly attaches IP address and port of destination to each packet r server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes ( “ datagrams ” ) between client and server

14 2: Application Layer 34 Client/server socket interaction: UDP Server (running on hostid ) close clientSocket read datagram from clientSocket create socket, clientSocket = DatagramSocket() Client Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x. serverSocket = DatagramSocket() read datagram from serverSocket write reply to serverSocket specifying client address, port number

15 2: Application Layer 35 Example: C client (UDP) Output: sends packet (recall that TCP sent “ byte stream ” ) Input: receives packet (recall thatTCP received “ byte stream ” ) Client process client UDP socket

16 2: Application Layer 36 Example: C client (UDP) int main(const int argc, const char** argv) { int s, port, len, res, fromlen; char buf[MAX_LINE]; struct hostent *hp; struct sockaddr_in saddr, raddr; if (argc 65535)) error processing; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) error processing; if ((hp = gethostbyname(argv[1])) == NULL) error processing; memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; memcpy(&saddr.sin_addr.s_addr, hp->h_addr, hp->h_length); saddr.sin_port = htons(port); Create client socket Translate hostname to IP address using DNS

17 2: Application Layer 37 Example: C client (UDP), cont. if (fgets(buf, sizeof(buf), stdin) == NULL) error processing; len = strlen(buf); if ((res = sendto(s, buf, len, 0, (struct sockaddr *)&saddr, sizeof(saddr))) < 0) error processing; fromlen = sizeof(raddr); if ((res = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&raddr, &fromlen) < 0) error processing; buf[res] = 0; printf(“received: %s”, buf); close(s); return 0; } Create datagram with data-to-send, length, IP addr, port Send datagram to server Read datagram from server

18 2: Application Layer 38 Example: C server (UDP) int main(const int argc, const char** argv) { int i, s, len, res, port, fromlen; char buf[MAX_LINE]; struct sockaddr_in saddr, claddr; if (argc 65535)) error processing; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) error processing; memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(port); if (bind(s, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) error processing; Create a socket Reserve a port to receive datagram

19 2: Application Layer 39 Example: C server (UDP), cont while (1) { fromlen = sizeof(claddr); if ((len = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&claddr, &fromlen)) <= 0) error processing; buf[len] = 0; for (i = 0; i < len; i++) { if (islower(buf[i])) buf[i] = toupper(buf[i]); } if ((res = sendto(s, buf, len, 0, (struct sockaddr *)&claddr, fromlen)) <= 0) error processing; } close(s); return(0); } Get IP addr port #, of sender End of while loop, loop back and wait for another datagram Create and send datagram to client Receive datagram

20 2: Application Layer 40 Summary r Application architectures  client-server  P2P  Hybrid r Internet transport service model  TCP: connection-oriented, reliable  UDP: unreliable, datagrams r Socket programming  socket(), read()/send(), write()/recv(), sendto(), recvfrom(), close()


Download ppt "CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights."

Similar presentations


Ads by Google