Socket programming Socket programming.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
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.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
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 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
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.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
2: Application Layer 1 Socket Programming TCP and UDP.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
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.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
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.
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.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer 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 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.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
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)
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Chapter 2 Application Layer
Socket Programming What is a socket? Using sockets Types (Protocols)
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming.
Socket Programming 2: Application Layer.
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming What is a socket? Using sockets Types (Protocols)
Presentation transcript:

Socket programming Socket programming

What is a socket? goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end-end-transport protocol Internet controlled by OS controlled by app developer transport application physical link network process socket

What is a socket? Socket is an interface between application and network (the lower levels of the protocol stack) The application creates a socket The socket type dictates the style of communication reliable vs. best effort connection-oriented vs. connectionless Once a socket is setup the application can pass data to the socket for network transmission receive data from the socket (transmitted through the network, received from some other host)

Most popular types of sockets TCP socket Type: SOCK_STREAM reliable delivery in-order guaranteed connection-oriented bidirectional UDP socket Type: SOCK_DGRAM unreliable delivery no order guarantees no notion of “connection” – app indicates destination for each packet can send or receive

Ports Each host machine has an IP address (or more!) Each host has 65,536 ports (2?) As you know some ports are reserved for specific apps 20,21: FTP 23: Telnet 80: HTTP see RFC 1700 (about 2000 ports are reserved) Port 0 Port 1 Port 65535 A socket provides an interface to send data to/from the network through a port

Addresses, Ports and Sockets Like apartments and mailboxes You are the application Your apartment building address is the address Your mailbox is the port The post-office is the network The socket is the key that gives you access to the right mailbox (one difference: assume outgoing mail is placed by you in your mailbox) Q: How do you choose which port a socket connects to?

Socket programming Application Example: Client reads a line of characters (data) from its keyboard and sends the data to the server. The server receives the data and converts characters to uppercase. The server sends the modified data to the client. The client receives the modified data and displays the line on its screen.

Ports Each host machine has an IP address (or more!) Each host has 65,536 ports (2?) As you know some ports are reserved for specific apps 20,21: FTP 23: Telnet 80: HTTP see RFC 1700 (about 2000 ports are reserved) Port 0 Port 1 Port 65535 A socket provides an interface to send data to/from the network through a port

Addresses, Ports and Sockets Like apartments and mailboxes You are the application Your apartment building address is the address Your mailbox is the port The post-office is the network The socket is the key that gives you access to the right mailbox (one difference: assume outgoing mail is placed by you in your mailbox) Q: How do you choose which port a socket connects to?

Socket programming with UDP UDP: no “connection” between client & server no handshaking before sending data sender explicitly attaches IP destination address and port # to each packet rcvr extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server

Client/server socket interaction: UDP server (running on serverIP) create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram via clientSocket client create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) read datagram from serverSocket close clientSocket read datagram from write reply to serverSocket specifying client address, port number Application 2-11

Example app: UDP client Python UDPClient include Python’s socket library from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close() create UDP socket for server get user keyboard input Attach server name, port to message; send into socket read reply characters from socket into string print out received string and close socket

Example app: UDP server Python UDPServer from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress) create UDP socket bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client

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 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 that particular client allows server to talk with multiple clients source port numbers used to distinguish clients application viewpoint: TCP provides reliable, in-order byte-stream transfer (“pipe”) between client and server

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

Example app: TCP client Python TCPClient from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() create TCP socket for server, remote port 12000 No need to attach server name, port

Example app: TCP server Python TCPServer from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket)

Socket Creation in C int s = socket(domain, type, protocol); s: socket descriptor, an integer (like a file-handle) domain: integer, communication domain e.g., PF_INET (IPv4 protocol) – typically used type: communication type SOCK_STREAM: reliable, 2-way, connection-based service SOCK_DGRAM: unreliable, connectionless, other values: need root permission, rarely used, or obsolete protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0 NOTE: socket call does not specify where data will be coming from, nor where it will be going to - it just creates the interface.

The bind function The bind function associates and (can exclusively) reserves a port for use by the socket int status = bind(sockid, &addrport, size); status: error status, = -1 if bind failed sockid: integer, socket descriptor addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address) size: the size (in bytes) of the addrport structure bind can be skipped for both types of sockets. When and why?

Skipping the bind SOCK_DGRAM: SOCK_STREAM: if only sending, no need to bind. The OS finds a port each time the socket sends a pkt if receiving, need to bind SOCK_STREAM: destination determined during conn. setup don’t need to know port sending from (during connection setup, receiving end is informed of port)

On the connecting end When connecting to another host (i.e., connecting end is the client and the receiving end is the server), the OS automatically assigns a free port for the outgoing connection. During connection setup, receiving end is informed of port) You can however bind to a specific port if need be.

Connection Setup A connection occurs between two ends Server: waits for an active participant to request connection Client: initiates connection request to passive side Once connection is established, server and client ends are “similar” both can send & receive data either can terminate the connection

Server and clients TCP Server TCP Client socket() bind() listen() accept() connection establishment connect() data request read() write() data reply write() read() read() end-of-file notification close() close()

