Network Programming Using Internet Sockets Wu Xuanxuan Lai Xinyu 吴璇璇 赖新宇

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

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()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
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.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
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.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
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.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
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.
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.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
1 Sockets The building blocks of Internet communications.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
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,
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.
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.
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.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
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 in C
Network Programming with Sockets
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Socket Programming in C
TCP/IP Socket Programming in C
Socket Programming(1/2)
Sockets Programming Socket to me!.
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:

Network Programming Using Internet Sockets Wu Xuanxuan Lai Xinyu 吴璇璇 赖新宇

Agenda Basic Socket Programming – Understanding Sockets – Socket API – Structs and Data Handling – A simple example Assignment References

Basic Socket Programming What is a Socket – Definition 1: A door between application process and end-end transport protocol(TCP/UDP) – Definition 2: A way to speak to other programs using standard UNIX file descriptors. – Definition 3: An endpoint in communication

Definition 1: a door between application process and end-end transport protocol (TCP/UDP) Basic Socket Programming Understanding Sockets- Definition 1

controlled by app developer Controlled by OS You write sockets programs without knowing how the lower levels‘ protocols works! loosely-coupled

Basic Socket Programming Understanding Sockets- Definition1 Socket API: (1) choice of transport protocol; (2) ability to fix a few parameters Two types of transport service via Internet socket API: – Stream Sockets (connection-oriented ) They use TCP (Transmission Control Protocol) protocol – Reliable stream – Datagram Sockets (connectionless) They use UDP (User Datagram Protocol) protocol – Unreliable datagram

Definition2: a way to speak to other programs using standard Unix file descriptors. Basic Socket Programming Understanding Sockets- Definition 2

