Week 13 - Wednesday.  What did we talk about last time?  Networking.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
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 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Client-Side Network Programming
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Week 13 - Monday.  What did we talk about last time?  Low level file I/O practice  File systems.
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
Introduction to Sockets
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets and Beginning Network Programming
Week 13 - Wednesday CS222.
UNIX Sockets COS 461 Precept 1.
Socket Programming (Cont.)
Week 14 - Monday CS222.
Class A Addresses The 1st bit of a class A address is 0
Week 13 - Friday CS222.
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
UDP Sockets Programming
TCP Sockets Programming
Advanced Network Programming spring 2007
Issues in Client/Server Programming
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Server-side Programming CSE 333 Winter 2019
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Sockets.
Presentation transcript:

Week 13 - Wednesday

 What did we talk about last time?  Networking

Besides a mathematical inclination, an exceptionally good mastery of one's native tongue is the most vital asset of a competent programmer. Edsger Dijkstra Besides a mathematical inclination, an exceptionally good mastery of one's native tongue is the most vital asset of a competent programmer. Edsger Dijkstra

 If you want to create a socket, you can call the socket() function  The function takes a communication domain  Will always be AF_INET for IPv4 Internet communication  It takes a type  SOCK_STREAM usually means TCP  SOCK_DGRAM usually means UDP  It takes a protocol  Which will always be 0 for us  It returns a file descriptor (an int ) int sockFD = -1; sockFD = socket(AF_INET, SOCK_STREAM, 0); int sockFD = -1; sockFD = socket(AF_INET, SOCK_STREAM, 0);

 What are you going to do with it?  By themselves, they aren't useful  You need to connect them together  We're going to be interested in the following functions to work with sockets  bind()  listen()  accept()  connect()  And also functions that are similar to the ones you know from low-level file I/O  recv()  send()  close()

