CSE 486/586 Distributed Systems Socket Programming and Android

Slides:



Advertisements
Similar presentations
1 Sockets Nick Feamster Computer Networking I Spring 2013.
Advertisements

Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
CS 4700 / CS 5700 Network Fundamentals
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Socket Programming Application Programming Interface.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Networked Applications: Sockets COS 461: Computer Networks Spring 2010 (MW 3:00-4:20 in CS 105) Michael Freedman Teaching Assistants: Muneeb Ali and David.
Socket Programming.
1 Networked Applications: Sockets COS 461: Computer Networks Spring 2008 (MW 1:30-2:50 in CS 105) Jennifer Rexford Teaching Assistants: Sunghwan Ihm and.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Networked Applications: Sockets COS 461: Computer Networks Spring 2006 (MW 1:30-2:50 in Friend 109) Jennifer Rexford Teaching Assistant: Mike Wawrzoniak.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems The Internet in 2 Hours: The Second Hour Steve Ko Computer Sciences and Engineering University.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Fall 2000Datacom 11 Lecture 4 Socket Interface Programming: Service Interface between Applications and TCP.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
Networked Applications: Sockets
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems The Internet in 2 Hours: The First Hour Steve Ko Computer Sciences and Engineering University.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Remote Shell CS230 Project #4 Assigned : Due date :
ECEN “Internet Protocols and Modeling”, Spring 2012 Course Materials: Papers, Reference Texts: Bertsekas/Gallager, Stuber, Stallings, etc Class.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
UNIX Sockets Outline UNIX sockets CS 640.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems The Internet in 2 Hours: The First Hour Steve Ko Computer Sciences and Engineering University.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Sockets and Beginning Network Programming
CS 3700 Networks and Distributed Systems
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
CS 3700 Networks and Distributed Systems
TCP Sockets Programming
Tutorial on Socket Programming
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Presentation transcript:

CSE 486/586 Distributed Systems Socket Programming and Android Steve Ko Computer Sciences and Engineering University at Buffalo

Last Time What to put on top of physical networks? Layers providing survivability Where to put functionalities? Fate-sharing & end-to-end arguments IP layer doesn’t provide much TCP handles most of the survivability issues TCP & UDP: the two transport protocols of the Internet What interface do applications see? Socket API

Today Today: background for programming assignments Socket programming, Android, and project overviews It’s imperative that you try yourself! If you have no clue at the end of the lecture, please stop by my office. Goal: today’s really about transferring basic knowledge, less about design decisions.

Today’s Question TCP provides a reliable, byte-stream connection as an abstraction. UDP gives raw access to what the IP network provides without all the overhead of TCP. So, how do we program applications on top of these abstractions?

What Applications See App Socket API TCP UDP OS IP Device Drivers Network Interface

UNIX Socket API Socket interface In UNIX, everything is like a file Originally provided in Berkeley UNIX Later adopted by all popular operating systems Simplifies porting applications to different OSes In UNIX, everything is like a file All input is like reading a file All output is like writing a file File is represented by an integer file descriptor API implemented as system calls E.g., socket(), connect(), listen(), read(), write(), close(), … Will look at the details of these system calls

So, Let’s Consider a Scenario What is the server address? IP address How do we identify the Web server? Port number Socket API Socket API OS OS IP network

Using Ports to Identify Services Server host 128.2.194.242 Service request for 128.2.194.242:80 (i.e., the Web server) Client host Web server (port 80) Client OS Echo server (port 7) Service request for 128.2.194.242:7 (i.e., the echo server) Web server (port 80) Client OS Echo server (port 7)

Knowing What Port Number To Use Popular applications have well-known ports E.g., port 80 for Web and port 25 for e-mail See http://www.iana.org/assignments/port-numbers Well-known vs. ephemeral ports Server has a well-known port (e.g., port 80) Between 0 and 1023 Client picks an unused ephemeral (i.e., temporary) port Between 1024 and 65535 Uniquely identifying the traffic between the hosts Two IP addresses and two port numbers Underlying transport protocol (e.g., TCP or UDP)

