Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
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.
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.
Yu-Chi Lai Lecture 3 Network Programming CS 640: Computer Networking.
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.
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.
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.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Computer Systems II CSC 2405 Network Programming.
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.
1 Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
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 :
The Pocket Guide to TCP/IP Sockets: C Version Michael J. Donahoo Kenneth L. Calvert.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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
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.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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
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.
回到第一頁 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.
Berkeley Sockets. Client-server model Sockets interface: Address structure, byte ordering, port no Socket primitives: socket, bind,listen… Example code.
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.
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.
UNIX Sockets COS 461 Precept 1.
The Pocket Guide to TCP/IP Sockets: C Version
Socket Programming in C
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Socket Programming in C
Socket 程式設計.
Yu-Chi Lai Lecture 3 Network Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Presentation transcript:

Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking

Client-server model Sockets interface Socket primitives Example code for echoclient and echoserver Debugging With GDB Programming Assignment 1 (MNS) Topics

Client/sever model Client asks (request) – server provides (response) Typically: single server - multiple clients The server does not need to know anything about the client –even that it exists The client should always know something about the server –at least where it is located Client process Server process 1. Client sends request 2. Server handles request 3. Server sends response 4. Client handles response Resource Note: clients and servers are processes running on hosts (can be the same or different hosts).

The interface that the OS provides to its networking subsystem application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) physical layer application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) physical layer OS network stack Sockets as means for inter-process communication (IPC) Client Process Server Process Socket OS network stack Socket Internet

Internet Connections (TCP/IP) Connection socket pair ( :3479, :80) Server (port 80) Client Client socket address :3479 Server socket address :80 Client host address Server host address Address the machine on the network –By IP address Address the process –By the “port”-number The pair of IP-address + port – makes up a “socket-address” Note: 3479 is an ephemeral port allocated by the kernel Note: 80 is a well-known port associated with Web servers

Clients Examples of client programs –Web browsers, ftp, telnet, ssh How does a client find the server? –The IP address in the server socket address identifies the host –The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. –Examples of well known ports Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server

Using Ports to Identify Services Web server (port 80) Client host Server host Echo server (port 7) Service request for :80 (i.e., the Web server) Web server (port 80) Echo server (port 7) Service request for :7 (i.e., the echo server) Kernel Client

Servers Servers are long-running processes (daemons). –Created at boot-time (typically) by the init process (process 1) –Run continuously until the machine is turned off. Each server waits for requests to arrive on a well-known port associated with a particular service. –Port 7: echo server –Port 23: telnet server –Port 25: mail server –Port 80: HTTP server Other applications should choose between 1024 and See /etc/services for a comprehensive list of the services available on a Linux machine.

Sockets What is a socket? –To the kernel, a socket is an endpoint of communication. –To an application, a socket is a file descriptor that lets the application read/write from/to the network. Remember: All Unix I/O devices, including networks, are modeled as files. Clients and servers communicate with each by reading from and writing to socket descriptors. The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.

