160 likes | 266 Vues
Introduction of Socket Programming. L.Aseel AlTurki King Saud University. Outline. Client/Server paradigm Addressing Socket addresses Socket programming. Client/Server Paradigm. Client (local) Process. Server (remote)Process. Local Host. Remote Host. Addressing. needs.
E N D
Introduction of Socket Programming L.AseelAlTurki King Saud University
Outline • Client/Server paradigm • Addressing • Socket addresses • Socket programming
Client/Server Paradigm Client (local) Process Server (remote)Process Local Host Remote Host
Addressing needs Transport Layer Port number needs Network Layer IP address needs Data Link Layer MAC address
Addressing IANA Ranges: Well-Known ports(0 to 1023) Registered ports(1024 to 49,151) Dynamic ports(49,152 to 65,535)
Socket Addresses • 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.
Socket Addresses (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.
Socket Programming • 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).
Socket Programming (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.
Socket Programming (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.
process process Transport layer protocol Transport layer protocol socket socket Socket Programming (cont..) controlled by application developer controlled by application developer controlled by operating system controlled by operating system internet host or server host or server
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 Socket Programming (cont..)
Socket Programming (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.
Socket Programming (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.