Typical Client Program E.g. browser Prepare to communicate Create a socket Determine server IP address and port number (e.g., for a Web server) Initiate the connection to the server Exchange data with the server Write data to the socket Read data from the socket Do stuff with the data (e.g., render a Web page) Close the socket

Servers Differ From Clients Passive open Prepare to accept connections … but don’t actually establish … until hearing from a client Hearing from multiple clients Allowing a backlog of waiting clients ... in case several try to communicate at once Create a socket for each client Upon accepting a new client … create a new socket for the communication

Typical Server Program E.g., a Web server Prepare to communicate Create a socket Associate local address and port with the socket Wait to hear from a client (passive open) Indicate how many clients-in-waiting to permit Accept an incoming connection from a client Exchange data with the client over new socket Receive data from the socket Do stuff to handle the request (e.g., get a file) Send data to the socket Close the socket Repeat with the next connection request

Putting it All Together Server socket() bind() Client listen() socket() establish connection accept() connect() block send request write() read() process request send response write() read()

Client Creating a Socket: socket() Operation to create a socket int socket(int domain, int type, int protocol) Returns a descriptor (or handle) for the socket Originally designed to support any protocol suite Domain: protocol family PF_INET for the Internet Type: semantics of the communication SOCK_STREAM: reliable byte stream SOCK_DGRAM: message-oriented service Protocol: specific protocol UNSPEC: unspecified (PF_INET and SOCK_STREAM already implies TCP) socket() connect() write() read()

Client: Learning Server Address/Port Server typically known by name and service E.g., “www.cnn.com” and “http” Need to translate into IP address and port # E.g., “64.236.16.20” and “80” Translating the server’s name to an address struct hostent *gethostbyname(char *name) Argument: host name (e.g., “www.cnn.com”) Returns a structure that includes the host address Identifying the service’s port number struct servent *getservbyname(char *name, char *proto) Arguments: service (e.g., “ftp”) and protocol (e.g., “tcp”)

Client: Connecting Socket to the Server Client contacts the server to establish connection Associate the socket with the server address/port Acquire a local port number (assigned by the OS) Request connection to server, who will hopefully accept Establishing the connection int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen) Arguments: socket descriptor, server address, and address size Returns 0 on success, and -1 if an error occurs Client socket() connect() write() read()

Client: Sending and Receiving Data Sending data ssize_t write(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer of data to send, and length of the buffer Returns the number of characters written, and -1 on error Receiving data ssize_t read(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer Returns the number of characters read (where 0 implies “end of file”), and -1 on error Closing the socket int close(int sockfd) socket() connect() write() read()

Server: Server Preparing its Socket Server creates a socket and binds address/port Server creates a socket, just like the client does Server associates the socket with the port number (and hopefully no other process is already using it!) Create a socket int socket(int domain, int type, int protocol) Bind socket to the local address and port number int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen) Arguments: socket descriptor, server address, address length Returns 0 on success, and -1 if an error occurs socket() bind() listen() accept()

Server: Allowing Clients to Wait Many client requests may arrive Server cannot handle them all at the same time Server could reject the requests, or let them wait Define how many connections can be pending int listen(int sockfd, int backlog) Arguments: socket descriptor and acceptable backlog Returns a 0 on success, and -1 on error What if too many clients arrive? Some requests don’t get through The Internet makes no promises… And the client can always try again socket() bind() listen() accept()

Server: Accepting Client Connection Now all the server can do is wait… Waits for connection request to arrive Blocking until the request arrives And then accepting the new request Accept a new connection from a client int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen) Arguments: socket descriptor, structure that will provide client address and port, and length of the structure Returns descriptor for a new socket for this connection socket() bind() listen() accept()

Client and Server: Cleaning House Once the connection is open Both sides and read and write Two unidirectional streams of data In practice, client writes first, and server reads … then server writes, and client reads, and so on Closing down the connection Either side can close the connection … using the close() system call What about the data still “in flight” Data in flight still reaches the other end So, server can close() before client finishing reading