Connection setup steps Client end: step 2: request & establish connection step 4: send/recv Server end: step 1: listen (for incoming requests) step 3: accept (a request) step 4: send/recv The accepted connection is on a new socket The old socket continues to listen for other active participants Server a-sock-1 l-sock a-sock-2 Client1 Client2 socket socket From: Dan Rubenstein’s slides: http://www.cs.columbia.edu/~danr/courses/6761/Summer03/intro/6761-1b-sockets.ppt

Server Socket: listen & accept Called on server side: int status = listen(sock, queuelen); status: 0 if listening, -1 if error sock: integer, socket descriptor queuelen: integer, # of active participants that can “wait” for a connection listen is non-blocking: returns immediately int s = accept(sock, &addr, &addrlen); s: integer, the new socket (used for data-transfer) sock: integer, the orig. socket (being listened on) addr: struct sockaddr, address of the active participant addrlen: sizeof(addr): value/result parameter must be set appropriately before call adjusted by OS upon return accept is blocking: waits for connection before returning

connect int status = connect(sock, &addr, addrlen); status: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connection addr: struct sockaddr: address of passive participant addrlen: integer, sizeof(addr) connect is blocking

Sending / Receiving Data int count = send(sock, &buf, len, flags); count: # bytes transmitted (-1 if error) buf: void*, buffer to be transmitted len: integer, length of buffer (in bytes) to transmit flags: integer, special options, usually just 0 int count = recv(sock, &buf, len, flags); count: # bytes received (-1 if error) buf: void*, stores received bytes len: # bytes received Calls are blocking [returns only after data is sent (to socket buf) / received]

close When finished using a socket, the socket should be closed: status = close(s); status: 0 if successful, -1 if error s: the file descriptor (socket being closed) Closing a socket closes a connection frees up the port used by the socket From: Dan Rubenstein’s slides: http://www.cs.columbia.edu/~danr/courses/6761/Summer03/intro/6761-1b-sockets.ppt

The struct sockaddr The struct to store the Internet address of a host: struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; sin_family = AF_INET // Specifies the address family sin_port: // Specifies the port #(0-65535) sin_addr: // Specifies the IP address sin_zero: unused // unused! From: Dan Rubenstein’s slides: http://www.cs.columbia.edu/~danr/courses/6761/Summer03/intro/6761-1b-sockets.ppt

SOCKADDR Example struct sockaddr_in server; // Definition memset(&server, 0, sizeof(server)); // Initilize to 0 server.sin_family = AF_INET; // Set address family server.sin_port = htons(MYPORTNUM); // Set port server.sin_addr.s_addr = htonl(INADDR_ANY);// Set address Host Byte-Ordering: the byte ordering used by a host (big- endian or little-endian) Network Byte-Ordering: the byte ordering used by the network – always big-endian Any words sent through the network should be converted to Network Byte-Order prior to transmission (and back to Host Byte- Order once received)

Network Byte-Ordering u_long htonl(u_long x); u_short htons(u_short x); u_long ntohl(u_long x); u_short ntohs(u_short x); On big-endian machines, these routines do nothing On little-endian machines, they reverse the byte order Big-Endian machine Little-Endian machine 128 119 40 12 128.119.40.12 128 119 40 12 128.119.40.12 htonl ntohl 128 119 40 12 128 119 40 12 From: Dan Rubenstein’s slides: http://www.cs.columbia.edu/~danr/courses/6761/Summer03/intro/6761-1b-sockets.ppt

TIPS 1 Sometimes, an ungraceful exit from a program (e.g., ctrl-c) does not properly free up a port Eventually (after a few minutes), the port will be freed You can kill the process, or To reduce the likelihood of this problem, include the following code: In header include: #include <signal.h> void cleanExit(){exit(0);} In socket code: signal(SIGTERM, cleanExit); signal(SIGINT, cleanExit);

Tips 2 Check Beej's Guide to Network Programming Using Internet Sockets http://beej.us/guide/bgnet/output/html/multipage/index.html Search the specification for the function you need to use for more info, or check the man pages.

Tips 3 How to find the IP address of the machine my server program is running on? Use 127.0.0.1 or localhost for accessing a server running on your local machine. For a remote server running linux use the bash shell command: “$ /sbin/ifconfig” For windows, use ipconfig in cmd

summary specific protocols: HTTP FTP SMTP, POP, IMAP DNS our study of network apps now complete! application architectures client-server P2P application service requirements: reliability, bandwidth, delay Internet transport service model connection-oriented, reliable: TCP unreliable, datagrams: UDP specific protocols: HTTP FTP SMTP, POP, IMAP DNS P2P: BitTorrent, DHT socket programming: TCP, UDP sockets

summary most importantly: learned about protocols! important themes: typical request/reply message exchange: client requests info or service server responds with data, status code message formats: headers: fields giving info about data data: info being communicated important themes: control vs. data msgs in-band, out-of-band centralized vs. decentralized stateless vs. stateful reliable vs. unreliable msg transfer “complexity at network edge”