Client-side Networking

Similar documents
CSE 333 Section 8 - Client-Side Networking

Server-side Programming

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 7. Client-Side Network Programming

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

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

CSE 333 Lecture network programming intro

CSE 333 Lecture 16 - network programming intro

CSE 333 SECTION 6. Networking and sockets

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

CSE 333 SECTION 7. Client-Side Network Programming

Server-side Programming

CSE 333 Lecture 8 - file and network I/O

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

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

The exam is closed book, closed notes, closed electronics, closed telepathy, open mind.

Write your answer on the next page. You may remove this page for reference while working if you wish.

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

Introduction to Concurrency

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

Pointers, Pointers, Pointers

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

C++ Templates. CSE 333 Autumn 2018

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

CSE 124 Discussion Section Sockets Programming 10/10/17

Computer Network Programming

Concurrency: Threads. CSE 333 Autumn 2018

C++ Class Details, Heap

UNIX System Programming Lecture 19: IP Sockets

Data Structures and Modules

Final C Details. CSE 333 Autumn 2018

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

C++ Inheritance I. CSE 333 Autumn 2018

C++ Smart Pointers. CSE 333 Autumn 2018

C++ Intro. CSE 333 Autumn 2018

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

Redesde Computadores(RCOMP)

Azblink API for Sending XMPP Messages via HTTP POST

CS 640: Computer Networking

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

// print product names in alphabetical order with total // number sold of each product using format name: total void PrintTotals();

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

CSE 333 Final Exam June 6, Name ID #

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

C++ Inheritance II, Casting

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

CSCI 4061: Sockets and Network Programming

UNIX Sockets. COS 461 Precept 1

6 장과제샘플코드 - 다자간채팅 (udp 버전 1) 목포해양대해양컴퓨터공학과

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

last time redo logging copy-on-write filesystems / snapshots distributed systems motivation, etc.

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

Sockets 15H2. Inshik Song

CLIENT-SIDE PROGRAMMING

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

CMPSC 311- Introduction to Systems Programming Module: Network Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

CSE 333 SECTION 3. POSIX I/O Functions

Socket Programming for TCP and UDP

TCP: Three-way handshake

CS4700/CS5700 Fundamentals of Computer Networking

Network Socket Programming - 3 BUPT/QMUL

2007 Microsoft Corporation. All rights reserved.

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

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

Network Socket Programming - 3 BUPT/QMUL

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

What s an API? Do we need standardization?

Tutorial on Socket Programming

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

Category: Informational J. Bound J. McCann Hewlett-Packard W. Stevens February 2003

5. System Calls or Bust

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

목포해양대해양컴퓨터공학과. IPv6 적용

Elementary TCP Sockets

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

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

Porting IPv4 applications to IPv4/v6 dual stack. Owen DeLong

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

Socket Programming TCP UDP

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

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

Winsock Server adding Multiple clients support C++

Final C Details. CSE 333 Winter Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht

Introduction to Computer Networks

Introduction to Concurrency

CS 351 Week 15. Course Review

EEC-484/584 Computer Networks

CSE 333 Final Exam June 6, 2017 Sample Solution

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

Programming with TCP/IP. Ram Dantu

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

CS 3516: Computer Networks

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

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

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

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

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

Transcription:

Client-side Networking CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Trais McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu

Administriia hw4 out today due Thur. Dec. 6 (last week of qtr) Demo today in class Exercise 15 due Monday Client-side TCP connection Companion exercise 16 out Monday, due next Wednesday Serer-side TCP connection (to talk with your client-side code!) 2

Socket API: Client TCP Connection There are fie steps: 1) Figure out the IP address and port to connect to 2) Create a socket 3) Connect the socket to the remote serer 4).read() and write() data using the socket 5) Close the socket 3

Step 1: DNS Lookup Coered in lecture and section already See dnsresole.cc struct addrinfo { int ai_flags; // additional flags int ai_family; // AF_INET, AF_INET6, AF_UNSPEC int ai_socktype; // SOCK_STREAM, SOCK_DGRAM, 0 int ai_protocol; // IPPROTO_TCP, IPPROTO_UDP, 0 size_t ai_addrlen; // length of socket addr in bytes struct sockaddr* ai_addr; // pointer to socket addr char* ai_canonname; // canonical name struct addrinfo* ai_next; // can form a linked list ; 4

Step 2: Creating a Socket Use the socket() system call int socket(int domain, int type, int protocol); Creating a socket doesn t bind it to a local address or port yet Returns file descriptor or -1 on error socket.cc #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <iostream> int main(int argc, char** arg) { int socket_fd = socket(af_inet, SOCK_STREAM, 0); if (socket_fd == -1) { std::cerr << strerror(errno) << std::endl; return EXIT_FAILURE; close(socket_fd); return EXIT_SUCCESS; 5

Step 3: Connect to the Serer The connect() system call establishes a connection to a remote host int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen); sockfd: Socket file description from Step 2 addr and addrlen: Usually from one of the address structures returned by getaddrinfo in Step 1 (DNS lookup) Returns 0 on success and -1 on error connect() may take some time to return It is a blocking call by default The network stack within the OS will communicate with the remote host to establish a TCP connection to it This inoles ~2 round trips across the network 6

Connect Example See connect.cc // Get an appropriate sockaddr structure. struct sockaddr_storage addr; size_t addrlen; LookupName(arg[1], port, &addr, &addrlen); // Create the socket. int socket_fd = socket(addr.ss_family, SOCK_STREAM, 0); if (socket_fd == -1) { cerr << "socket() failed: " << strerror(errno) << endl; return EXIT_FAILURE; // Connect the socket to the remote host. int res = connect(socket_fd, reinterpret_cast<sockaddr*>(&addr), addrlen); if (res == -1) { cerr << "connect() failed: " << strerror(errno) << endl; 7

Step 4: read() If there is data that has already been receied by the network stack, then read will return immediately with it read() might return with less data than you asked for If there is no data waiting for you, by default read() will block until something arries This might cause deadlock! Can read() return 0? 9

Step 4: write() write() enqueues your data in a send buffer in the OS and then returns The OS transmits the data oer the network in the background When write() returns, the receier probably has not yet receied the data! If there is no more space left in the send buffer, by default write() will block 11

Read/Write Example while (1) { int wres = write(socket_fd, readbuf, res); if (wres == 0) { cerr << "socket closed prematurely" << endl; close(socket_fd); return EXIT_FAILURE; if (wres == -1) { if (errno == EINTR) continue; cerr << "socket write failure: " << strerror(errno) << endl; close(socket_fd); return EXIT_FAILURE; break; See sendreceie.cc Demo 12

Step 5: close() int close(int fd); Nothing special here it s the same function as with file I/O Shuts down the socket and frees resources and file descriptors associated with it on both ends of the connection 13

hw4 demo Multithreaded Web Serer (333gle) Don t worry multithreading has mostly been written for you./http333d <port> <static files> <indices+> Some security bugs to fix, too 14

Extra Exercise #1 Write a program that: Reads DNS names, one per line, from stdin Translates each name to one or more IP addresses Prints out each IP address to stdout, one per line 15