תקשורת באינטרנט 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.

Slides:



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

Socket Programming Application Programming Interface.
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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
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.
1 Example (UDP Client) // This program sends UDP packets to the given address #include #define SERVER_ADDR " " #define SERVER_PORT 5555 void error(char.
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.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
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.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Chapter 2 Applications and Layered Architectures Sockets.
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.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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 Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
Chapter 2 Applications and Layered Architectures Sockets.
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.
Introduction to Sockets
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.
UNIX Sockets Outline UNIX sockets CS 640.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
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)
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.
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Berkeley API Socket Programming
Socket Programming in C
Socket Programming.
Berkeley API Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Example (UDP Client) // This program sends UDP packets to the given address #include #include #include #include.
Internet Networking recitation #8
Berkeley API Socket Programming
Outline Communications in Distributed Systems Socket Programming
Sockets.
Presentation transcript:

תקשורת באינטרנט 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 u Service functions for sockets programming u Examples for Client - Server communication F STREAM Communication (TCP) F DATAGRAM Communication (UDP) u Port reservation u Example Contents

3 What is Sockets ? n First appeared in 4.1 BSD UNIX n Sockets: u is an abstraction used as communication endpoints. u is implemented as file descriptor with some state information stored in the socket library.

4 The Client Server model request reply

5 Example: UDP Echo n The simplest form of client-server interaction uses unreliable datagram delivery to convey messages from client to server and back. clientserver Waits for a datagram to arrive the echo port Reserve the source and destination addresses (IP addresses and ports) Return the datagram to its original sender Request sent to a well-known port The program become a UDP echo client when it allocates an unused UDP protocol port sends a UDP message to the UDP echo server Waits for the replay Response sent to the client’s port

6 Example: UDP Echo (Cont.) n The example illustrates two important points that are generally true about client-server interaction: u The difference between the lifetime of servers and clients. u The use of reserved and non-reserved port identifiers.

7 Sockets architecture Kernel User Application TCP/UDP layer IP (network) layer Socket API MAC layer

8 Type of Sockets n Socket has an associated type that determine the semantic of the communication : u SOCK_STREAM: connection oriented byte stream - TCP. u SOCK_DGRAM: unreliable, connectionless communication - UDP u and more

9 Protocol family for sockets functions n The family specifies the address family: group of protocols with the same address format n Example of address family constants: u AF_INET - IPv4 protocols u AF_INET6 - IPv6 protocols u AF_LOCAL (AF_UNIX) - Unix domain protocols

10 Data Structures struct sockaddr_in { short int sin_family; /* we use AF_INET */ unsigned short int sin_port; /* port number */ struct in_addr sin_addr; /* comp. address */ unsigned char sin_zero[8]; /* filled with 0s */ }; struct in_addr { u_long s_addr; /* unsigned long */ };

11 The Socket system calls n The Socket system calls: n int socket (int family, int type, int protocol) allocate a socket return: a socket descriptor or -1 family: communication domain type: type of socket- SOCK_STREAM, SOCK_DGRAM, SOCK_RAW protocol: particular protocol to use - 0 n int bind (int sd, struct sockaddr *addr, size_t addrlen) Bind a name to a socket return: 0 on success or -1 sd: socket descriptor (from the socket() system call) addr: pointer to the socket address addrlen: socket address length n int connect (int sd, const struct sockaddr *addr, size_t addrlen) Try to connect to a socket (done by the client) return: 0 on success or -1 sd: socket descriptor (from the socket() system call) addr: pointer to the socket address addrlen: socket address length

12 The Socket system calls (cont) n int listen (int sd, int backlog) a queue for incoming connections is activated using listen() return: 0 on success or -1 sd: socket descriptor (from the socket() system call) backlog : Maximum queue length of processes waiting for connection. n int accept (int sd, struct sockaddr *addr, size_t *addrlen); Wait for a connection request (done by the server) return: new socket descriptor or -1 in case of failure sd: socket descriptor (from the socket() system call) addr: pointer to the socket address addrlen: socket address length. n int close (int sd) Close the socket or the connection with the remote host. sd: socket descriptor (from the socket() system call)

13 Data Transfer - send u int send (int sd, char *buf, int buf_len) transmit a message to another socket. send() can be used only when the socket is in a connected state. return: the number of bytes that has been delivered or -1. sd: socket descriptor (from the socket() system call) buf: the message you send. buf_len: the data length of buf u int sendto (int sd, char *buf, int buf_len, const struct sockaddr *to_addr, size_t to_len) used to transmitting data to remote host return: the number of bytes that has been delivered or -1. sd: socket descriptor (from the socket() system call) buf: a pointer to the data location buf_len: the data length to_addr: the socket address of the destination. to_len: the “to_addr” structure length.

