Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1652 Jack Lange University of Pittsburgh

Similar presentations


Presentation on theme: "CS 1652 Jack Lange University of Pittsburgh"— Presentation transcript:

1 CS 1652 Jack Lange University of Pittsburgh
The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights Reserved 1

2 Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm 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 2: Application Layer 22

3 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 Internet controlled by OS controlled by app developer transport application physical link network process socket 2: Application Layer 23

4 Socket programming with TCP
Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP 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 2: Application Layer 24

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

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

7 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 2: Application Layer 27

8 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 < 3 || ((port = atoi(argv[2])) <= 0 || port > 65535)){ // error processing; } if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { if ((hp = gethostbyname(argv[1])) == NULL) { 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) { parameter checking create socket name lookup connect to server 2: Application Layer 28

9 Example: C client (TCP)
Read a line from stdin if (fgets(buf, sizeof(buf), stdin) == NULL) { // error processing; } if ((res = write(s, buf, len)) <= 0) { if ((res = read(s, buf, sizeof(buf)-1) <= 0) { 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 2: Application Layer 29

10 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 < 2 || ((port = atoi(argv[1])) <= 0 || port > 65535)) { // error processing; } if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { 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) { if (listen(s, 32) < 0) { Create socket Reserve the port on interfaces Set as a server socket Create a client connection queue 2: Application Layer 30

11 Example: C server (TCP), cont
Accept a new client connection 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) { close(c); close(s); return(0); Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection 2: Application Layer 31

12 Example: What’s not covered?
Error handling read() == 0 ? 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? Feel free to reuse the code for assignment 1 Read socket programming primer linked at the course page 2: Application Layer 32

13 Socket programming with UDP
UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet 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 2: Application Layer 33

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

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

16 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 < 3 || ((port = atoi(argv[2])) <= 0 || port > 65535)) { // error processing; } if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { // error processing; if ((hp = gethostbyname(argv[1])) == NULL) { 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 2: Application Layer 36

17 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) { fromlen = sizeof(raddr); if ((res = recvfrom(s, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&raddr, &fromlen) < 0) { buf[res] = 0; printf(“received: %s”, buf); close(s); return 0; Create datagram Send datagram to server Read datagram from server 2: Application Layer 37

18 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 < 2 || ((port = atoi(argv[1])) <= 0 || port > 65535)) { // error processing; } if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 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) { Create a socket Reserve a port to receive datagram 2: Application Layer 38

19 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, fromlen)) <= 0) { close(s); return(0); Receive datagram Get IP addr port #, of sender Create and send datagram to client End of while loop, loop back and wait for another datagram 2: Application Layer 39

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


Download ppt "CS 1652 Jack Lange University of Pittsburgh"

Similar presentations


Ads by Google