Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

CS 4700 / CS 5700 Network Fundamentals
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
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.
Socket Programming.
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.
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)
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
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.
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.
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.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
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,
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Remote Shell CS230 Project #4 Assigned : Due date :
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.
Distributed Computing A Programmer’s Perspective.
UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses
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
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
Socket Programming.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Today’s topic: UDP Reliable communication over UDP.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
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
CS 3700 Networks and Distributed Systems
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
CS 3700 Networks and Distributed Systems
Network Programming CSC- 341
Socket Programming in C
Server-side Programming CSE 333 Summer 2018
Internet Networking recitation #8
Presentation transcript:

Sockets Intro to Network Programming

Before the internet... Early computers were entirely isolated No Internet No network No model No external communications whatsoever

Before the internet... However, early computers did have many concepts we have already studied: File I/O Console I/O Shared memory Inner-process communication Solution: extend existing concepts to allow for remote communication

Before the internet... Solution: sockets A socket is nothing more than an end-point of a bidirectional inner-process communication flow. A socket, in a Linux operation system, is a file descriptor – the exact same thing you get form a call to open().

Sockets Sockets are often described as a door, gateway, or portal. The sender sends information through the portal. The receiver receives the information. To a programmer, the mechanism to get the information from the sender to receiver is abstracted away (TCP/IP, UDP/IP, etc).

Sockets Potential Problem: Many programs could possibly be communicating externally at once. Web browser loading a page client checking Streaming radio/music in the background …and a download going on.

Sockets Solution: UDP and TCP both provide ports. A port is a 16-bit number to describe which program on a computer the information is intended to be received by. Example: By default, web servers always listen for connections form clients on port 80. Written as: cs.illinois.edu:80

Sockets Bob Alice clientserver I am Bob port 80. I am accepting connections. Talk to Bob port 80. I will speak to Alice port 87. resulting TCP connection identified by (Alice:87, Bob:46)

Programming with sockets… To create a socket in C, you make a call to socket(). Definition: int socket(int domain, int type, int protocol); Alternatively, accept() will return a socket file descriptor (we’ll see this later).

Programming with sockets… Definition: int socket(int domain, int type, int protocol); domain: Specifies a communication domain (eg: IPv4 v. IPv6). type: Specifies what mechanism of communication is used (eg: stream v. datagram). protocol: Specifies what protocol should be used (eg: TCP v. UDP).

Programming with sockets… If you wish to allow for others to connect to your socket, you will need to bind() it to a port on the operating system. int bind(int socket, const struct sockaddr *address, socklen_t address_len);

Programming with sockets… Now that you’ve created your socket and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog);

Programming with sockets… Now that you’ve created your socket and bind()’d it to a port, you can listen for incoming connections! int listen(int socket, int backlog); backlog: defines the maximum length for the queue of pending connections. For most programs that will be constantly accept()’ing connections, a smallish value (5 or so) works great.

Programming with sockets… When you begin to listen() on your socket: …you’ve told the operating system to allow remote systems to connect at the specified port using the specified protocol. …you haven’t had a way to actually get a socket to communicate with that remote system.

Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); addr: The address of the system connect()ing to your system is written to the struct you pass in.

Programming with sockets… To receive a socket file descriptor for an incoming connection, you accept() the incoming connection. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); Return value: A new socket file descriptor to communicate with the remote system. Your original socket is still listening and more connections can be accept()’d.

Programming with sockets… If you’ve created a socket that you want to connect() rather than listen(), you simply create the socket and connect(). int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); (If you’re connect()’ing, you won’t want to listen() or bind() on that socket.)

A Simple Client-Server Program bind() listen() accept() read() write() close() write() read() connect() socket() close() Client Server

A Simple Client-Server Program In the diagram: read() write() close() …you’ll notice you’ve seen these calls before. These file I/O calls are socket I/O calls, too.

Programming with sockets… There are a few calls that are socket specific… ssize_t send(int socket, const void *buffer, size_t length, int flags); flags: Provides access to socket-specific parameters. A call to write() is equivalent to send() with no flags (eg: flags == 0).

Programming with sockets… For completeness, there are a whole set of send()/recv() calls: send(), sendto(), sendmsg() recv(), recvfrom(), recvmsg() (In CS 241, we won’t need any of the flag parameters.)

Server Initialization OS 1. socket() Web Server 2. bind(80) 3. listen() 80 Listen queue

Connecting to the Server OS 1. socket() Web Server 2. bind(80) 3. listen() 80 Listen queue Client connect() Request from (IP, port)

Busy Server Operation OS Web Server 80 Listen queue Client 1Client 3Client 2 Client requests get queued-up in the listen queue First-come first- served

The accept() Call OS Web Server 80 Listen queue Client 1Client 3Client 2 Client requests get queued-up in the listen queue First-come first- served accept() Connected socket

A final note... You now know the calls to use sockets, but you’ll find you need one more set of calls…

A final note... Various different architectures store 16- bit and 32-bit numbers differently (big- endian v. little-endian). When configuring port numbers and IP addresses, we need to talk in an architecture-independent way.

A final note... In C, functions are provided to convert between your local architecture and network architecture. htonl(): Converts a long from host to network byte order. htons(): Converts a short from host to network byte order. …and the inverse: ntohl() and ntohs().

A final note... You’ll often find code: ip.offset = ntohs(ip.offset); ip.length = ntohs(ip.length); ip.id = ntohs(ip.id);

Speaking of code... We’ll take a look at actual C code to run a client/server in section tomorrow. Tomorrow will be the very last section of CS 241 (next Thursday is reading week).

CS 241 At this point, we’ve covered every topic in CS 241! Congratulations! Remember: Prof. Caccamo will be back on Monday HW #2: Due Monday Solutions will be discussed during Monday’s lecture MP #7: Due on SVN on Wednesday Final Exam: Dec. 18