CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Socket Programming CS3320 Fall 2010.
Socket programming Socket programming.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
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.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
1 Netcomm Sockets Communication Networks Recitation 1.
Tutorial 8 Socket Programming
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
Sirak Kaewjamnong Computer Network Systems
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
CS x760 Computer Networks1 Socket Programming. CS 6760 Computer Networks2 Socket Programming  What is a socket?  Using sockets  Types (Protocols) ‏
Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,
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.
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.
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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.
Introduction to Socket
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.
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 m Note:
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,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
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 What is a socket? Using sockets Types (Protocols)
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Socket Programming in C
Introduction to Computer Networks
Tutorial on Socket Programming
Transport layer API: Socket Programming
Socket Programming What is a socket? Using sockets Types (Protocols)
Chapter 2 Application Layer
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Sockets.
Presentation transcript:

CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C

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) 2 From: Dan Rubenstein’s slides:

MOST POPULAR TYPES OF SOCKETS TCP socket Type: SOCK_STREAM reliable delivery in-order guaranteed connection-oriented bidirectional We focus on TCP UDP socket Type: SOCK_DGRAM unreliable delivery no order guarantees no notion of “connection” – app indicates destination for each packet can send or receive

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. 4 From: Dan Rubenstein’s slides:

PORTS 5 Each host machine has an IP address (or more!) Each host has 65,536 ports (2 ? ) 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 A socket provides an interface to send data to/from the network through a port From: Dan Rubenstein’s slides:

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? 6 From: Dan Rubenstein’s slides:

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? 7 From: Dan Rubenstein’s slides:

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

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 9

SERVER AND CLIENTS 10 socket() bind() listen() accept() write() read() TCP Server close() socket() TCP Client connect() write() read() close() connection establishment data request data reply end-of-file notification From: UNIX Network Programming Volume 1, figure 4.1

CONNECTION SETUP STEPS 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 Client end: step 2: request & establish connect ion step 4: send/recv 11 Server l-socka-sock-1a-sock-2 Client1 socket Client2 socket From: Dan Rubenstein’s slides:

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 12 From: Dan Rubenstein’s slides:

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 13 From: Dan Rubenstein’s slides:

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 flags : integer, special options, usually just 0 Calls are blocking [returns only after data is sent (to socket buf) / received] 14 From: Dan Rubenstein’s slides:

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 15 From: Dan Rubenstein’s slides:

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 #( ) sin_addr : // Specifies the IP address sin_zero : unused // unused! 16 From: Dan Rubenstein’s slides:

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

18 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 htonl ntohl From: Dan Rubenstein’s slides:

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 void cleanExit(){exit(0);} In socket code: signal(SIGTERM, cleanExit); signal(SIGINT, cleanExit); 19 From: Dan Rubenstein’s slides:

TIPS 2 Check Beej's Guide to Network Programming Using Internet Sockets Search the specification for the function you need to use for more info, or check the man pages. 20

TIPS 3 How to find the IP address of the machine my server program is running on? Use 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 21

LETS WRITE SOME CODE! Sample socket program: 1. Echo server: echo’s what it receives back to client 2. Client/server example. 22

REFERENCES These are good references for further study of Socket programming with C: Beej's Guide to Network Programming Using Internet Sockets Dan Rubenstein’s lecture on Socket “Programming”: sockets.ppt 23

TIPS FOR THE ASSIGNMENT 1 socket() bind() listen() accept() write() read() Proxy close() socket() TCP Client connect() write() read() close() Get request Send data socket() bind() listen() accept() write() read() Web close() socket() connect() write() read() close() Get data modify