Socket Programming int sockfd; /*socket descriptor*/ sockfd= socket( AF_INET, SOCK_STREAM, 0); read( sockfd, … ); File Operation char buf[100]; int fd;/*file descriptor*/ fd= open(“foo", O_RDONLY); read( fd, buf, num_bytes); Basic Socket Programming Understanding Sockets- Definition2

Definition3: merely an endpoint in communication. Basic Socket Programming Understanding Sockets- Definition 3

– IP : User needs to know the IP ADDRESS of the server. – Port number : On the server, many processes are running. We want to “talk” to the right one. The knowledge of the IP addresses and the Port Number define uniquely a communication endpoint.

Basic Socket Programming Socket API-TCP Model

Basic Socket Programming Socket API-UDP Model Server: No bind() and accept(). Client: No connect().

Basic Socket Programming Socket API-socket() socket() -Get the File Descriptor! – The first step is to require the OS to reserve one “descriptor” for the communication channel. int sockfd = socket(AF_INET, socket_type, 0); /*socket_type=SOCK_STREAM|SOCK_DGRAM*/ /*AF_INET means we use IP (version 4)*/ /*0 is for IP (not ICMP or others)*/ /*return a socket descriptor or -1 when error*/ Protocol Family Symbolic Name TCP/IP Internet AF_INET Xerox NS AF_NS Intra-host Unix AF_UNIX DEC DNA AF_DECNET Service Type symbolic name datagram (UDP) SOCK_DGRAM reliable, in order (TCP) SOCK_STREAM raw socket SOCK_RAW If(sockfd < 0) perror(“can’t create socket");

Basic Socket Programming Socket API-bind() bind()-What IP address and port am I on? struct sockaddr_in servaddr; bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); Note: – Only the server need to call this API; – The type of the second parameter should be sockaddr while we often use sockaddr_in in the program——Type Conversation

close(sockfd); – frees up any memory associated with the socket shutdown(sockfd, how); /* how = 0 -> disable receptions */ /* how = 1 -> disable transmission */ /* how = 2 -> the same as close() */ Basic Socket Programming Socket API-close(),shutdown()

Basic Socket Programming TCP API(SERVER)-listen() listen()-Who will call me? listen(sockfd, req_no); req_no: specifies the maximum number of client connections that the kernel will queue for this listening descriptor.

accept()-Thank you for calling port int new_sd, sockfd, size; struct sockaddr remote_addr; new_sd = accept(sockfd, &remote_addr, &size); – If the queue is empty, the process will be blocked. – If so, accept() returns a NEW SOCKET DESCRIPTOR ! – the old socket descriptor (sockfd) is still queuing request from the network ! – So, if we want to communicate with the client, we MUST use new_sd. – new_sd is a socket ready for the communication – It is suggested that handle new_sd in a child process or a thread. Basic Socket Programming TCP API(SERVER)-accept()

Basic Socket Programming TCP API(CLIENT)-connect() connect()-Hey, you! int sockfd; connect(sockfd, (sockaddr*) &servaddr, sizeof(servaddr) ); – On the client side, bind is done automatically – The local IP address is the one provided by default – A port “randomly” assigned by the operating system is good. – So, we put in servaddr the information on the server and try to connect to it. – Finish three-way handshake

Basic Socket Programming TCP API-send(), recv() send() and recv() -Talk to me, baby! int send(int sockfd, const void *msg, int len, int flags); int recv(int sockfd, void *buf, int len, unsigned int flags); char *msg = “Hi, baby!"; char buffer[SOME_SIZE]; int len, nset, nrecv;. len = strlen(msg); nset= send(sockfd, msg, len, 0);. nrecv = recv(sockfd, &buffer, len, flags)

Basic Socket Programming TCP API-read(), write() read() and write() –more choices read() = recv(*, *, *, 0) write() = send(*, *, *, 0) Programmers are familiar with, similar to file operations recv() and send() explicit meaning

Basic Socket Programming UDP API-sendto(), recvfrom() sendto() and recvfrom() i nt sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recvfrom(int sockfd, void *buf,int len,unsigned int flags, struct sockaddr *from, int *fromlen) – Send directly without handshake – The first four parameters are the same with send/recv – to/from is a pointer to a struct sockaddr which contains the destination IP address and port. – tolen/fromlen, can simply be set to sizeof(struct sockaddr).

Basic Socket Programming Structs and Data Handling-IP address IP Address – Every host has a unique IP Address – 32bits Three forms – hostname (string)e.g. localhost – dotted decimal(string)e.g – binary(u_long) Dealing with them inet_addr() : dotted decimal to binary gethostbyname() : hostname to binary e.g. servaddr.sin_addr.s_addr =inet_addr(“ ");

Basic Socket Programming Structs and Data Handling-Port Port – 1-255reserved for standard services – 21ftp – 23telnet – 25SMTP – 80HTTP – Available only to priviledged users – Usuable by system and user processes – 5000-Usuable by user processes only

Basic Socket Programming Structs and Data Handling-struct sock_addr struct sockaddr : This structure holds socket address information for many types of sockets: struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14];// 14 bytes of protocol address }; sockaddr is the structure with the addresses and the ports. We put IP address and the Port in sa_data[14]. Not convenient to deal with?

Basic Socket Programming Structs and Data Handling-struct sockaddr_in struct sockaddr_in: To deal with sockaddr, programmers created a structure: struct sockaddr_in ("in" for "Internet".) struct sockaddr_in { short int sin_family; // Address family unsigned short int sin_port; // Port number struct in_addr sin_addr;// Internet address unsigned char sin_zero[8]; // Same size as struct sockaddr }; struct in_addr{ /* 32-byte IP Address *./ in_addr_t s_addr; }; We can work with sockaddr_in and cast it to sockaddr. struct sockaddr_in servaddr; bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

Basic Socket Programming Structs and Data Handling Convert the Natives! Know this: – there are two byte orderings most significant byte first-“Network Byte Order” (NBO). or least significant byte first-"Host Byte Order” (HBO). – htons() host to network short – htonl() host to network long – ntohs() network to host short – ntohl() network to host long e.g. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);/*IP address of localhost*/ servaddr.sin_port = htons(13);

Basic Socket Programming Structs and Data Handling And here's a sample, while packing a struct sockaddr_in /* an Internet endpoint address*/ struct sockaddr_in my_addr; my_addr.sin_family = AF_INET;/* host byte order */ my_addr.sin_port = htons(MYPORT); /* short, NBO*/ my_addr.sin_addr.s_addr = inet_addr(" "); bzero(&(my_addr.sin_zero)); /* zero the rest of the struct */

Basic Socket Programming a simple example-TCP server

Basic Socket Programming a simple example-TCP client

Basic Socket Programming a simple example - Result

P2P Principle login search share logout ping download P2P Server Peer Client Programming Assignment

Assignment Phase1: Establishing Client-Server Communications (Mandatory; 60 points) Phase2: Establishing Peer-Peer Communications (Mandatory; 40 points Optional; 20 points)

Phase1: Establishing Client-Server Communications -- Mandatory 1) Authenticate with the server (provide a username and encrypted password) 2) Send a list of files to the server that you wish to share with other users 3) Submit a search query to the server for a file you wish to download 4) Receive the search results, parse them, and output them to the user 5) Log out

Phase1: Authenticate Protocol packet packet head is 4 bytes!

Phase1: Share Protocol packet:

Phase1: Search Protocol packet:

Phase1: Logout Protocol packet:

Phase2: Establishing Peer-Peer Communications 1) Add functionality to send "ping" messages over UDP and listen for echo responses, in order to estimate the round trip time to another peer. -- Mandatory 2) Add functionality to respond to ping messages from other peers.-- Mandatory 3) Add functionality to connect to another peer and download a file. 4) Add functionality to make your peer a file server, so that other peers may connect and download files from you.

Phase2: Ping Send UDP Ping Message. Protocol packet: Do not guarantee delivery of packets! Use timeout control—select()

Phase2: Peer-Peer Communications Protocol packet: No need password!

Phase2: Peer-Peer Communications (cont...) Protocol packet:

Requirement Program Correctness and Functionality: 60% Code design & Program Structure: 10% Documentation: 30%

Deadline Mandatory : April 27 Optional: May 7 Submit to: For more detail: detailed requirements.pdf

References Warren W. Gay, “Linux Socket Programming by Example” Beej's Guide to Network Programming Using Internet Sockets Douglas E. Comer, David L. Stevens, “Internetworking With TCP/IP Vol III” Thank you!