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.

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.
Network Programming and Java Sockets
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
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.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
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.
Protocols Rules for communicating between two entities (e.g., a client and a server) “A protocol definition specifies how distributed system elements interact.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
1 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.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
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.
1 DNS: Domain Name System People: many identifiers: m SSN, name, Passport # Internet hosts, routers: m IP address (32 bit) - used for addressing datagrams.
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.
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,
Networking OSI (Open Systems Interconnection) model of computer networking, seven layers (the Application, Presentation, Session, Transport, Network, Data.
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.
CPSC Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default.
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.
Socket programming in C
CS 1652 Jack Lange University of Pittsburgh
Socket programming with TCP
Chapter 2 Application Layer
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming.
Socket Programming 2: Application Layer.
Socket Programming Neil Tang 09/08/2008
Socket programming in C
Presentation transcript:

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 Reserved Jack Lange University of Pittsburgh 1

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

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

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

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)

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()

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

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

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

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

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

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

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

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

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

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

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

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

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

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()