CSE 124 Discussion Section Sockets Programming 10/10/17

Similar documents
TCP: Three-way handshake

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 Section 8 - Client-Side Networking

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

CLIENT-SIDE PROGRAMMING

Socket Programming TCP UDP

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 333 SECTION 6. Networking and sockets

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Programming with TCP/IP. Ram Dantu

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Unix Network Programming

Chapter 6. The Transport Layer. Transport Layer 3-1

Oral. Total. Dated Sign (2) (5) (3) (2)

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

A. Basic Function Calls for Network Communications

Tutorial on Socket Programming

Socket Programming for TCP and UDP

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

System Programming. Sockets

CS 640: Computer Networking

Network Communication

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

CSE 333 SECTION 7. Client-Side Network Programming

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Socket Programming(2/2)

Communication. Sockets (Haviland Ch. 10)

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

EEC-484/584 Computer Networks

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Internet applications

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

STUDY OF SOCKET PROGRAMMING

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

CSE 333 Lecture 16 - network programming intro

UNIT IV- SOCKETS Part A

CS321: Computer Networks Socket Programming

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

Introduction to Socket Programming

Group-A Assignment No. 6

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSE 333 Lecture network programming intro

Networked Applications: Sockets. End System: Computer on the Net

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

Sockets 15H2. Inshik Song

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

CS321: Computer Networks Introduction to Application Layer

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Elementary TCP Sockets

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

ECE 435 Network Engineering Lecture 2

Computer Networks Prof. Ashok K. Agrawala

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Network programming(i) Lenuta Alboaie

Communication Networks ( ) / Fall 2013 The Blavatnik School of Computer Science, Tel-Aviv University

ECE 435 Network Engineering Lecture 2

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

UNIX Network Programming. Overview of Socket API Network Programming Basics

UNIX Sockets. COS 461 Precept 1

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Socket programming in C

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

Client-side Networking

Introduction to Socket Programming

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Server-side Programming

A Client-Server Exchange

CS 3516: Computer Networks

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

CS4700/CS5700 Fundamentals of Computer Networking

Content 1. SDK Library Overview API Library Introduction API Data Structure HAL Driver Function Type LIB

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Transcription:

CSE 124 Discussion Section Sockets Programming 10/10/17

Topics What s a socket? Creating a socket Connecting a socket Sending data Receiving data Resolving URLs to IPs Advanced socket options Live code examples!

What s a socket? Sockets are how programs communicate over the network. There are two types of sockets: Client sockets represent a one-to-one connection between two endpoints. Each client socket has a unique 5-tuple: (Protocol, Source IP, Dest IP, Source Port, Dest Port) Server sockets actively listen for new connections from clients on a specific port. When a new client connects, it creates a new client socket for that connection.

Creating a Socket Here s the socket function (from the socket man page): int sockfd = socket(int socket_family, int socket_type, int protocol); We ll go over each of these arguments in the following slides. The function returns a socket file descriptor ( sockfd ), which is used to other system calls that work with sockets.

Socket Family This is the Layer 3 protocol to use with the socket. Common socket families include: AF_UNIX, AF_LOCAL: Local communication (IPC sockets) AF_INET: IP protocol version 4 (IPv4) AF_INET6: IP protocl version 6 (IPv6) AF_PACKET: Low level packets, usually for raw sockets

Socket Type This is the Layer 4 protocol to use with the socket. For this course, there s only two you need to worry about: SOCK_STREAM: TCP connection. SOCK_DGRAM: UDP connection. The chosen protocol affects how other socket system calls work.

Socket Protocol This usually isn t used; for this course, always set this to 0. Some socket types support various protocols, but usually they only support one.