Socket Programming Cliches Network Byte Ordering –Network is big-endian, host may be big- or little-endian –Functions work on 16-bit (short) and 32-bit (long) values –htons() / htonl() : convert host byte order to network byte order –ntohs() / ntohl(): convert network byte order to host byte order –Use these to convert network addresses, ports, … Structure Casts –You will see a lot of ‘structure casts’ struct sockaddr_in serveraddr; /* fill in serveraddr with an address */ … /* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); …

Socket primitives SOCKET: int socket(int domain, int type, int protocol); –domain := AF_INET (IPv4 protocol) –type := (SOCK_DGRAM or SOCK_STREAM ) –protocol := 0 (IPPROTO_UDP or IPPROTO_TCP) –returned: socket descriptor (sockfd), -1 is an error BIND: int bind(int sockfd, struct sockaddr *my_addr, int addrlen); –sockfd - socket descriptor (returned from socket()) –my_addr: socket address, struct sockaddr_in is used –addrlen := sizeof(struct sockaddr) struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ };

LISTEN: int listen(int sockfd, int backlog); –backlog: how many connections we want to queue ACCEPT: int accept(int sockfd, void *addr, int *addrlen); –addr: here the socket-address of the caller will be written –returned: a new socket descriptor (for the temporal socket) CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); //used by TCP client –parameters are same as for bind() SEND: int send(int sockfd, const void *msg, int len, int flags); –msg: message you want to send –len: length of the message –flags := 0 –returned: the number of bytes actually sent RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags); –buf: buffer to receive the message –len: length of the buffer (“don’t give me more!”) –flags := 0 –returned: the number of bytes received

SEND (DGRAM-style): int sendto(int sockfd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen); –msg: message you want to send –len: length of the message –flags := 0 –to: socket address of the remote process –tolen: = sizeof(struct sockaddr) –returned: the number of bytes actually sent RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); –buf: buffer to receive the message –len: length of the buffer (“don’t give me more!”) –from: socket address of the process that sent the data –fromlen:= sizeof(struct sockaddr) –flags := 0 –returned: the number of bytes received CLOSE: close (socketfd);

Client+server: connectionless CREATEBINDSEND CLOSE RECEIVE

Client+server: connection-oriented Concurrent server SOCKET BINDLISTENCONNECTACCEPTRECEIVE SEND CLOSE TCP three-way handshake

EchoClient.c – #include’s #include /* for printf() and fprintf() */ #include /* for socket(), connect(), sendto(), and recvfrom() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #define ECHOMAX 255 /* Longest string to echo */

EchoClient.c -variable declarations int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ struct sockaddr_in fromAddr; /* Source address of echo */ unsigned short echoServPort =7; /* Echo server port */ unsigned int fromSize; /* address size for recvfrom() */ char *servIP=“ ”; /* IP address of server */ char *echoString=“I hope this works”; /* String to send to echo server */ char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */ int echoStringLen; /* Length of string to echo */ int respStringLen; /* Length of received response */

EchoClient.c - creating the socket and sending /* Create a datagram/UDP socket */ sock = socket(AF_INET, SOCK_DGRAM, 0); /* Construct the server address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet addr family */ echoServAddr.sin_addr.s_addr = htonl(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ /* Send the string to the server */ sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr); /* Recv a response */

EchoClient.c – receiving and printing fromSize = sizeof(fromAddr); recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &fromAddr, &fromSize); /* Error checks like packet is received from the same server*/ /* null-terminate the received data */ echoBuffer[echoStringLen] = '\0'; printf("Received: %s\n", echoBuffer); /* Print the echoed arg */ close(sock); exit(0); } /* end of main () */

EchoServer.c int main(int argc, char *argv[]) { int sock; /* Socket */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned int cliAddrLen; /* Length of incoming message */ char echoBuffer[ECHOMAX]; /* Buffer for echo string */ unsigned short echoServPort =7; /* Server port */ int recvMsgSize; /* Size of received message */ /* Create socket for sending/receiving datagrams */ sock = socket(AF_INET, SOCK_DGRAM, 0); /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(“ ”); echoServAddr.sin_port = htons(echoServPort); /* Local port */ /* Bind to the local address */ bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr);

for (;;) /* Run forever */ { cliAddrLen = sizeof(echoClntAddr); /* Block until receive message from a client */ recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen); printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); /* Send received datagram back to the client */ sendto(sock, echoBuffer, recvMsgSize, 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr); } } /* end of main () */ Error handling is must

Socket Programming Help man is your friend –man accept –man sendto –Etc. The manual page will tell you: –What #include<> directives you need at the top of your source code –The type of each argument –The possible return values –The possible errors (in errno)

Debugging with gdb Prepare program for debugging –Compile with “ -g ” (keep full symbol table) –Don’t use compiler optimization (“ -O ”, “ –O2 ”, …) Two main ways to run gdb –On program directly gdb progname Once gdb is executing we can execute the program with: –run args –On a core (post-mortem) gdb progname core Useful for examining program state at the point of crash Extensive in-program documentation exists –help (or help or help )

More information… Socket programming –W. Richard Stevens, UNIX Network Programming –Infinite number of online resources – GDB –Official GDB homepage: –GDB primer: