TA ECEN 602 Kiran Kotla By 1 An Introduction to Network Programming Interface Application Internet Interface Application How do two applications interact 2 ECEN 602 Socket Programming Suppose mymachine tamu edu wants to open this webpage It needs to talk to the web server at www tamu edu www tamu edu Host Name Address This specifies one end point of communication http www tamu edu Example 3 Interface Application Client Mymachine tamu edu Internet Interface Application Server www tamu edu How do two applications interact 4 ECEN 602 Socket Programming As there could be multiple applications running at the server machine each application operates at a port number In this type of set up www tamu edu is the server machine And Mymachine tamu edu is the client machine Here the tag http tells the browser to contact the web server at www tamu edu Example contd 5 Interface Application Client Mymachine tamu edu Browser Internet Interface Application Server www tamu edu 6 Web Server How do two applications interact Interface Application Client Mymachine tamu edu Browser port x Internet Interface Application Server www tamu edu 7 Web Server port y How do two applications interact Physical Physical Data link Physical Data link Network Network Data link Transport Transport Network Application Application TCP IP Network Stack 8 Protocol B Protocol C ECEN 602 Socket Programming Protocol A Network API Application The services provided often by the operating system that provide the interface between application and protocol software Network Application Programming Interface API 9 connect listen accept ConnectionConnection oriented TCP send recv ECEN 602 Socket Programming Connectionless UDP read write close General Use System calls 10 Client Server Session open clientfd EOF close read write read close read accept listen write connect Connection request socket socket bind Server Client Overview open listenfd 11 ECEN 602 Socket Programming Generic Programming Interface Support for message oriented and connection oriented communication Operating System independence Desired properties of Network API 12 ECEN 602 Socket Programming Support multiple communication protocol suites families Address endpoint representation independence Generic Programming Interface 13 ECEN 602 Socket Programming TCP IP does not include an API definition TCP IP 14 ECEN 602 Socket Programming Specify local and remote communication endpoints Initiate a connection Wait for incoming connection Send and receive data Terminate a connection gracefully Error handling Functions needed 15 ECEN 602 Socket Programming support for multiple protocol families address representation independence Generic Berkeley Sockets 16 ECEN 602 Socket Programming establishing a connection specifying communication endpoint addresses A socket is an abstract representation of a communication endpoint Need of Sockets Socket 17 ECEN 602 Socket Programming family specifies the protocol family PF INET for TCP IP type specifies the type of service SOCK STREAM SOCK STREAM SOCK DGRAM SOCK DGRAM protocol specifies the specific protocol usually 0 which means the default default int socket int family int type int proto Creating a Socket 18 ECEN 602 Socket Programming socket allocates resources needed for a communication endpoint but it does not deal with endpoint addressing The socket system call returns a socket descriptor small integer or 1 on error socket 19 ECEN 602 Socket Programming Remember that the sockets API is generic There must be a generic way to specify endpoint addresses TCP IP requires an IP address and a port number for each endpoint address Other protocol suites families may use other schemes Specifying an Endpoint Address 20 4 3 2 1 0 Descriptor Table ECEN 602 Socket Programming Family AF INET Service SOCK STREAM Local IP 111 22 3 4 Remote IP 123 45 6 78 Local Port 2249 Remote Port 3726 Socket Descriptor Data Structure 21 address family length of struct IPv4 address IP port number ECEN 602 Socket Programming sa family t socklen t in addr t in port t Some data types that are used 22 sa len sa family sa data 14 ECEN 602 Socket Programming sa family specifies the address type sa data specifies the address value struct sockaddr uint8 t sa family t char Generic socket addresses 23 ECEN 602 Socket Programming 16 bit port number 32 bit IP address For AF INET we need AF INET 24 ECEN 602 Socket Programming A special kind of sockaddr structure struct sockaddr in uint8 t sin len sa family t sin family in port t sin port struct in addr sin addr char sin zero 8 struct sockaddr in IPv4 25 ECEN 602 Socket Programming However the architectures of the two end hosts could be different i e one is little endian and the other is big endian Bytes travel through the network in a stream of bytes Network Byte Order 26 ECEN 602 Socket Programming Network Byte Order Contd 27 sin port sin addr ECEN 602 Socket Programming a TCP IP port number an IP address All values stored in a sockaddr in must be in network byte order Network Byte Order Contd 28 l long 32bit s short 16bit ECEN 602 Socket Programming uint32 t hton tonl uint32 t uint32 t ntoh tohl uint32 t uint16 t hton tons uint16 t tohs uint 16 t uint16 t ntoh n network byte order h host byte order Network Byte Order Functions 29 ECEN 602 Socket Programming BUT The C functions that make up the sockets API expect structures of type sockaddr sockaddr We don t need to deal with sockaddr structures since we will only deal with a real protocol family We can use sockaddr in structures TCP IP Addresses 30 sa data ECEN 602 Socket Programming sin len AF INET sa len sa family sin zero sin addr sin port sockaddr in sockaddr 31 ECEN 602 Socket Programming bind returns 0 if successful or 1 on error int bind int sockfd const struct sockaddr myaddr int addrlen const The bind system call is used to assign an address to an existing socket Assigning an address to a socket 32 ECEN 602 Socket Programming bind mysock struct sockaddr myaddr sizeof myaddr calling bind assigns the address specified by the sockaddr structure to the socket descriptor You can give bind a sockaddr in structure bind 33 ECEN 602 Socket Programming err bind mysock sockaddr myaddr sizeof myaddr mysock socket PF INET SOCK STREAM 0 myaddr sin family AF INET myaddr sin port htons portnum myaddr sin addr htonl ipaddress int mysock err struct sockaddr in myaddr bind Example 34 ECEN 602 Socket Programming Client can ask the O S to assign any available port number Client can
View Full Document