Zhu Reference: Daniel Spangenberger 15-441 Computer Networks, Fall 2007 PPT-4 Socket Programming.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Sop, Fan Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Socket Programming Computer Networks, Spring 2008 Xi Liu.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
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.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Daniel Spangenberger Computer Networks, Fall 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
UNIX Socket Programming CS 6378
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.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Sockets The building blocks of Internet communications.
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 Lab 1 1CS Computer Networks.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
UNIX Internet Socket API
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
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.
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.
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.
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.
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.
CS 3700 Networks and Distributed Systems
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
CS 3700 Networks and Distributed Systems
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
TCP Sockets Programming
Sockets Programming Socket to me!.
Computer Networks, Spring 2013
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming

Goal of Networking: Communication  Share data  Pass Messages Say I want to talk to a friend in Singapore…  How can I do this?  What applications and services must I use?  Where can I access them?  How will the data get there?  Will it be reliable?

Motivations for Sockets What’s in a Socket? Working with Sockets Concurrent Network Applications

Application Presentation Session Transport Network Data Link Physical Application Presentation Session Transport Network Data Link Physical Network Data Link Physical Core Network End-to-end Transparency Application IP TCP/ UDP WiFi

Let’s consider project one… Client (mIRC) :6262 Client (mIRC) :6262 Server (IRC) :6667 Server (IRC) :6667 TCP IP Packet #1 IP Packet #2 IP Packet #3 IP Packet #4 Ethernet Frame #1 Ethernet Frame #2 Ethernet Frame #3 Ethernet Frame #4 Which Is also…

An application programmer (writing an IRC server)  Doesn’t need to send IP packets  Doesn’t need to send Ethernet frames  Doesn’t need to worry about reliability  Shouldn’t have to! Sockets do this!  TCP streams  UDP packetized service (Project 2) You’ll be doing this! (using sockets)  To share data  To pass messages

Some information needed…  Where is the remote machine?  IP Address  Hostname (resolved to IP)  Which service do I want?  Port After that…  You get a file! A plain old file!  As simple as other Unix I/O  Don’t forget to close it when you’re done!

Request a socket descriptor  Both the client and the server need to  Bunch of kernel allocations… And the server…  Binds to a port  “I am offering a service on port x. Hear me roar”  Listens to the socket  “Hey! Say something!”  Accepts the incoming connection  “Good, you spoke up!” And the client…  Connects  “I’m interested!”

Server Client socket() connect() write() read() close() socket() bind() listen() accept() write() read() close() read() EOF Connection Request Client / Server Session

Both the client and server need to setup the socket  int socket(int domain, int type, int protocol) Domain  AF_INET (IPv4, also IPv6 available) Type  SOCK_STREAM TCP  SOCK_DGRAM UDP Protocol  0 int sockfd = socket(AF_INET, SOCK_STREAM, 0);

Server-only  int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); sockfd  A file descriptor to bind with, what socket returned! my_addr  It’s a struct (duh), describing an Internet socket/endpoint struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() }; struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() };

