CS 1652 Jack Lange University of Pittsburgh

Slides:



Advertisements
Similar presentations
Socket Programming Application Programming Interface.
Advertisements

Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
2: Application Layer 1 Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5th edition. Jim Kurose, Keith Ross Addison-Wesley, April.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Network Layers Application Transport Network Data-Link Physical bits.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Julia Ljunbjörk and Anita Mugenyi. What is a socket? Like a house Between the layers.
2: Application Layer 1 Socket Programming TCP and UDP.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Discussion 2 Sockets Programming Applets TCP UDP HTTP Delay Estimation
Application Layer 2-1 ESERCITAZIONE SOCKET PROGRAMMING.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
Socket Programming Lee, Sooyong
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Chapter 2 Applications and Layered Architectures Sockets.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
1 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.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
05 - P2P applications and Sockets
Socket programming in C
Socket Programming Socket Programming Overview
Socket programming with TCP
Imam Ahmad Trinugroho, ST., MMSI
Chapter 2 Application Layer
Transport layer API: Socket Programming
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming Socket Programming Overview
Socket Programming.
Chapter 2 Application Layer
Socket Programming 2: Application Layer.
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Socket Programming with UDP
Socket programming in C
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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