14 Data Transfer - receive u recv (int sd, char *buf, int bub_len) Obtain data from the socket. Used only when the socket is in a connected state. return: the number of bytes at the obtain input or -1. sd: socket descriptor (from the socket() system call) buf: the buffer address (in memory) for holding the received data. buf_len: the buffer length u int recvfrom (int sd, char *buf, int bub_len, struct sockaddr *from_addr, int from_len) Obtain data from the socket. return: the number of bytes at the obtain input or -1. sd: socket descriptor (from the socket() system call) buf: the buffer address (in memory) for holding the received data. buf_len: the buffer length from_addr: the socket address of the transmitter from_len: the “from_addr” structure length

15 Service functions for sockets programming n Byte ordering u short htons(short) - convert 16-bit value from host to network u long htonl(long) - convert 32-bit value from host to network u short ntohs(short) - convert 16-bit value from network to host u long ntohl(long) - convert 32-bit value from network to host n Deal with IP address u ina.sin_addr.s_addr = inet_addr(" ") u returns the address in Network Byte Order already. n DNS u int gethostname(char* name, int namelen) return local host name u struct hostent *gethostbyname(char* name) use to get computer address by the name. u struct hostent *gethostbyaddr(char *addrp, int len, int type)

16 From Beej's Guide to Network Programming n socket()--Get the File Descriptor! n bind()--What port am I on? n connect()--Hey, you! n listen()--Will somebody please call me? n accept()--"Thank you for calling port 3490." n send() and recv()--Talk to me, baby! n sendto() and recvfrom()--Talk to me, DGRAM-style n close() and shutdown()--Get outta my face!

17 Stream Sockets TCP ClientTCP Server sd = socket(…) bind(sd,port) connect(sd,dest) send(sd,…) recv(sd,…) close(sd) sd = socket(…) bind(sd,port) listen(sd) new_sd = accept(sd) recv(new_sd,…) send(new_sd,…) close(new_sd,...)

18 Datagram Sockets UDP Client UDP Server sd = socket(…) sendto(sd,…) recvfrom(sd,…) close(sd) sd = socket(…) bind(sd,port) recvfrom(sd,…) sendto(sd,…)

19 Port reservation n Ports 0 through 1023 are reserved. n Other ports are used by system for assignment to clients.

20 Example (UDP Client) // This program sends UDP packets to the given address #include #define SERVER_ADDR " " #define SERVER_PORT 5555 void error(char *str) { printf("\n%s", str); exit(0); } int main(int argc, char *argv[]) { char message[100], message2[10]; int sockfd, res; struct sockaddr_in client_addr, server_addr; int i, mesNum; printf("\nClient is running..."); if (argc < 2) error("\nYou should supply parameter: the number of messages to send");

21 Example (UDP Client) // Opening socket if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) error("Could not open socket"); // Sending a message to the server bzero((char*) &server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR); server_addr.sin_port = htons(SERVER_PORT); mesNum = atoi(argv[1]); if (mesNum == 0) error("\nIllegal parameter"); for (i=0; i<mesNum; i++) { strcpy(message, "Test message: "); sprintf(message2, "%d", i+mesNum); strcat(message, message2); res = sendto (sockfd, message, strlen(message)+1, 0, (struct sockaddr*)&server_addr, sizeof(server_addr)); printf("\nClient sent %d bytes", res); }

22 Example (UDP Server) // This program receives UDP packets #include #define SERVER_PORT 5555 #define MAX_MESSAGE_SIZE 100 void error(char *str) { printf("\n%s", str); exit(0); } int main() { char message[MAX_MESSAGE_SIZE]; int sockfd, res; struct sockaddr_in client_addr, server_addr; int addr_len; printf("\nServer is running..."); fflush(stdout);

23 Example (UDP Server) // Opening socket if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) error("Could not open socket"); // Bind local ip and process addresses bzero((char*) &server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(SERVER_PORT); if ( bind (sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) error("Could not bind to the socket"); while (1) { // Receiving a message from the client addr_len = sizeof(client_addr); res = recvfrom (sockfd, message, MAX_MESSAGE_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len); printf("\nServer received %d bytes", res); printf("\n%s", message); fflush(stdout); }