Client-side Networking CSE 333 Spring 2018

Slides:



Advertisements
Similar presentations
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Advertisements

Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming Application Programming Interface.
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.
Tutorial 8 Socket Programming
1 Advanced Name and Address Conversions getaddrinfo, getnameinfo, gai_strerror, freeaddrinfo host_serv, tcp_connect, tcp_listen, udp_client, udp_connect,
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
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.
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.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Assignment 3 A Client/Server Application: Chatroom.
Client-Side Network Programming
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
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,
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
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.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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.
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 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Chapter 11 Advanced Name and Address Conversion. Introduction gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: –a function providing protocol.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CSE 333 – SECTION 8 Client-Side Network Programming.
Sockets API Developing Applications using the Sockets API.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Week 13 - Friday CS222.
Socket Programming in C
Server-side Programming CSE 333 Spring 2018
Client-Side Network Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
C++ Templates CSE 333 Spring 2018
UDP Sockets Programming
TCP Sockets Programming
Network Programming: Part II CSCI 380: Operating Systems Lecture #13
IP Addresses, DNS CSE 333 Spring 2018
References Revisited CSE 333 Spring 2018
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
IP Addresses, DNS CSE 333 Summer 2018
Client-side Networking CSE 333 Summer 2018
IP Addresses, DNS CSE 333 Autumn 2018
Concurrency and Processes CSE 333 Spring 2018
Client-side Networking CSE 333 Autumn 2018
Network Programming: Part II CSCI 380: Operating Systems
IP Addresses, DNS CSE 333 Winter 2019
Server-side Programming CSE 333 Winter 2019
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Client-side Networking CSE 333 Winter 2019
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Sockets.
Client-side Networking CSE 333 Spring 2019
Presentation transcript:

Client-side Networking CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang Wei Lin

Administrivia hw3 is due Thursday (5/17) Usual reminders: don’t forget to tag, clone elsewhere, and recompile Exercise 15 will be released on Thursday Related to section this week hw4 out on Friday (5/18)

Socket API: Client TCP Connection There are five steps: Figure out the IP address and port to connect to Create a socket Connect the socket to the remote server .read() and write() data using the socket Close the socket

DNS Lookup Example See dnsresolve.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 };

Step 2: Creating a Socket Use the socket() system call Creating a socket doesn’t bind it to a local address or port yet Returns file descriptor or -1 on error int socket(int domain, int type, int protocol); socket.cc #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <iostream> int main(int argc, char** argv) { 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;

Step 3: Connect to the Server The connect() system call establishes a connection to a remote host sockfd: Socket file description from Step 2 addr and addrlen: Related to address structures from Step 1 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 involves ~2 round trips across the network int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);

Connect Example See connect.cc // Get an appropriate sockaddr structure. struct sockaddr_storage addr; size_t addrlen; LookupName(argv[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;

Review Question How do we error check read() and write()? A. ferror() Vote at http://PollEv.com/justinh A. ferror() B. Return value less than expected C. Return value of 0 or NULL D. Return value of -1 E. We’re lost…

Step 4: read() If there is data that has already been received 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 arrives This might cause deadlock! Can read() return 0? oipu

Step 4: read() Assume we have: int socket_fd; // fd of connected socket char readbuf[BUF]; // read buffer int res; // to store read result Write C++ code to read in BUF characters from socket_fd If error occurs, send error message to user and exit()

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

Read/Write Example See sendreceive.cc Demo 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; break; sendreceive demo: (term1) ./sendreceive (term2) nc -l 3333; (term1) ./sendreceive 127.0.0.1 3333; (term2) hello, world! (term2) nc -l 3333; (term1) ./sendreceive 127.0.0.1 3333; (term2) Ctrl-D (term1) ./sendreceive 127.0.0.1 1111 (term1) ./sendreceive 127.0.0.0 3333

Step 5: close() int close(int fd); Nothing special here – it’s the same function as with file I/O int close(int fd);

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