Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.

Similar presentations


Presentation on theme: "Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission."— Presentation transcript:

1 Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission

2 Socket API API: Application Program Interface
An API is simply a collection of functions to help simplify a programming task Socket API is a standardized set of functions used to help create software that communicates over networks Socket API is available for most modern operating systems including Microsoft's Windows, Apple's OS-X, Android, and Unix.

3 Sockets Socket: a line of communication used by an application to talk to another application program Socket: an endpoint in a communication flow between two application programs Applications create sockets in order to talk to other applications Applications can have multiple sockets Client Server socket socket

4 Using Sockets Applications create sockets for communication
When a socket is created, the OS returns a descriptor, which is an integer used to identify the socket Applications pass the descriptor to all other API functions to identify which socket the function should work with OS request OS response Application create socket socket #1 created create socket socket #2 created socket #1 create socket socket #3 created data socket #2 send data on socket 2 4 bytes sent data socket #3 recv data on socket 3 7 bytes sent close socket 1 socket closed

5 Socket API Parameters Applications must supply many details:
IP address of the remote computer Port number Protocol to use Functions in the API could have many parameters so that all the details can be supplied Few functions would be needed Socket API, instead, uses many functions Thus, each function does not need many parameters

6 Common Socket API Functions
Used by Description accept server Accept an incoming connection bind Specify IP address and port number close either Close the connection* connect client Connect to a server getpeername Obtain the client's IP address listen Prepare socket to listen for incoming connections recv Receive incoming data send Send outgoing data socket Create a socket *note: Windows uses closesocket instead of close

7 Typical Socket Function Calls
Client Server socket bind socket listen connect accept send recv recv send close close

8 Server Uses Multiple Sockets
One socket for listening and one for communicating Client Server socket 1 socket bind connect listen accept socket 2 send recv recv send close close

9 Creating Sockets Please refer to the Socket API handout on the class website int int int SOCKET descriptor; descriptor = socket( address family, type, protocol ); Use 2 for TCP Constant: IPPROTO_TCP Use 1 for Stream Constant: SOCK_STREAM Use 6 for Internet Version 4 (IPv4) Constant: AF_INET For more detailed information, visit:

10 SOCKET_ERROR if unsuccessful
Connecting To A Server Please refer to the Socket API handout on the class website descriptor = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); int code = connect( socket, serverAddr, serverAddrLength ); Length in bytes of the serverAddr structure. Use C lang sizeof command 0 = success or SOCKET_ERROR if unsuccessful struct SOCKADDR_IN sin_family sin_address sin_port For more detailed information, visit:

11 Starting Up A Server Please refer to the Socket API handout on the class website int code = bind( socket, address, lengthOfAddress ); Parameters and return value are essentially the same as the connect function. For more detailed information, visit: and look up the function in question

12 Incoming connections above this limit will be automatically rejected
Starting Up A Server Please refer to the Socket API handout on the class website int code = listen( socket, queueLength ); integer Number of pending connections that can be placed in a queue, waiting to be processed Incoming connections above this limit will be automatically rejected Should be >= 1

13 INVALID_SOCKET if unsuccessful
Starting Up A Server Please refer to the Socket API handout on the class website SOCKET commSocket = accept( socket, clientAddr, clientAddrLength ); Optional returns the address of the connecting client works the same as the server addresses Returns: a new socket or INVALID_SOCKET if unsuccessful

14 SOCKET_ERROR if unsuccessful
Sending Data Please refer to the Socket API handout on the class website int bytes = send( socket, buffer, lengthOfData, flags ); Unused. Set to 0 How many BYTES to send Returns: # of bytes sent or SOCKET_ERROR if unsuccessful The address (pointer) in memory of where the data to send is located

15 Sending Data Example Please refer to the Socket API handout on the class website char buffer[6] = "HELLO"; int bytes = send( socket, buffer, lengthOfData, flags ); 6 bytes H E L O \0

16 Sending Data Example Please refer to the Socket API handout on the class website int buffer[3] = { 56, 23, 17 }; int bytes = send( socket, (char*)buffer, lengthOfData, flags ); 12 bytes 56 23 17 Modern C compilers define 4 bytes per int Type cast needed because the function wants a pointer to a char(byte) array

17 SOCKET_ERROR if unsuccessful
Receiving Data Please refer to the Socket API handout on the class website int bytes = recv( socket, buffer, lengthOfData, flags ); Unused. Set to 0 Size of the storage buffer, in BYTES Returns: # of bytes received or SOCKET_ERROR if unsuccessful The address (pointer) in memory of where the data is to be stored

18 Receiving Data Example
Please refer to the Socket API handout on the class website char buffer[10]; 10 bytes int bytes = recv( socket, buffer, lengthOfData, flags ); 6 bytes W O R L D \0

19 Other Functions (Windows only)
int portCorrect = htons( port ); "host to network service" converts the port (integer) from host byte ordering to the correct network byte ordering ensures that the bytes making up the port number are in the correct order as needed by the network Example: Port Number 5 LSB Host: 5 Network: 5 LSB

20 Other Functions (Windows only)
inet_pton( addressFamily, addressIn, addressOut ); converts an IP address in dotted decimal notation into its 32-bit IP address form technically: converts from a string to an integer Example: Convert IPv4 address AF_INET " " inet_pton( addressFamily, addressIn, addressOut );

21 Including the Functions
To compile a program: Socket function definitions must be included at the top of your code so that your code will know of their existance. #include <WinSock2.h> #include <Ws2tcpip.h>

22 Including the Functions
To execute a program: A) The linker must know about the location of the code for the socket functions #pragma comment(lib, "Ws2_32.lib")

23 Including the Functions
To execute a program: B) Socket function code must be loaded into memory. This is done after your program code starts executing through a process called dynamic linking. WSAStartup( version, data pointer ) Example: // Load version 2.2 of the WinSocket DLL, and store the information in wsaData structure WSADATA wsaData; WSAStartup( MAKEWORD(2,2), &wsaData );

24 Static Linking Code for external functions is included with program code and is loaded into memory when the program is loaded Every program which uses these functions has its own copy of the function code Occupies extra memory Occupies extra hard drive space Hard to upgrade the library RAM Memory Web Browser Client Streaming Music App Socket API functions Socket API functions Socket API functions

25 Dynamic Linking Code for external functions is stored in a file (DLL) separate from the application program file The program loads the DLL when needed Programs can share the loaded function code RAM Memory Web Browser Client Streaming Music App Socket API functions

26 Dynamic Linking When an application ends, it needs to indicate that it is finished using the DLL code WSACleanup() When the last application ends, the DLL code is removed from memory RAM Memory Web Browser Client Streaming Music App Socket API functions


Download ppt "Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission."

Similar presentations


Ads by Google