1 CS: 4244 Internet Programming Sockets Programming Dr. Eli Tilevich January 29, 2007.

Slides:



Advertisements
Similar presentations
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r Sockets are explicitly created, used, released by applications.
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.
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.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
Networking Overview February 2, /2/2004 Assignments Due – Homework 0 Due – Reading and Warmup questions Work on Homework 1.
2: Application Layer1 Chapter 2 (continued) Application Layer – part 2 Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim.
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.
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.
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 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.
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
2: Application Layer 1 Socket Programming TCP and UDP.
TCP/IP Sockets in Java: Practical Guide for Programmers Kenneth L. Calvert Michael J. Donahoo.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
ECE5650: Network Programming
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.
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.
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.
18-1 Last time □ Fast retransmit ♦ 3 duplicate ACKs □ Flow control ♦ Receiver windows □ Connection management ♦ SYN/SYNACK/ACK, FIN/ACK, TCP states □ Congestion.
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.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
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.
Java Socket programming. Socket programming with TCP.
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 Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
Chapter 2 Application Layer Application 2-1. Chapter 2: Application layer 2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic.
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.
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.
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
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.
1 All rights reserved to Chun-Chuan Yang Upon completion you will be able to: The OSI Model and the TCP/IP Protocol Suite Understand the architecture of.
Socket Programming Socket Programming Overview
DNS: Domain Name System
Socket programming with TCP
Chapter 2 Application Layer
CSCD 330 Network Programming
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming Socket Programming Overview
Socket Programming.
CSCD 330 Network Programming
Socket Programming 2: Application Layer.
DNS: Domain Name System
DNS: Domain Name System
TCP/IP Sockets in Java: Practical Guide for Programmers
DNS: Domain Name System
Presentation transcript:

1 CS: 4244 Internet Programming Sockets Programming Dr. Eli Tilevich January 29, 2007

2 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley, July A note on the use of these ppt slides: We’re making these slides freely available to all (faculty, students, readers). They’re in PowerPoint form so you can add, modify, and delete slides (including this one) and slide content to suit your needs. They obviously represent a lot of work on our part. In return for use, we only ask the following:  If you use these slides (e.g., in a class) in substantially unaltered form, that you mention their source (after all, we’d like people to use our book!)  If you post any slides in substantially unaltered form on a www site, that you note that they are adapted from (or perhaps identical to) our slides, and note our copyright of this material. Thanks and enjoy! JFK/KWR All material copyright J.F Kurose and K.W. Ross, All Rights Reserved

3 Sockets r process sends/receives messages to/from its socket r socket analogous to door  sending process shoves message out door  sending process relies on transport infrastructure on other side of door which brings message to socket at receiving process process TCP with buffers, variables socket host or server process TCP with buffers, variables socket host or server Internet controlled by OS controlled by app developer r API: (1) choice of transport protocol; (2) ability to fix a few parameters (lots more on this later)

4 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

5 Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UCP 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

6 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 TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint

7 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.

8 Socket programming with TCP Example client-server app: 1) client reads line from standard input ( inFromUser stream), sends to server via socket ( outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket ( inFromServer stream) Client process client TCP socket

9 Client/server socket interaction: TCP wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup

10 Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket

11 Example: Java client (TCP), cont. BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println ("FROM SERVER: " + modifiedSentence ); clientSocket.close(); } Create input stream attached to socket Send line to server Read line from server

12 Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket

13 Example: Java server (TCP), cont DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Read in line from socket Create output stream, attached to socket Write out line to socket End of while loop, loop back and wait for another client connection

14 TCP/IP Sockets in Java: Practical Guide for Programmers r Kenneth L. Calvert r Michael J. Donahoo

15 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection Server starts by getting ready to receive client connections…

16 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection ServerSocket servSock = new ServerSocket(servPort);

17 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection for (;;) { Socket clntSock = servSock.accept();

18 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection Server is now blocked waiting for connection from a client

19 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection Later, a client decides to talk to the server…

20 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection Socket socket = new Socket(server, servPort);

21 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection OutputStream out = socket.getOutputStream(); out.write(byteBuffer);

22 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection Socket clntSock = servSock.accept();

23 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Communicate 3. Close the connection Server 1. Create a TCP socket 2. Repeatedly: a. Accept new connection b. Communicate c. Close the connection InputStream in = clntSock.getInputStream(); recvMsgSize = in.read(byteBuffer);

24 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Establish connection 3. Communicate 4. Close the connection Server 1. Create a TCP socket 2. Bind socket to a port 3. Set socket to listen 4. Repeatedly: a. Accept new connection b. Communicate c. Close the connection close(sock); close(clntSocket)

25 TCP Tidbits Client out.write(“Hello Bob”) in.read() -> “Hi Jane” Server in.read() -> “Hello ” in.read() -> “Bob” out.write(“Hi ”) out.write(“Jane”) Client knows server address and port No correlation between send() and recv()

26 Closing a Connection close() used to delimit communication Analogous to EOF Client out.write(string) while (not received entire string) in.read(buffer) out.write(buffer) close(socket) Server in.read(buffer) while(client has not closed connection) out.write(buffer) in.read(buffer) close(client socket)

27 TCP Tidbits Client out.write(“Hello Bob”) in.read() -> “Hi Jane” Server in.read() -> “Hello ” in.read() -> “Bob” out.write(“Hi ”) out.write(“Jane”) Client knows server address and port No correlation between send() and recv()

28 Closing a Connection close() used to delimit communication Analogous to EOF Client out.write(string) while (not received entire string) in.read(buffer) out.write(buffer) close(socket) Server in.read(buffer) while(client has not closed connection) out.write(buffer) in.read(buffer) close(client socket)

29 Constructing Messages …beyond simple strings

30 TCP/IP Byte Transport r TCP/IP protocols transports bytes r Application protocol provides semantics Application TCP/IP byte stream Application TCP/IP byte stream Here are some bytes. I don’t know what they mean. I’ll pass these to the app. It knows what to do.

31 Application Protocol r Encode information in bytes r Sender and receiver must agree on semantics r Data encoding  Primitive types: strings, integers, and etc.  Composed types: message with fields

32 Primitive Types r String  Character encoding: ASCII, Unicode, UTF  Delimit: length vs. termination character Mom\n

33 r Integer  Strings of character encoded decimal digits Advantage:1. Human readable 2. Arbitrary size Disadvantage:1. Inefficient 2. Arithmetic manipulation Primitive Types ‘1’‘7’‘9’‘9’‘8’‘7’‘0’\n

34 Primitive Types r Integer  Native representation  Network byte order (Big-Endian) Use for multi-byte, binary data exchange htonl(), htons(), ntohl(), ntohs() byte two’s-complement integer 23, Big-Endian Little-Endian

35 Message Composition r Message composed of fields  Fixed-length fields  Variable-length fields integershort Mike12\n