Connecting a Socket: Server Side First we need to set up the server socket. Clients connect to this. Bind the Socket to an address and port using bind() system call: int bind(int socket_fd, struct sockaddr *addr, socklen_t addrlen); Listen for connections using Listen() system call: void listen(int sockfd, int size_queue); Accept connections using accept system call: int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Binding a Socket This is used to set the IP and port the socket should operate on. The main argument is servaddr, where the program sets the IP + port. But wait: the IP depends on if we re using IPv4 or IPv6! Use struct sockaddr_in for IPv4, and struct sockaddr_in6 for IPv6. For this class, we only need to worry about IPv4. The argument addrlen should always be sizeof(struct sockaddr_in) for IPv4. If the function succeeds, it returns 0, Otherwise, it returns -1.

Creating a struct sockaddr_in struct sockaddr_in { short sin_family; // Always AF_INET unsigned short sin_port; // htons(port_number); struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; }; // load with inet_pton() inet_pton(af_inet, 192.168.1.25, &my_sockaddr_in.sin_addr.s_addr);

Listening on a Socket Now we need to tell the OS to allow clients to connect to it using the listen() call. size_queue is how many clients can be waiting for the server to accept their connection.

Accepting a Client Connection The server uses the accept() call to accept client connections. Here, the argument addr will get filled in by the function with the client s IP + port. The value returned will be -1 if an error occurred. Otherwise, the return value is a new socket fd that the server should use to communicate with the client via the send() and recv() calls.

Connecting a Socket: Client Side Clients only need to use one system call to connect to a server socket: int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); The addr argument is the same as in bind(), but in this case is the IP + port the client wants to connect to. This call will return 0 for a successful connection and -1 if it failed. Once a socket is connected, it must be closed later.

Closing a Socket This system call closes the socket/connection between client and server void close(int sockfd); Always close the socket before exiting the program. If the socket is not closed then the other side of the socket won t know that the connection has ended.

Sending data The send() call is used to send data over the network via a client socket. ssize_t send(int sockfd, const void *buffer, size_t len, int flags); This system call sends len bytes from buffer to the other end of the socket. flags can be used to set advanced features, but usually it can just be 0. The return value is the number of bytes that were sent to the network (-1 on error). If the socket has been closed (by either side), an error is returned.

Send: UDP vs TCP For TCP and UDP, send() acts differently. For UDP, send() will either consume all or none of the data. If data is consumed, that becomes a single UDP packet in the network. For TCP, send() may consume, all, some, or none of the data. Use sendall(): ssize_t sendall(int sockfd, const void *buffer, size_t len) { ssize_t ret, sent = 0; while(sent < len) { ret = send(sockfd, buffer + sent, len - sent); if (ret >= 0) sent += ret; } return sent; }

Receiving data The recv() call is used to receive data from the network. ssize_t recv(int sockfd, void *buffer, size_t len, int flags); This system call receives up to len bytes of data and it stores in buffer. flags can be used to set advanced features, but usually it can just be 0. The return value is the number of bytes that were received from the network (-1 on error).

Recv: UDP vs TCP Again, the way recv() works depends on the protocol used. In UDP, recv() retrieves the data in a single UDP packet. In TCP, recv() retrieves between 1 and len bytes. Use recvall() if needed. In both cases, recv() blocks until there is data. If there is no data left and the socket is closed, then recv() returns 0 and doesn t block.

Resolving URLs to IPs Sockets only connect to IPs. For URLs, we have to perform DNS resolution. int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); node: Website to query, but without protocol info, e.g. www.google.com service: Protocol to use, e.g. http hints: Hints to the DNS resolver about how to perform the DNS query res: Set by the function. A pointer to an array of IP + ports associated with the requested URL. This must be freed using freeaddrinfo()

Advanced Socket Options There s a number of advanced socket options that can be read/set using two similar system calls: int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); int optval = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval);

Advanced Socket Options SO_REUSEADDR: Allow reusing the same source address (if local address) SO_KEEPALIVE: Allow sending keepalive messages for certain protocols SO_BINDTODEVICE: Bind the socket to a specific hardware interface (L2 bind) SO_SNDBUF/SO_RCVBUF: Set the buffer size for sending/receiving SO_SNDTIMEO/SO_RCVTIMEO: Set the timeout for send/recv calls If one of these options is set on a server socket, all client sockets created by it will have the option set as well.

Live code examples!