One Annoying Thing: Byte Order Hosts differ in how they store data E.g., four-byte number (byte3, byte2, byte1, byte0) Little endian (“little end comes first”)  Intel PCs!!! Low-order byte stored at the lowest memory location Byte0, byte1, byte2, byte3 Big endian (“big end comes first”) High-order byte stored at lowest memory location Byte3, byte2, byte1, byte 0 Makes it more difficult to write portable code Client may be big or little endian machine Server may be big or little endian machine

IP is Big Endian But, what byte order is used “on the wire” That is, what do the network protocol use? The Internet Protocols picked one convention IP is big endian (aka “network byte order”) Writing portable code require conversion Use htons() and htonl() to convert to network byte order Use ntohs() and ntohl() to convert to host order Hides details of what kind of machine you’re on Use the system calls when sending/receiving data structures longer than one byte htons == host to network (short) htonl == host to network (long) ntohs == network to host (short) ntohl == network to host (long)

CSE 486/586 Administrivia Recitations will begin from next Monday. Will mainly cover project 0 Project 0 description will be out today. Will talk about this more at the end of the lecture today Will use the submit script The deadline is 2/6/12 (Monday). Please use Piazza; all announcements will go there. If you want an invite, let me know. Please come to my office during the office hours! Give feedback about the class, ask questions, etc.

Android Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

Android Architecture

Android Programming Using the Android APIs and implementing your logic Android expects programmers to write their code in the “Android way”. You need to implement application building blocks defined by Android. Your logic will be largely event-driven: reaction to something happened, e.g., a button touch, a received message, etc. A lot of the times, you’ll struggle with the APIs. It’s necessary to learn how to navigate the API documents and how to quickly find the APIs yourself! One of the goals of this course It’s a necessary skill to learn for any other programming you will do in the future.

Application Building Blocks Activities: an activity is a single screen with a user interface. Subclass of android.app.Activity

Application Building Blocks Services: a service runs in the background with no UI for long-running operations. Playing music, sending/receiving network messages, … Subclass of android.app.Service Content providers: a content provider manages a shared set of application data. Provides a customized storage beyond what Android provides (e.g., contacts, calendar, …) Subclass of android.content.ContentProvider Broadcast receivers: a broadcast receiver responds to system-wide broadcast announcements. Screen off, battery low, picture captured, … Subclass of android.content.BroadcastReceiver

Communication Intent: building blocks communicate through it. android.content.Intent Intent public class PlayService extends Service { …. }

Application Registration AndroidManifest.xml file (the "manifest" file) defines a number of things about the application. Mainly, Components (activity, service, etc.) Permission Letting the system know about the application <?xml version="1.0" encoding="utf-8"?> <manifest ... > <application android:icon="@drawable/app_icon.png" ... > <activity android:name="com.example.project.ExampleActivity" android:label="@string/example_label" ... > </activity> ... </application> </manifest>

Project 0 A simple messenger program Two android devices (emulators) sending messages to each other Official project description will be out today. Step 1: Installation (Eclipse & Android SDK) Use 4.0.3 (API 15) Step 2: Android Dev Guide http://developer.android.com/guide/index.html Read “Android Basics” Read “Actitivies” & “Services” under “Framework Topics” Step 3: Tutorials from Android Developers Minimum: HelloWorld & HelloViews Step 4: Messenger

Project 0 Messenger Design document A user of one device should be able to write a message to the other device. The other device should be able to display what was received. And vice versa. You need to use the socket API (PF_INET & SOCK_STREAM, i.e., TCP). Design document What components you designed and what they do Difficulties you faced (what things took time to figure out, …) (Rough) # of hours you put

Brief Overview: Project 1 ~ Project 3 Distributed key-value store on Android in 3 steps A stripped-down version of Amazon’s Dynamo, a backend storage for Amazon’s services Key: vacation_photo, Value: vacation.jpg

Acknowledgements These slides contain material developed and copyrighted by Indranil Gupta at UIUC Mike Freedman and Jen Rexford at Princeton