DOC PREVIEW
UI CS 270 - Sockets

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SocketsBased on Section 17.3 of Computer Networking with Internet Protocols and Technology, by William Stallings, Prentice Hall.book chapter 12.6.21SocketsThe concept of sockets and sockets programming was developed in the 1980s in the UNIX environment as the Berkeley Sockets Interface. a socket enables communication between a client and server process and may be connection-oriented or connectionless.The Berkeley Sockets Interface is the de facto standard application programming interface (API) for developing networking applicationsWindows Sockets (WinSock) is based on the Berkeley specification. The sockets API provides generic access to interprocess communications services.2Sockets3SocketsTCP and UDP header includes source port and destination port fields, IP header includes IP addressTCP/UDP: The port values identify the respective users (applications) of the two TCP entities.IP (IPv4 and IPv6): header includes source address and destination address fieldsthese IP addresses identify the respective host systems. Definition of a SocketThe concatenation of a port value and an IP address forms a socket, which is unique throughout the Internet.4SocketsThe socket is used to define an API, which is a generic communication interface for writing programs that use TCP or UDP.In practice, when used as an API, a socket is identified by the triple (protocol, local address, local process).The local address is an IP address and the local process is a port number. Because port numbers are unique within a system, the port number implies the protocol (TCP or UDP).5SocketsThe Sockets API recognizes two types of sockets: Stream sockets (SOCK_STREAM)make use of TCP, which provides a connection-oriented reliable data transfer. with stream sockets, all blocks of data sent between a pair of sockets are guaranteed for delivery and arrive in the order that they were sent.6SocketsThe Sockets API recognizes two types of sockets: Datagram sockets, (SOCK_DGRAM)make use of UDP, which does not provide the connection-oriented features of TCP. with datagram sockets, delivery is not guaranteed, nor is order necessarily preserved.There is a third type of socket provided by the Sockets API: raw sockets, (SOCK_RAW)Raw sockets allow direct access to lower layer protocols, such as IP.7SocketsSocket Interface CallsTo use sockets, it is a three-step process:1. Socket Setup 2. Socket Connection 3. Socket CommunicationAny program that uses sockets must include/usr/include/sys/types.h /usr/include/sys/socket.h8SocketsThe typical TCP client’s communication involves four basic steps: 1. Create a TCP socket using socket(). 2. Establish a connection to the server using connect(). 3. Communicate using send() and recv().4. Close the connection with close().9SocketsSocket SetupThe first step in using Sockets is to create a new socket using the socket() command. There are three parameters:1. the protocol family is always PF INET for the TCP/IP protocol suite. 2. the type specifies whether this is a stream or datagram socket 3. the protocol specifies either TCP or UDP.The reason that both type and protocol need to be specified is to allow additional transport-level protocols to be included in a future implementation.10SocketsAfter socket is created, it must have an address to listen to. The bind() function binds a socket to a socket address. The address has the structure:11After a socket is created, it must have an address to listen to. The bind()function binds a socket to a socket address. The address has the structure:struct sockaddr_in {short int sin_family; // Address family (TCP/IP)unsigned short int sin_port; // Port numberstruct in_addr sin_addr; // Internet addressunsigned char sin_zero[8]; // Same size as struct sockaddr};8SocketsSocket ConnectionStream socketonce the socket is created, a connection must be set up to a remote socket. one side functions as a client, and requests a connection to the other side, which acts as a server.12SocketsThe server side of a connection setup requires two steps:1. a server application issues a listen(), indicates that socket is ready to accept incoming connections. parameter backlog is the number of connections allowed on the incoming queue. Each incoming connections is placed in this queue until a matching accept() is issued by the server side.13SocketsThe server side of a connection setup requires two steps:2. the accept() call is used to remove one request from the queue. If the queue is empty, the accept() blocks the process until a connection request arrives. If there is a waiting call, then accept() returns a new file descriptor for the connection. This creates a new socket, which has the IP address and port number of the remote party, the IP address of this system, and a new port number.14SocketsA client application issues a connect() that specifies both a local socket and the address of a remote socket. If the connection attempt is unsuccessful connect() returns the value 1. If the attempt is successful, connect() returns a 0 and fills in the file descriptor parameter to include the IP address and port number of the local and foreign sockets. Recall that the remote port number may differ from that specified in the foreignAddress parameter because the port number is changed on the remote host.15SocketsSocket CommunicationFor stream communication, the functions send() and recv() are used to send or receive data over the connection identified by the sockfd parameter.In the send() call, the *msg parameter points to the block of data to be sent and the len parameter specifies the number of bytes to be sent.The flags parameter contains control flags, typically set to 0.The send() call returns the number of bytes sent, which may be less than the number specified in the len parameter.16SocketsSocket Communication cont.In the recv() call, the *buf parameter points to the buffer for storing incoming data, with an upper limit on the number of bytes set by the len parameter.At any time, either side can close the connection with the close() call, which prevents further sends and receives. The shutdown() call allows the caller to terminate sending or receiving or both.1718Socket System Calls12SocketsDatagram CommunicationFor datagram communication, the functions sendto() and recvfrom() are used.The sendto() call includes all the parameters of the send() call plus a specification of the destination address (IP address and port).Similarly, the recvfrom() call includes an address


View Full Document

UI CS 270 - Sockets

Download Sockets
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Sockets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sockets 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?