Presentation on theme: "I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University."— Presentation transcript:
I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University
O UTLINE Client/Server paradigm Addressing Socket addresses Socket programming
C LIENT /S ERVER P ARADIGM Local Host Remote Host Server (remote) Process Client (local) Process
A DDRESSING Data Link Layer Network Layer Transport Layer Port number MAC address IP address needs
A DDRESSING IANA Ranges: Well-Known ports(0 to 1023) Registered ports(1024 to 49,151) Dynamic ports(49,152 to 65,535)
S OCKET A DDRESSES The combination of an IP address and a port number is called a socket address. A socket is defined as "the endpoint in a connection.“ Sockets are the fundamental technology for programming software to communicate on TCP/IP networks. A socket provides a bidirectional communication endpoint for sending and receiving data with another socket.
S OCKET A DDRESSES ( CONT..) Sockets are interfaces that can "plug into" each other over a network. Once so "plugged in", the programs so connected communicate (TCP). Transport layer needs a pair of socket addresses: the client socket address and the server socket address. The IP header contains IP addresses; the UDP or TCP header contains the port numbers.
S OCKET P ROGRAMMING Socket programming is a method for communication between a client program and a server program in a network. Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API). The communication between the client and server process may either be connection-oriented (such as an established TCP virtual circuit or session), or connectionless (based on UDP datagrams).
S OCKET P ROGRAMMING ( CONT..) Socket connections normally run between two different computers on a LAN or across the Internet, but they can also be used for inter- process communication on a single computer.
S OCKET P ROGRAMMING ( CONT..) Socket connections normally run between two different computers on a LAN or across the Internet, but they can also be used for inter- process communication on a single computer. A program that can act both as a client and a server is based on peer-to-peer communication. an examples for the uses of socket programming: Web browsers.
S OCKET P ROGRAMMING ( CONT..) process Transport layer protocol socket controlled by application developer controlled by operating system host or server process Transport layer protocol socket controlled by application developer controlled by operating system host or server internet
S OCKET P ROGRAMMING ( CONT..) Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local socket specifying IP address, port number of server process When client creates socket: client transport protocol establishes connection to the server When contacted by client, server transport protocol creates new socket for server process to communicate with client allows server to talk with multiple clients
S OCKET P ROGRAMMING ( CONT..) These are examples of functions or methods typically provided by the API library: socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it. bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number and IP address. listen() is used on the server side, and causes a bound TCP socket to enter listening state. connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP connection.
S OCKET P ROGRAMMING ( CONT..) accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection. send() and recv(), or write() and read(), or recvfrom() and sendto(), are used for sending and receiving data to/from a remote socket. close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.