the socket api
play

The Socket API Prof. Chuan-Ming Liu Computer Science and - PowerPoint PPT Presentation

Mobile Computing & Software Engineering Lab The Socket API Prof. Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN NTUT, TAIWAN 1 Mobile Computing & Software


  1. Mobile Computing & Software Engineering Lab The Socket API Prof. Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN NTUT, TAIWAN 1

  2. Mobile Computing & Software Engineering Lab Socket API Specifying a Protocol Interface Socket Abstraction Specifying an Endpoint Address Address Structure Major System Calls Integer Conversion Symbolic Constants NTUT, TAIWAN 2

  3. Mobile Computing & Software Engineering Lab Specifying a Protocol Interface Two approaches when designing functions to OS that provide applications to access TCP/IP protocols Specific functions for TCP/IP communication Flexible functions in general with parameters to specify NTUT, TAIWAN 3

  4. Mobile Computing & Software Engineering Lab Specifying a Protocol Interface Example: First approach may use maketcpconnection The other may use makeconnection with a parameter to specify the TCP protocol Second approach is preferable To accommodate multiple sets of communication protocols TCP/IP protocols are one single family (family PF_INET ) NTUT, TAIWAN 4

  5. Mobile Computing & Software Engineering Lab Specifying a Protocol Interface Applications specify operations using a type of service instead of the protocol name Example: using stream transfer type of service to specify a TCP connection NTUT, TAIWAN 5

  6. Mobile Computing & Software Engineering Lab Specifying a Protocol Interface The socket API provides generalized functions that support network communication using many possible protocols. Socket calls refer to all TCP/IP protocols as a single protocol family. The calls allow the programmer to specify the type of service required rather than the name of a specific protocol. NTUT, TAIWAN 6

  7. Mobile Computing & Software Engineering Lab Socket Abstraction File descriptors When performing I/O on a file, open( ) function is called open( ) function will create a file descriptor deal with the files Are stored in a file descriptor table Each process has one separate descriptor table NTUT, TAIWAN 7

  8. Mobile Computing & Software Engineering Lab File Descriptor Table Operation System descriptor table (one per process) internal data structure for file 0 0 1 internal data structure for file 1 2 internal data structure for file 2 3 internal data structure for file 3 NTUT, TAIWAN 8

  9. Mobile Computing & Software Engineering Lab Socket Abstraction Similarly, socket API adds new abstraction for network communication, the socket Each socket is identified by an small integer, socket descriptor same with the file descriptor An application cannot have both a file descriptor and a socket descriptor with the same value Note: Windows OS using a separate table for socket descriptor NTUT, TAIWAN 9

  10. Mobile Computing & Software Engineering Lab Socket Abstraction Socket API contains a function, socket to create a socket. On UNIX, file and socket descriptors use the same descriptor table In Windows OS, a file descriptor and a socket descriptor may have the same value. NTUT, TAIWAN 10

  11. Mobile Computing & Software Engineering Lab Socket Data Structure When socket () is called, the OS allocates a new data structure for socket fills in a new descriptor table entry leaves most of the fields in the structure unfilled NTUT, TAIWAN 11

  12. Mobile Computing & Software Engineering Lab Socket Descriptor Table OS data structure socket descriptor table for a socket (one per process) Family: PF_INET 0 Service: SOCK_STREAM 1 pointers to Local IP: 2 other Remote IP: 3 socket Local port: 4 structures Remote port: NTUT, TAIWAN 12

  13. Mobile Computing & Software Engineering Lab Socket Abstraction When a socket has been created, The socket is completely general and can be used for arbitrary communication Application specifies how it will be used Tell the role of a socket in Passive Active The only difference between an active socket and a passive one lies in how the applications use it; initially all sockets are created in the same way NTUT, TAIWAN 13

  14. Mobile Computing & Software Engineering Lab Specifying an Endpoint Address When a socket is created contains no information about how to use it no information about the port number and IP address, …, etc Before using a socket, it must specify some addresses NTUT, TAIWAN 14

  15. Mobile Computing & Software Engineering Lab Specifying an Endpoint Address Communication endpoint In TCP/IP, consists of an IP address and a protocol port number Each protocol family has its own specification for endpoint address NTUT, TAIWAN 15

  16. Mobile Computing & Software Engineering Lab Specifying an Endpoint Address The socket abstraction defines an address family for each type of address Each protocol family can use one or more address families to define address representations TCP/IP protocols all use a single address representation with the symbolic constant AF_INET NTUT, TAIWAN 16

  17. Mobile Computing & Software Engineering Lab Specifying an Endpoint Address Note: Confusion arises between the TCP/TP protocol family, PF_INET and its address family, AF_INET Both have the same numeric value (2) NTUT, TAIWAN 17

  18. Mobile Computing & Software Engineering Lab Address Structure Socket system defines a generalized format that all endpoint addresses used for flexibility ( address family , endpoint address in that family ) Address family: one of preassigned address types The standard representation for the specified address type NTUT, TAIWAN 18

  19. Mobile Computing & Software Engineering Lab Sockadd Structure The most general structure, sockaddr structure struct sockaddr { u_char sa_len; /* total length */ u_short sa_family; /* type of address*/ char sa_data[14]; /* value of address*/ }; NTUT, TAIWAN 19

  20. Mobile Computing & Software Engineering Lab Sockadd Structure Does not fit for all address families Some address families have longer endpoint address Accommodates address in the AF_INET family, so TCP/IP software works well To keep the portability and maintainable, do not use this address structure NTUT, TAIWAN 20

  21. Mobile Computing & Software Engineering Lab Sockadd_in Structure TCP/IP protocols exclusively use structure sockaddr_in instead struct sockaddr_in { u_char sin_len; /* total length */ u_short sin_family; /* type of address*/ u_short sin_port; /* port number*/ struct in_addr sin_addr; /* IP address*/ char sin_zero[8]; /* unused*/ }; NTUT, TAIWAN 21

  22. Mobile Computing & Software Engineering Lab Sockadd_in Structure When representign a TCP/IP communication endpoint, an application program uses structure sockaddr_in , which contains both an IP address and a protocol port number. Programmers must be careful when writing programs that use a mixture of protocols because some non-TCP/IP endpoint addresses require a larger structure NTUT, TAIWAN 22

  23. Mobile Computing & Software Engineering Lab Major System Calls in the Socket API Two groups for socket calls: Primary calls Utility routines NTUT, TAIWAN 23

  24. Mobile Computing & Software Engineering Lab Primary Socket Calls Socket Connect Recv Close Bind Listen Accept Read and write NTUT, TAIWAN 24

  25. Mobile Computing & Software Engineering Lab Socket Routines Particularly, we discuss the ones for integer conversion, including htons ntohs htonl ntohl NTUT, TAIWAN 25

  26. Mobile Computing & Software Engineering Lab Socket Function Create a new socket for network communication Return a descriptor for the newly created socket NTUT, TAIWAN 26

  27. Mobile Computing & Software Engineering Lab Connect Function On client side Following the socket call Establish an active connection to a remote server NTUT, TAIWAN 27

  28. Mobile Computing & Software Engineering Lab Send Function Clients and servers use it to transfer data across a TCP connection Client usually uses send to transmit request; server uses it to transmit replies Which data should be sent The address of the data to be sent The length of the data NTUT, TAIWAN 28

  29. Mobile Computing & Software Engineering Lab Recv Function Clients and serves use it to receive data from a TCP connection Usually, servers use it to receive requests; clients use if to receive replies Which socket descriptor to use The address of a buffer The length of the buffer The amount of data received depends on the buffer size defined Also works for receiving message using UDP NTUT, TAIWAN 29

  30. Mobile Computing & Software Engineering Lab Close Function To finish using a socket A socket may be used by many processes, close terminates the connection for the process issues the close call and reduces the reference count by 1 until to 0; when the counter is 0, the socket is deallocated No socket reference count for the threads in a process NTUT, TAIWAN 30

  31. Mobile Computing & Software Engineering Lab Bind Function When socket created, no endpoint information inside Bind is used to specify the local endpoint address NTUT, TAIWAN 31

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend