C++ socket programming tutorial

client and server socket programming in c and client server communication in java using socket and define socket programming
VoiletFord Profile Pic
VoiletFord,United States,Professional
Published Date:12-07-2017
Your Website URL(Optional)
Comment
Introduction to Sockets Programming in C using TCP/IP Professor: Panagiota Fatourou TA: Eleftherios Kosmas CSD - May 2012‰‰‰‰‰ „„„„„ Introduction Computer Network Host hosts, routers, communication channels Router Hosts run applications Routers forward information Communication Packets: sequence of bytes channel contain control information e.g. destination host Protocol is an agreement meaning of packets structure and size of packets e.g. Hypertext Transfer Protocol (HTTP) CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 2‰‰‰‰‰ „)„„„ Protocol Families - TCP/IP Several protocols for different problems Protocol Suites or Protocol Families: TCP/IP TCP/IP provides end-to-end connectivity specifying how data should be formatted, addressed, transmitted, routed, and received at the destination can be used in the internet and in stand-alone private networks it is organized into layers CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 3TCP/IP FTP, SMTP, … Transport Layer TCP or UDP Network Layer IP Communication Channels image is taken from “http://en.wikipedia.org/wiki/TCP/IP_model” CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 4‰‰ „„„ Internet Protocol (IP) provides a datagram service packets are handled and delivered independently best-effort protocol may loose, reorder or duplicate packets each packet must contain an IP address of its destination CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 5‰‰ „„ Addresses - IPv4 The 32 bits of an IPv4 address are broken into 4 octets, or 8 bit fields (0-255 value in decimal notation). For networks of different size, the first one (for large networks) to three (for small networks) octets can be used to identify the network, while the rest of the octets can be used to identify the node on the network. Range of addresses 724 1.0.0.0 to Class A: 0 Network ID Host ID 127.255.255.255 14 16 128.0.0.0 to Class B: 1 0 Network ID Host ID 191.255.255.255 21 8 192.0.0.0 to Class C: 1 1 0 Network ID Host ID 223.255.255.255 28 224.0.0.0 to Class D (multicast): 1 1 1 0 Multicast address 239.255.255.255 27 240.0.0.0 to Class E (reserved): 1 1 1 1 0 unused 255.255.255.255 CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 6Local Area Network Addresses - IPv4 CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 7„ ‰‰)‰‰‰‰‰‰‰‰ „„„ TCP vs UDP Both use port numbers application-specific construct serving as a communication endpoint 16-bit unsigned integer, thus ranging from 0 to 65535 to provide end-to-end transport UDP: User Datagram Protocol no acknowledgements no retransmissions out of order, duplicates possible connectionless, i.e., app indicates destination for each packet TCP: Transmission Control Protocol reliable byte-stream channel (in order, all arrive, no duplicates) similar to file I/O flow control connection-oriented bidirectional CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 8„„„ TCP vs UDP TCP is used for services with a large data capacity, and a persistent connection UDP is more commonly used for quick lookups, and single use query-reply actions. Some common examples of TCP and UDP with their default ports: DNS lookup UDP 53 FTP TCP 21 HTTP TCP 80 POP3 TCP 110 Telnet TCP 23 CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 9‰ „„„„ Berkley Sockets Universally known as Sockets It is an abstraction through which an application may send and receive data Provide generic access to interprocess communication services e.g. IPX/SPX, Appletalk, TCP/IP Standard API for networking Application Application Socket Socket TCP TCP IP Channel IP Channel IP Router Host Host CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 10„„„ ‰‰‰‰‰‰‰ „„„ Sockets Uniquely identified by an internet address an end-to-end protocol (e.g. TCP or UDP) a port number Two types of (TCP/IP) sockets Descriptor Table internal data 0 Stream sockets (e.g. uses TCP) structure for file 1 1 provide reliable byte-stream service 2 Family: PF_INET Datagram sockets (e.g. uses UDP) Service: SOCK_STREAM provide best-effort datagram service Local_IP: Remote_IP: messages up to 65.500 bytes Local_Port: Socket extend the convectional UNIX I/O facilities Remote_Port: … file descriptors for network communication extended the read and write system calls CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 11Sockets Descriptor references Applications UDP sockets TCP sockets Sockets bound to ports TCP ports 65535 1 2 65535 UDP ports 1 2 TCP UDP IP CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 12Socket Programming CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 13‰‰‰‰‰ „„ Client-Server communication Server passively waits for and responds to clients passive socket Client initiates the communication must know the address and the port of the server active socket CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 14Sockets -Procedures CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 15Client - Server Communication -Unix Stream Datagram (e.g. TCP) (e.g. UDP) Server Client Server Client socket() socket() socket() socket() bind() bind() bind() listen() synchronization point accept() connect() recv() send() recvfrom() sendto() send() recv() sendto() recvfrom() close() close() close() close() CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 16„„„„„„ ‰‰‰‰‰ „) Socket creation in C: socket() int sockid = socket(family, type, protocol); sockid: socket descriptor, an integer (like a file-handle) family: integer, communication domain, e.g., PF_INET, IPv4 protocols, Internet addresses (typically used) PF_UNIX, Local communication, File addresses type: communication type SOCK_STREAM - reliable, 2-way, connection-based service SOCK_DGRAM - unreliable, connectionless, messages of maximum length protocol: specifies protocol IPPROTO_TCP IPPROTO_UDP usually set to 0 (i.e., use default protocol) upon failure returns -1 NOTE: socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 17Client - Server Communication -Unix Stream Datagram (e.g. TCP) (e.g. UDP) Server Client Server Client socket() socket() socket() socket() bind() bind() bind() listen() synchronization point accept() connect() recv() send() recvfrom() sendto() send() recv() sendto() recvfrom() close() close() close() close() CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 18‰‰‰‰ „„„ Socket close in C: close() When finished using a socket, the socket should be closed status = close(sockid); sockid: the file descriptor (socket being closed) status: 0 if successful, -1 if error Closing a socket closes a connection (for stream socket) frees up the port used by the socket CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 19„„) Specifying Addresses Socket API defines a generic data type for addresses: struct sockaddr unsigned short sa_family; / Address family (e.g. AF_INET) / char sa_data14; / Family-specific address information / Particular form of the sockaddr used for TCP/IP addresses: struct in_addr unsigned long s_addr; / Internet address (32 bits) / struct sockaddr_in unsigned short sin_family; / Internet protocol (AF_INET) / unsigned short sin_port; / Address port (16 bits) / struct in_addr sin_addr; / Internet address (32 bits) / char sin_zero8; / Not used / Important: sockaddr_in can be casted to a sockaddr CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 20