Sockets and Beginning Network Programming

Slides:



Advertisements
Similar presentations
Socket Programming Application Programming Interface.
Advertisements

Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Tutorial 8 Socket Programming
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.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
UNIX Sockets COS 461 Precept 1.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
Computer Networks Chapter 1: Foundation
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
CompSci 356: Introduction to Computer Networks Lecture 3: Hardware and physical links Chap 1.4, 2 of [PD] Xiaowei Yang
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.
1 Chapter 1 Foundation Computer Networks: A Systems Approach, 5e Larry L. Peterson and Bruce S. Davie Copyright © 2010, Elsevier Inc. All rights Reserved.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Remote Shell CS230 Project #4 Assigned : Due date :
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
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.
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.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
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,
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.
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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 in C
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Internet and Intranet Protocols and Applications
Berkeley API Socket Programming
The University of Adelaide, School of Computer Science
Socket Programming in C
Berkeley API Socket Programming
Chapter 2: Application layer
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Berkeley API Socket Programming
Outline Communications in Distributed Systems Socket Programming
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Socket programming in C
Presentation transcript:

Sockets and Beginning Network Programming Networking CS 3470, Section 1 Sarah Diesburg

Network Software Having a network is nice. But if there is no software to transmit information on top of the network, what is the point? Operating systems implement the core networking stack As application writers, we learn how to use the operating system networking API to send and receive packets

Network Software Operating systems are crucial Implement drivers for networking cards

Network Software Receiving a packet Accept packets from a network

Network Software Receiving a packet Process the packet Take off headers, reassemble multiple packets together in the right order, etc.

Network Software Receiving a packet Hands the information in the packet to an application running on the machine

The University of Adelaide, School of Computer Science 23 April 2018 Network API - Sockets Socket Interface was originally provided by the Berkeley distribution of Unix - Now supported in virtually all operating systems Each protocol provides a certain set of services, and the API provides a syntax by which those services can be invoked in this particular OS Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 23 April 2018 Socket What is a socket? The point where a local application process attaches to the network An interface between an application and the network An application creates the socket The interface defines operations for Creating a socket Attaching a socket to the network Sending and receiving messages through the socket Closing the socket Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 23 April 2018 Socket Socket Family PF_INET denotes the Internet family PF_UNIX denotes the Unix pipe facility PF_PACKET denotes direct access to the network interface (i.e., it bypasses the TCP/IP protocol stack) Socket Type SOCK_STREAM is used to denote a byte stream SOCK_DGRAM is an alternative that denotes a message oriented service, such as that provided by UDP Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 23 April 2018 Creating a Socket int sockfd = socket(address_family, type, protocol); The socket number returned is the socket descriptor for the newly created socket int sockfd = socket (PF_INET, SOCK_STREAM, 0); int sockfd = socket (PF_INET, SOCK_DGRAM, 0); The combination of PF_INET and SOCK_STREAM implies TCP Chapter 2 — Instructions: Language of the Computer

Client-Server Model The server is passively waiting for connections The client queries the server for information, the server responds to the client query response server client

The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model Server Passive open Prepares to accept connection, does not actually establish a connection Server invokes int bind (int socket, struct sockaddr *address, int addr_len) int listen (int socket, int backlog) int accept (int socket, struct sockaddr *address, int *addr_len) Chapter 2 — Instructions: Language of the Computer

Client-Server Model with TCP The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model with TCP Bind Binds the newly created socket to the specified address i.e. the network address of the local participant (the server) Address is a data structure which combines IP and port Listen Defines how many connections can be pending on the specified socket Chapter 2 — Instructions: Language of the Computer

Client-Server Model with TCP The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model with TCP Accept Carries out the passive open Blocking operation Does not return until a remote participant has established a connection When it does, it returns a new socket that corresponds to the new established connection and the address argument contains the remote participant’s address Chapter 2 — Instructions: Language of the Computer

Client-Server Model with TCP The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model with TCP Client Application performs active open It says who it wants to communicate with Client invokes int connect (int socket, struct sockaddr *address, int addr_len) Connect Does not return until TCP has successfully established a connection at which application is free to begin sending data Address contains remote machine’s address Chapter 2 — Instructions: Language of the Computer

Client-Server Model with TCP The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model with TCP In practice The client usually specifies only remote participant’s address and let’s the system fill in the local information Whereas a server usually listens for messages on a well-known port A client does not care which port it uses for itself, the OS simply selects an unused one Chapter 2 — Instructions: Language of the Computer

What is a port?? When the operating system receives data from a packet, which listening application receives it? Web server (HTTP)? Mail server (SMTP)? SSH server? Each packet is addressed to a certain port number

What is a port?? Some port numbers are “well-known” numbers 25 is almost always a mail server 22 is almost always SSH Applications know to use these numbers by default But many allow you to change the port number if a service is running on an abnormal port Take a look at the /etc/services file $> nano /etc/services

/etc/services

Client-Server Model with TCP The University of Adelaide, School of Computer Science 23 April 2018 Client-Server Model with TCP Once a connection is established, the application process invokes two operations int send (int socket, char *msg, int msg_len, int flags) int recv (int socket, char *buff, int buff_len, Chapter 2 — Instructions: Language of the Computer

Example Application: Client The University of Adelaide, School of Computer Science 23 April 2018 Example Application: Client #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) { FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host; char buf[MAX_LINE]; int s; int len; if (argc==2) { host = argv[1]; } else { fprintf(stderr, "usage: simplex-talk host\n"); exit(1); Chapter 2 — Instructions: Language of the Computer

Example Application: Client The University of Adelaide, School of Computer Science 23 April 2018 Example Application: Client /* translate host name into peer’s IP address */ hp = gethostbyname(host); if (!hp) { fprintf(stderr, "simplex-talk: unknown host: %s\n", host); exit(1); } /* build address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); sin.sin_port = htons(SERVER_PORT); /* active open */ if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk: socket"); if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("simplex-talk: connect"); close(s); /* main loop: get and send lines of text */ while (fgets(buf, sizeof(buf), stdin)) { buf[MAX_LINE-1] = ’\0’; len = strlen(buf) + 1; send(s, buf, len, 0); Chapter 2 — Instructions: Language of the Computer

Example Application: Server The University of Adelaide, School of Computer Science 23 April 2018 Example Application: Server #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdlib.h> #include <strings.h> #include <string.h> #define SERVER_PORT 5432 #define MAX_PENDING 5 #define MAX_LINE 256 int main() { struct sockaddr_in sin; char buf[MAX_LINE]; int len; int s, new_s; /* build address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(SERVER_PORT); /* setup passive open */ if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk: socket"); exit(1); } Chapter 2 — Instructions: Language of the Computer

Example Application: Server The University of Adelaide, School of Computer Science 23 April 2018 Example Application: Server if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) { perror("simplex-talk: bind"); exit(1); } listen(s, MAX_PENDING); /* wait for connection, then receive and print text */ while(1) { if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) { perror("simplex-talk: accept"); while (len = recv(new_s, buf, sizeof(buf), 0)) fputs(buf, stdout); close(new_s); Chapter 2 — Instructions: Language of the Computer

Demo of Client and Server For now, use “localhost” for “host” when invoking the client “localhost” resolves to IP address 127.0.0.1, which is a shortcut for meaning “this machine” Can change this when more machines are in network

Introduction to Program 1 [Here I will introduce Program 1]

Program 1 Hints If you are not familiar with C, play with test files Download simpletalk textbook example program and play with it (step 1) Remember, change SERVER_PORT to your UNI ID number so no one else uses it at the same time!! Think about how you must modify the design to fit the program 1 specifications