B.P. PODDAR INSTITUTE OF MANAGEMENT AND TECHNOLOGY 137, V.I.P. ROAD, PODDAR VIHAR, KOLKATA – 700052 DEPARTMENT OF INFORMATION TECHNOLOGY Term paper On GUESS THE NUMBER PUZZLE USING SOCKET PROGRAMMING Subject : COMPUTER NETWORKING (PCC CS602) Third Year, Semester VI Academic Year 2020-21 SUBMITTED by : Ujjwal Kumar Singh - 11500218003 CONTENTS Socket Programming Sockets Stages for Server Client-Server communication Guess the number puzzle using socket programming Conclusion SOCKET PROGRAMMING A SHORT INTRODUCTION Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server What are Sockets? A socket is one endpoint of a two way communication link between two programs running on the network. The socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points between which the communication take place. Like ‘Pipe’ is used to create pipes and sockets is created using ‘socket’ system call. The socket provides bidirectional FIFO Communication facility over the network. A socket connecting to the network is created at each end of the communication. Each socket has a specific address. This address is composed of an IP address and a port number. Socket are generally employed in client server applications. The server creates a socket, attaches it to a network port addresses then waits for the client to contact it. The client creates a socket and then attempts to connect to the server socket. When the connection is established, transfer of data takes place. Types of Sockets : There are two types of Sockets: the datagram socket and the stream socket. 1. Datagram Socket : This is a type of network which has connection less point for sending and receiving packets. It is similar to mailbox. The letters (data) posted into the box are collected and delivered (transmitted) to a letterbox (receiving socket). 2. Stream Socket In Computer operating system, a stream socket is type of interprocess communications socket or network socket which provides a connection-oriented, sequenced, and unique flow of data without record boundaries with well defined mechanisms for creating and destroying connections and for detecting errors. It is similar to phone. A connection is established between the phones (two ends) and a conversation (transfer of data) takes place. Network socket A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture. Sockets are created only during the lifetime of a process of an application running in the node. Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is most commonly used in the context of the Internet Protocol Suite, and is therefore often also referred to as Internet socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport protocol, IP address, and port number. The term socket is also used for the software endpoint of node-internal inter-process communication (IPC), which often uses the same API as a network socket. Use The use of the term socket in software is analogous to the function of an electrical female connector, a device in hardware for communication between nodes interconnected with an electrical cable. Similarly, the term port is used for external physical endpoints at a node or device. The application programming interface (API) for the network protocol stack creates a handle for each socket created by an application, commonly referred to as a socket descriptor. In Unix-like operating systems, this descriptor is a type of file descriptor. It is stored by the application process for use with every read and write operation on the communication channel. At the time of creation with the API, a network socket is bound to the combination of a type of network protocol to be used for transmissions, a network address of the host, and a port number. Ports are numbered resources that represent another type of software structure of the node. They are used as service types, and, once created by a process, serve as an externally (from the network) addressable location component, so that other hosts may establish connections. Network sockets may be dedicated for persistent connections for communication between two nodes, or they may participate in connectionless and multicast communications. In practice, due to the proliferation of the TCP/IP protocols in use on the Internet, the term network socket usually refers to use with the Internet Protocol (IP). It is therefore often also called Internet socket. Socket addresses An application can communicate with a remote process by exchanging data with TCP/IP by knowing the combination of protocol type, IP address, and port number. This combination is often known as a socket address. It is the network-facing access handle to the network socket. The remote process establishes a network socket in its own instance of the protocol stack, and uses the networking API to connect to the application, presenting its own socket address for use by the application. Implementation A protocol stack, usually provided by the operating system (rather than as a separate library, for instance), is a set of services that allow processes to communicate over a network using the protocols that the stack implements. The operating system forwards the payload of incoming IP packets to the corresponding application by extracting the socket address information from the IP and transport protocol headers and stripping the headers from the application data. The application programming interface (API) that programs use to communicate with the protocol stack, using network sockets, is called a socket API. Development of application programs that utilize this API is called socket programming or network programming. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor, due to the Unix philosophy that "everything is a file", and the analogies between sockets and files. Both have functions to read, write, open, and close. In practice the differences strain the analogy, and different interfaces (send and receive) are used on a socket. In inter-process communication, each end generally has its own socket. In the standard Internet protocols TCP and UDP, a socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have a source address, for example, for only sending data, but if a program binds a socket to a source address, the socket can be used to receive data sent to that address. Based on this address, Internet sockets deliver incoming data packets to the appropriate application process. Socket often refers specifically to an internet socket or TCP socket. An internet socket is minimally characterized by the following: local socket address, consisting of the local IP address and (for TCP and UDP, but not IP) a port number protocol: A transport protocol, e.g., TCP, UDP, raw IP. This means that (local or remote) endpoints with TCP port 53 and UDP port 53 are distinct sockets, while IP does not have ports. A socket that has been connected to another socket . CLIENT-SERVER COMMUNICATION CLIENT Initiates the communication Must know the address and the port of the server Active socket SERVER Passively waits for and responds to clients Passive socket SOCKET CREATION int sockfd = socket(domain, type, protocol) sockid: socket descriptor, an integer (like a file-handle) domain: integer, communication domain e.g., AF_INET (IPv4 protocol) , AF_INET6 (IPv6 protocol) type: communication type SOCK_STREAM: TCP(reliable, connection oriented) SOCK_DGRAM: UDP(unreliable, connectionless) protocol: Protocol value for Internet Protocol(IP), which is 0. This is the same number which appears on protocol field in the IP header of a packet.(man protocols for more details) BIND int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); After creation of the socket, bind function binds the socket to the address and port number specified in addr(custom data structure). In the example code, we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address LISTEN int listen(int sockfd, int backlog); It puts the server socket in a passive mode, where it waits for the client to approach the server to make a connection ACCEPT int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. At this point, connection is established between client and server, and they are ready to transfer data. CONCLUSION Network programming makes use of socket for interprocess communication between hosts where sockets act as the endpoint of the interprocess communication. Here sockets can also be termed as network socket or Internet socket since communication between computers is based on Internet protocol AFTER COMPLETING THIS PRESENTATION ON SOCKET PROGRAMMING .WE CAME TO KNOW ABOUT SO MANY TERMS RELATED TO THIS TOPIC .FIRSTLY, WE HAVE LEARNT ABOUT SOCKET AND IT'S TYPE THEN WE HAVE LEARNT ABOUT SOCKET PROCEDURES ..,etc THANK YOU !!!!!!!!!!