socket() bind() listen() accept() recv() send() close() socket() connect() recv() send() close() Repeat until done ServerClient

 Once you've created your socket, set up your port and address, and called connect(), you can send data  Assuming there were no errors  Sending is very similar to writing to a file  The send() function takes  The socket file descriptor  A pointer to the data you want to send  The number of bytes you want to send  Flags, which can be 0 for us  It returns the number of bytes sent char* message = "Flip mode is the squad!"; send(socketFD, message, strlen(message)+1, 0); char* message = "Flip mode is the squad!"; send(socketFD, message, strlen(message)+1, 0);

 Or, once you're connected, you can also receive data  Receiving is very similar to reading from a file  The recv() function takes  The socket file descriptor  A pointer to the data you want to receive  The size of your buffer  Flags, which can be 0 for us  It returns the number of bytes received, or 0 if the connection is closed, or -1 if there was an error char message[100]; recv(socketFD, message, 100, 0); char message[100]; recv(socketFD, message, 100, 0);

 Sending and receiving are the same on servers, but setting up the socket is more complex  Steps: 1. Create a socket in the same way as a client 2. Bind the socket to a port 3. Set up the socket to listen for incoming connections 4. Accept a connection

 Is your machine little endian or big endian?  Is the network you're connecting to little endian or big endian?  You don't want to have to keep it straight  But the port number you put in a sockaddr_in has to have the network endianness  htons() takes a short from host endianness to network endianness  ntohs() takes a short from network endianness to host endianness  Both functions work correctly whether or not the network and the host have the same endianness

 Binding attaches a socket to a particular port at a particular IP address  You can give it a flag that automatically uses your local IP address, but it could be an issue if you have multiple IPs that refer to the same host  Use the bind() function, which takes  A socket file descriptor  A sockaddr pointer (which will be a sockaddr_in pointer for us) giving the IP address and port  The length of the address struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(80); address.sin_addr.s_addr = INADDR_ANY; bind(socketFD, (struct sockaddr*)&address, sizeof(address)); struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(80); address.sin_addr.s_addr = INADDR_ANY; bind(socketFD, (struct sockaddr*)&address, sizeof(address));

 If you don't want to fill out the sockaddr_in structure by hand, you can use the getaddrinfo() function to do it for you  Unfortunately, you have to fill out a hints structure which is almost as bad  But getaddrinfo() is good because it can do DNS lookup, allowing you to put in a domain name ( instead of an IP address

 getaddrinfo() takes  A string giving an IP address or a domain name (or NULL if it's going to figure out your computer's IP for you)  A string giving the port number (e.g. "80" ) or the service linked to a port number (e.g. "http" )  A pointer to an addrinfo structure that gives hints about the address  A pointer to a pointer to an addrinfo structure, which will end up pointing to a dynamically allocated linked list of addresses that fit the bill struct addrinfo hints, *addresses; memset(&hints, 0, sizeof(hints)); hints.ai_flag = AI_PASSIVE; //pick IP for me hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(NULL, "80", &hints, &addresses); struct addrinfo hints, *addresses; memset(&hints, 0, sizeof(hints)); hints.ai_flag = AI_PASSIVE; //pick IP for me hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(NULL, "80", &hints, &addresses);

 The previous code showed how to call it to get an unspecified IP address for a local machine  We could use it to get an IP that we specify as follows struct addrinfo hints, *addresses; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(" "http", &hints, &addresses); struct addrinfo hints, *addresses; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(" "http", &hints, &addresses);

 After a server has bound a socket to an IP address and a port, it can listen on that port for incoming connections  To set up listening, call the listen() function  It takes  A socket file descriptor  The size of the queue that can be waiting to connect  You can have many computers waiting to connect and handle them one at a time  For our purpose, a queue of size 1 often makes sense listen( socketFD, 1);

 Listening only sets up the socket for listening  To actually make a connection with a client, the server has to call accept()  It is a blocking call, so the server will wait until a client tries to connect  It takes  A socket file descriptor  A pointer to a sockaddr structure that will be filled in with the address of the person connecting to you  A pointer to the length of the structure  It returns a file descriptor for the client socket  We will usually use a sockaddr_storage structure struct sockaddr_storage otherAddress; socklen_t otherSize = sizeof(otherAddress); int otherSocket; otherSocket = accept( socketFD, (struct sockaddr *) &otherAddress, &otherSize); struct sockaddr_storage otherAddress; socklen_t otherSize = sizeof(otherAddress); int otherSocket; otherSocket = accept( socketFD, (struct sockaddr *) &otherAddress, &otherSize);

 The setsockopt() function allows us to set a few options on a socket  The only one we care about is the SO_REUSEADDR option  If a server crashes, it will have to wait for a timeout (a minute or so) to reconnect on the same port unless this option is set  A dead socket is taking up the port  When working on Project 6, it's a good idea to use this so you don't get stuck waiting around int value = 1; //1 to turn on port reuse setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); int value = 1; //1 to turn on port reuse setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));

 Last time, we suggested that you use send() and recv() for writing to and reading from sockets  You can actually use write() and read()  The difference is that send() and recv() have an extra parameter for flags  We provided 0 as the argument (no value)  These flags control how the send() or recv() acts

