Computer Networks LECTURE 2 Performance and Socket Programming

Computer Networks LECTURE 2 Performance and Socket Programming

Computer Networks Assignments LECTURE 3 • Lab 2 – socket programming Socket Programming and – DUE: Wednesday September 14th Application Layer Protocols Sandhya Dwarkadas Department of Computer Science University of Rochester Computer Networks: Principles Why layering? and Architecture dealing with complex systems: • Network Architecture: • explicit structure allows identification, relationship of – Design and Implementation Guide complex system’s pieces Principle of Abstraction – layering of protocols – layered reference model for discussion • modularization eases maintenance, updating of system A protocol provides a communication service to – change of implementation of layer’s service transparent to rest of system the next higher-level layer – e.g., change in gate procedure doesn’t affect rest of system • Service interface – e.g., send and receive • layering considered harmful? • Peer interface – form and meaning of messages exchanged between peers Introduction 1-4 1 Internet protocol stack Socket programming • application: supporting network applications goal: learn how to build client/server – FTP, SMTP, HTTP application applications that communicate using sockets • transport: process-process data transfer transport socket: door between application process and – TCP, UDP end-end-transport protocol application • network: routing of datagrams network application socket controlled by from source to destination process process app developer – IP, routing protocols link transport transport network network controlled • link: data transfer between by OS link Internet link neighboring network elements physical physical physical – Ethernet, 802.111 (WiFi), PPP • physical: bits “on the wire” Introduction 1-5 Application Layer 2-6 Operating Systems Support: Socket Addresses Sockets #include <sys/types.h> • A socket is an operating system abstraction #include <sys/socket.h> in which a port is embedded #include <netinet/in.h> • A port is a communication endpoint struct sockaddr{ u_short sa_family; /*address family: AF_xxx value*/ char sa_data[14];/* up to 14 bytes of addr */ Host, port Host, port }; /* (protocol-specific) */ struct sockaddr_in { short sin_family; /* AF_INET */ u_short sin_port; /* 16-bit port number */ Process socket Process struct in_addr sin_addr; /* 32-bit netid/hostid */ socket char sin_zero[8];/* unused */ }; /*sin_port and sin_addr are network byte ordered*/ 2 Network Byte Order Socket System Call • Two ways to map byte addresses onto words: – Little Endian byte ordering: Intel 80x86, DEC VAX • Creates an endpoint for communication int socket(int family, int type, int protocol); – Big Endian byte ordering: IBM 360/370, Motorola 68K, MIPS, SPARC, HPPA – Family (or domain): AF_UNIX, AF_INET, AF_NS, AF_IMPLINK – type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ... • Network byte order (TCP/IP, XNS, SNA – protocol: typically 0 protocols): big-endian in protocol headers • Byte ordering/alignment routines: htonl, htons, – Returns a socket descriptor, or sockfd ntohl, ntohs Services Provided by the Transport Layer • Methods by which socket options may be • Connection-oriented (virtual circuit) versus changed connection-less (datagram) protocols – setsockopt • Sequencing – fcntl • Error control – ioctl • Flow control • Byte stream vs. messages • Full duplex vs. half duplex 3 • Connection-oriented (virtual circuit) – Establish a connection – Transfer data – Terminate connection • Connection-less (datagram) – Each message or datagram transmitted independently – must contain all information for delivery Socket programming Two socket types for two transport services: • Operations: – UDP: unreliable datagram – fd = socket(): creates the socket (think OS – TCP: reliable, byte stream-oriented metadata to manage communication endpoint) Application Example: – bind(fd, port): binds socket to local port 1. client reads a line of characters (data) from its (address) keyboard and sends data to server 2. server receives the data and converts characters – sendto(), recvfrom(), write(), read(): operations to uppercase for sending and receiving data 3. server sends modified data to client – close(fd): close the connection 4. client receives modified data and displays line on its screen Application Layer 2-15 4 Socket programming with UDP Datagram Protocol: UDP UDP: no “connection” between client & server • no handshaking before sending data • sender explicitly attaches IP destination address and port # to each packet • receiver extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: • UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Application Layer 2-17 Client/server socket interaction: UDP Example app: UDP client Python UDPClient on serverIP server (running ) client include Python’s socket library from socket import * create socket: serverName = ‘hostname’ create socket, port= x: clientSocket = serverPort = 12000 serverSocket = socket(AF_INET,SOCK_DGRAM) socket(AF_INET,SOCK_DGRAM) clientSocket = socket(AF_INET, create UDP socket for Create datagram with server IP and server SOCK_DGRAM) port=x; send datagram via get user keyboard message = raw_input(’Input lowercase sentence:’) read datagram from clientSocket input clientSocket.sendto(message.encode(), serverSocket Attach server name, port to (serverName, serverPort)) message; send into socket write reply to modifiedMessage, serverAddress = serverSocket read datagram from read reply characters from clientSocket.recvfrom(2048) specifying clientSocket socket into string print modifiedMessage.decode() client address, port number close clientSocket.close() print out received string clientSocket and close socket Application Layer 2-20 Application 2-19 5 Example app: UDP server Socket programming with TCP Python UDPServer client must contact server • when contacted by client, from socket import * • server process must first be server TCP creates new serverPort = 12000 running socket for server process to serverSocket = socket(AF_INET, SOCK_DGRAM) create UDP socket • server must have created communicate with that serverSocket.bind(('', serverPort)) bind socket to local port socket (door) that welcomes particular client number 12000 print (“The server is ready to receive”) client’s contact – allows server to talk with while True: multiple clients loop forever message, clientAddress = serverSocket.recvfrom(2048) client contacts server by: – source port numbers used Read from UDP socket into modifiedMessage = message.decode().upper() • Creating TCP socket, to distinguish clients message, getting client’s serverSocket.sendto(modifiedMessage.encode(), address (client IP and port) specifying IP address, port clientAddress) application(more inviewpoint: Chap 3) send upper case string number of server process TCP provides reliable, in-order back to this client • when client creates socket: byte-stream transfer (“pipe”) client TCP establishes between client and server connection to server TCP Application Layer 2-21 Application Layer 2-22 Socket System Calls for Connection-Oriented System Connection-Oriented Protocol: TCP Calls • Connect: Establishes a connection with a server – includes bind - assigns 4 elements of the 5-tuple • Listen: Indicates that server is willing to accept connections (allows queueing) • Accept: Waits for actual connection from client process – creates a new socket descriptor – assumes a concurrent server 6 Client/server socket interaction: TCP Example app: TCP client server (running on hostid) client Python TCPClient create socket, from socket import * port=x, for incoming serverName = ’servername’ request: serverSocket = socket() serverPort = 12000 create TCP socket for server, remote port 12000 clientSocket = socket(AF_INET, SOCK_STREAM) wait for incoming TCP create socket, connection request connect to hostid, port=x clientSocket.connect((serverName,serverPort)) connection setup connectionSocket = clientSocket = socket() sentence = raw_input(‘Input lowercase sentence:’) serverSocket.accept() clientSocket.send(sentence.encode()) send request using No need to attach server read request from clientSocket modifiedSentence = clientSocket.recv(1024) name, port connectionSocket print (‘From Server:’, modifiedSentence.decode()) write reply to clientSocket.close() connectionSocket read reply from clientSocket close connectionSocket close clientSocket Application Layer 2-25 Application Layer 2-26 Example app: TCP server Python TCPServer Client-Server Model from socket import * serverPort = 12000 create TCP welcoming socket serverSocket = socket(AF_INET,SOCK_STREAM) • Iterative versus concurrent servers serverSocket.bind((‘’,serverPort)) server begins listening for serverSocket.listen(1) • Role of client and server asymmetric incoming TCP requests print ‘The server is ready to receive’ while True: loop forever connectionSocket, addr = serverSocket.accept() server waits on accept() for incoming requests, new sentence = connectionSocket.recv(1024).decode() socket created on return capitalizedSentence = sentence.upper() read bytes from socket (but connectionSocket.send(capitalizedSentence. not address as in UDP) encode()) close connection to this connectionSocket.close() client (but not welcoming socket) Application Layer 2-27 7 I/O Multiplexing Select System Call • Methods by which I/O may be multiplexed • Waits on several socket descriptors – Set socket to non-blocking and poll • int select(int nfds, fd_set *readfds, fd_set • fcntl(fd, F_SETFL, FNDELAY), where

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us