addrlen  sizeof(your sockaddr_in struct) struct sockaddr_in my_addr; int sockfd; unsigned short port = 80; if (0 > (sockfd = socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out my_addr.sin_family = AF_INET; // match the socket() call my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address my_addr.sin_port = htons(port); // specify port to listen on if((bind(sockfd, (struct sockaddr *) &my_addr, sizeof(saddr)) < 0) { printf(“Error binding\n”);... } struct sockaddr_in my_addr; int sockfd; unsigned short port = 80; if (0 > (sockfd = socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out my_addr.sin_family = AF_INET; // match the socket() call my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address my_addr.sin_port = htons(port); // specify port to listen on if((bind(sockfd, (struct sockaddr *) &my_addr, sizeof(saddr)) < 0) { printf(“Error binding\n”);... }

bind() takes a sockaddr C polymorphism  There’s a different sockaddr for IPv6!  And options for more in the future… struct sockaddr { short int sa_family;// “virtual pointer” char sa_data[14];// address info } struct sockaddr { short int sa_family;// “virtual pointer” char sa_data[14];// address info }

A little lesson on byte ordering…  Network byte ordering is defined to be big-endian  x86, x86-64 are little endian So how do we convert?  htons() / htonl() – Convert host order to network order  ntohs() / ntohl() – Convert network order to host order And what needs to be converted?  Addresses  Ports  Practically anything that deals with a network syscall  Maybe even data (up to the protocol designer)

Allows the server to listen for new connections  int listen(int sockfd, int backlog) sockfd  A file descriptor to listen on, what socket returned! backlog  The number of connections to queue listen(sockfd, 10);

The server must explicitly accept connections  int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); sockfd  A file descriptor to listen on, what socket returned! addr  Pointer to a sockaddr_in, cast as sockaddr* to store the client’s address information in addrlen  Pointer to an int to store the returned size of addr, should be initialized as sizeof(addr) int csock = accept(sockfd, (struct sockaddr_in *) &caddr, &clen);

struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if (0 > (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr))) { printf(“Error binding\n”);... } if (0 > listen(sockfd, 5)) { // listen for incoming connections printf(“Error listening\n”);... } clen = sizeof(caddr); if (0 > (isock = accept(sockfd, (struct sockaddr *) &caddr, &clen))) { printf(“Error accepting\n”);... } struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if (0 > (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr))) { printf(“Error binding\n”);... } if (0 > listen(sockfd, 5)) { // listen for incoming connections printf(“Error listening\n”);... } clen = sizeof(caddr); if (0 > (isock = accept(sockfd, (struct sockaddr *) &caddr, &clen))) { printf(“Error accepting\n”);... }

Client does not need to bind, listen, or accept Client needs only to socket and connect  int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen); connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));

struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } // looking up the hostname if (NULL == (h=gethostbyname(“ { printf(“Unknown host\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port); // specify port to connect to if (!connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) { printf(“Cannot connect\n”);... } struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) { printf(“Error creating socket\n”);... } // looking up the hostname if (NULL == (h=gethostbyname(“ { printf(“Unknown host\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port); // specify port to connect to if (!connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) { printf(“Cannot connect\n”);... }

But what now? Send data of course!  write()  read() Both are used by the client and the server To write and read  ssize_t read(int fd, void* buf, size_t len);  ssize_t write(int ffd, const void* buf, size_t len); read(sockfd, buffer, sizeof(buffer)); write(sockfd, “what’s up?\n”, strlen(“what’s up?\n”)); read(sockfd, buffer, sizeof(buffer)); write(sockfd, “what’s up?\n”, strlen(“what’s up?\n”));

Server Client socket() sendto() recvfrom() close() socket() bind() write() recvfrom() Client / Server Session close()

Server sets up a UDP socket and bind. Client sets up a UDP socket. No Listen No connection And now we are ready to send and receive data between each other! int sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(sockfd, my_addr, addrlen); int sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(sockfd, my_addr, addrlen);

ssize_t recvfrom(int sockfd, void *buffer, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, void *buffer, size_t nbytes, struct sockaddr *toaddr, socklen_t *addrlen); Sockfd Source address and dest address Flags: 0 recvfrom(sockfd, buffer, nbytes, 0, fromaddr, addrlen); sendto(sockfd, “Hi”, sizeof(“Hi”), toaddr, addrlen); recvfrom(sockfd, buffer, nbytes, 0, fromaddr, addrlen); sendto(sockfd, “Hi”, sizeof(“Hi”), toaddr, addrlen);

Server Client socket() connect() write() read() close() socket() bind() listen() accept() write() read() close() read() EOF Connection Request Client / Server Session

You must close() it!  Just like a file (since it appears as one!) What next? Loop around…  Accept new connections  Process requests  Close them  Rinse, repeat What’s missing here?