FlagMeaning for send() Meaning for recv() MSG_DONTWAIT Nonblocking send. If the buffer is full, return EAGAIN. Nonblocking receive. If no message is available, return EAGAIN. MSG_OOB Send a single out of band (high priority) byte Receive a single out of band (high priority) byte MSG_PEEK Invalid flag Read the data from the buffer, but don't remove it MSG_WAITALL Keep reading until you have received the maximum bytes you can hold MSG_MORE A series of messages will be packed into a single TCP packet Invalid flag MSG_NOSIGNAL A send on a closed socket will not generate a signal

 This is the basic sockaddr used by socket functions:  We often need sockaddr_in :  They start with the same bytes for family, we can cast without a problem  C has no inheritance, we can't use a child class struct sockaddr { unsigned short sa_family; //address family char sa_data[14]; //14 bytes of address }; struct sockaddr { unsigned short sa_family; //address family char sa_data[14]; //14 bytes of address }; struct sockaddr_in { short sin_family; // AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // 4 bytes char sin_zero[8]; // zero this }; struct sockaddr_in { short sin_family; // AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // 4 bytes char sin_zero[8]; // zero this };

 Let's make a server and connect to it with nc  We'll just print everything we get to the screen

 C can have pointers to functions  You can call a function if you have a pointer to it  You can store these function pointers in arrays and structs  They can be passed as parameters and returned as values  Java doesn't have function pointers  Instead, you pass around objects that have methods you want  C# has delegates, which are similar to function pointers

 K&R group function pointers in with other pointers  I put them off because:  They are confusing  The syntax to declare function pointer variables is awful  They are not used very often  They are not type-safe  But you should still know of their existence!

 The syntax is a bit ugly  Pretend like it's a prototype for a function  Except take the name, put a * in front, and surround that with parentheses #include int main() { double (*root) (double); //pointer named root root = &sqrt; //note there are no parentheses printf( "Root 3 is %lf", root(3) ); printf( "Root 3 is %lf", (*root)(3) ); //also legal return 0; } #include int main() { double (*root) (double); //pointer named root root = &sqrt; //note there are no parentheses printf( "Root 3 is %lf", root(3) ); printf( "Root 3 is %lf", (*root)(3) ); //also legal return 0; }

 Some function's prototype:  Its (worthless) definition:  A compatible function pointer:  Function pointer assignment: int** fizbin(char letter, double length, void* thing); int** fizbin(char letter, double length, void* thing) { return (int**)malloc(sizeof(int*)*50); } int** fizbin(char letter, double length, void* thing) { return (int**)malloc(sizeof(int*)*50); } int** (*pointer)(char, double, void*); pointer = fizbin;

 Just to be confusing, C allows two different styles for function pointer assignment and usage #include int main() { int (*thing) (); //pointer named thing thing = &main; //looks like regular pointers thing = main; //short form with & omitted (*thing)();//normal dereference thing();//short form with * omitted return 0; } #include int main() { int (*thing) (); //pointer named thing thing = &main; //looks like regular pointers thing = main; //short form with & omitted (*thing)();//normal dereference thing();//short form with * omitted return 0; }

Why would we want function pointers?

 Consider a bubble sort that sorts an array of strings  The book uses quicksort as the example, but I don't want to get caught up in the confusing parts of quicksort void bubbleSort(char* array[], int length) { char* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(strcmp(array[j],array[j+1]) > 0) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } void bubbleSort(char* array[], int length) { char* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(strcmp(array[j],array[j+1]) > 0) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 Now consider a bubble sort that sorts arrays of pointers to single int values void bubbleSort(int* array[], int length) { int* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(*(array[j]) > *(array[j+1])) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } void bubbleSort(int* array[], int length) { int* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(*(array[j]) > *(array[j+1])) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 Let's pause for a moment in our consideration of sorts and make a struct that can contain a rectangle typedef struct { double x;//x value of upper left double y;//y value of upper left double length; double height; } Rectangle; typedef struct { double x;//x value of upper left double y;//y value of upper left double length; double height; } Rectangle;

 Now consider a bubble sort that sorts arrays of pointers to Rectangle structs  Ascending sort by x value, tie-breaking with y value void bubbleSort(Rectangle* array[], int length) { Rectangle* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(array[j]->x > array[j+1]->x || (array[j]->x == array[j+1]->x && array[j]->y > array[j+1]->y)) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } void bubbleSort(Rectangle* array[], int length) { Rectangle* temp; int i, j; for(i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(array[j]->x > array[j+1]->x || (array[j]->x == array[j+1]->x && array[j]->y > array[j+1]->y)) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 We can write a bubble sort (or ideally an efficient sort) that can sort anything  We just need to provide a pointer to a comparison function void bubbleSort(void* array[], int length, int (*compare)(void*, void*)) { void* temp; int i, j; for( i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(compare(array[j],array[j+1]) > 0) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } void bubbleSort(void* array[], int length, int (*compare)(void*, void*)) { void* temp; int i, j; for( i = 0; i < length – 1; i++ ) for(j = 0; j < length – 1; j++ ) if(compare(array[j],array[j+1]) > 0) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }

 Function pointers don't give you a lot of typechecking  You might get a warning if you store a function into an incompatible pointer type  C won't stop you  And then you'll be passing who knows what into who knows where and getting back unpredictable things

 C doesn't have classes or objects  It is possible to store function pointers in a struct  If you always pass a pointer to the struct itself into the function pointer when you call it, you can simulate object-oriented behavior  It's clunky and messy and there's always an extra argument in every function (equivalent to the this pointer)  As it turns out, Java works in a pretty similar way  But it hides the ugliness from you

 Introduction to C++  Lab 13

 Keep working on Project 6  It's tough!  Read Section 5.11 of K&R if you want more information on function pointers