Example: barber

Introduction to Sockets Programming in C using TCP/IP

Introduction to Sockets Programming in C using TCP/IPProfessor: Panagiota FatourouTA: Eleftherios KosmasCSD - May 2012 IntroductionCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 2 Computer Network hosts, routers, communication channels Hostsrun applications Routersforward information Packets: sequence of bytes contain control information destination host Protocolis an agreement meaning of packets structure and size of Hypertext Transfer Protocol (HTTP)HostRouterCommunication channelProtocol Families - TCP/IPCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 3 Several protocols for different problems)Protocol Suites or Protocol Families.

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

Tags:

  Introduction, Programming, Sockets, Datagrams, Introduction to sockets programming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Introduction to Sockets Programming in C using TCP/IP

1 Introduction to Sockets Programming in C using TCP/IPProfessor: Panagiota FatourouTA: Eleftherios KosmasCSD - May 2012 IntroductionCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 2 Computer Network hosts, routers, communication channels Hostsrun applications Routersforward information Packets: sequence of bytes contain control information destination host Protocolis an agreement meaning of packets structure and size of Hypertext Transfer Protocol (HTTP)HostRouterCommunication channelProtocol Families - TCP/IPCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 3 Several protocols for different problems)Protocol Suites or Protocol Families.

2 TCP/IP TCP/IP provides end-to-endconnectivity 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 layersTCP/IP* image is taken from *Communication ChannelsNetwork Layer IPTransport Layer TCP or UDPFTP, SMTP, ..CS556 - Distributed SystemsTutorial by Eleftherios Kosmas 4 Internet Protocol (IP) provides a datagramservice packets are handled and delivered independently best-effortprotocol may loose, reorder or duplicate packets each packet must contain an IP address of its destinationCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 5 Addresses - IPv4724 Class A:0 Network IDHost ID1416 Class B:1 0 Network IDHost ID218 Class C:1 1 0 Network IDHost ID28 Class D (multicast):1 1 1 0 Multicast address27 Class E (reserved).

3 1 1 1 to to to to to of addresses The 32bits 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 nodeon the -Distributed SystemsTutorial by Eleftherios Kosmas 6 Local Area Network Addresses - IPv4CS556 - Distributed SystemsTutorial 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.

4 User Datagram Protocol no acknowledgements no retransmissions out of order, duplicates possible connectionless, , 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 bidirectionalCS556 - Distributed SystemsTutorial 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.

5 DNS lookupUDP53 FTPTCP21 HTTPTCP80 POP3 TCP110 TelnetTCP23CS556 -Distributed SystemsTutorial 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 accessto interprocesscommunication services IPX/SPX, Appletalk, TCP/IP Standard API for networkingHostApplicationSocketTCPIPHost ApplicationSocketTCPIPR outerIPChannelChannelCS556 -Distributed SystemsTutorial by Eleftherios Kosmas 10 Sockets Uniquely identified by an internet address an end-to-end protocol ( TCP or UDP) a port number Two types of ( TCP/IP ) Sockets Streamsockets ( uses TCP) provide reliable byte-stream service Datagramsockets ( uses UDP) provide best-effort datagram service messages up to bytes Socket extend the convectional UNIX I/O facilities file descriptors for network communication extended the read and write system calls012internal data structure for file 1 Family: PF_INETS ervice: SOCK_STREAML ocal_IP:Remote_IP:Remote_Port.

6 Local_Port: Descriptor TableCS556 -Distributed SystemsTutorial by Eleftherios Kosmas 11 SocketsTCPUDPIP12655351265535 ApplicationsTCP socketsUDP socketsTCP portsUDP portsDescriptor referencesSockets bound to portsCS556 -Distributed SystemsTutorial by Eleftherios Kosmas 12 Socket ProgrammingCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 13 Client-Server communication Server passively waits for and responds to clients passivesocket Client initiates the communication must know the address and the port of the server activesocketCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 14 Sockets

7 -ProceduresCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 15 Client -Server Communication -Unixsocket()bind()listen()accept()recv( )send()close()Serversocket()connect()sen d()recv()close()Clientsynchronization pointStream ( TCP)socket()bind()recvfrom()sendto()clos e()Serversocket()sendto()recvfrom()close ()ClientDatagram ( UDP)bind()CS556 -Distributed SystemsTutorial 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, , 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 ( , 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!

8 CS556 - Distributed SystemsTutorial by Eleftherios Kosmas 17 Client -Server Communication -Unixsocket()bind()listen()accept()recv( )send()close()Serversocket()connect()sen d()recv()close()Clientsynchronization pointStream ( TCP)socket()bind()recvfrom()sendto()clos e()Serversocket()sendto()recvfrom()close ()ClientDatagram ( UDP)bind()CS556 -Distributed SystemsTutorial 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 socketCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 19 Specifying Addresses Socket API defines a genericdata type for addresses: Particular form of the sockaddr used for TCP/IPaddresses:)Important: sockaddr_in can be casted to a sockaddrstruct sockaddr{unsigned short sa_family; /* Address family ( AF_INET) */char sa_data[14]; /* Family-specific address information */}struct in_addr {unsigned long s_addr.}

9 /* 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_zero[8]; /* Not used */}CS556 - Distributed SystemsTutorial by Eleftherios Kosmas 20 Client -Server Communication -Unixsocket()bind()listen()accept()recv( )send()close()Serversocket()connect()sen d()recv()close()Clientsynchronization pointStream ( TCP)socket()bind()recvfrom()sendto()clos e()Serversocket()sendto()recvfrom()close ()ClientDatagram ( UDP)bind()CS556 -Distributed SystemsTutorial by Eleftherios Kosmas 21 Assign address to socket: bind() associates and reserves a port for use by the socket int status = bind(sockid, &addrport, size).

10 Sockid: integer, socket descriptor addrport: struct sockaddr, the (IP) address and port of the machine for TCP/IP server, internet address is usually set to INADDR_ANY, , chooses any incoming interface size: the size (in bytes) of the addrport structure status: upon failure -1 is returnedCS556 - Distributed SystemsTutorial by Eleftherios Kosmas 22bind()- Example with TCPint sockid;struct sockaddr_in addrport;sockid= socket(PF_INET, SOCK_STREAM, 0); = AF_INET; = htons(5100); = htonl(INADDR_ANY);if(bind(sockid, (struct sockaddr *) &addrport, sizeof(addrport))!= -1) { ..}CS556 - Distributed SystemsTutorial by Eleftherios Kosmas 23 Skipping the bind() bind can be skipped for both types of Sockets Datagram socket: if only sending, no need to bind.


Related search queries