




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Socket programming. System calls. TCP client-server architecture.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





A socket is a communications connection point (endpoint) that you can name and address in a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes. A socket is an endpoint in communication between networks, and socket programming enables these endpoints to transfer data, thereby supporting communication between networks and programs. Socket programming, for beginners, can play a major role in understanding how networks communicate. Socket programming has several benefits, such as aiding in real-time connectivity, and can use a variety of programming languages. One of the popular programming language options is Python, which is a common language that networking professionals will encounter in their careers. While Python isn't the only language beginners should learn for socket programming, it has various tools and modules to help handle packets and provides both high- and low-level methods of handling sockets. What are the benefits and challenges of socket programming for networking and security? From a networking point of view, we could use sockets to implement a client-server application -- for example, a chat. From a security point of view, sockets are used at a low level to determine the ports that a machine has open, and typical port scanner tools, such as Nmap, use them at a low level. They can also be used to connect to an external server using a reverse shell in the same way that we can use the SSH [Secure Socket Shell] command. The main challenges are related to being able to make requests asynchronously -- for example, with the python-socketio project, you can implement clients, applications and servers that can be integrated with Python web frameworks, like Flask. How much does socket programming differ from Transmission Control Protocol (TCP) to User Datagram Protocol (UDP)? The main difference between TCP and UDP is that TCP is connection-oriented. This means [it] is guaranteed our packets will reach their destinations, with error notifications if packet delivery fails. On the other hand, UDP … is suitable for applications that require efficient communication that don't have to worry about packet loss. The main difference between working with TCP and UDP in Python is that, when creating the socket, you have to use SOCK_DGRAM for UDP and SOCK_STREAM for TCP. Why should a network professional use Python for socket programming over another language? Python provides the socket module required to work with sockets at high and low levels. The socket module provides all the required functionalities to quickly write TCP and UDP clients and servers. Python also offers other tools for the manipulation of network packets -- like Scapy, [which] is a module written in Python to manipulate packets with support for multiple network
protocols. This tool allows the creation and modification of network packets of various types, implementing functions for capturing and sniffing packets. Also, its learning curve is low if we compare it [to] programming with languages like C/C++. What's driving the implementation of socket programming? Are there any viable alternatives? The main advantage provided by sockets is that they have the ability to maintain the connection in real time and we can send and receive data from one end of the connection to another. For example, we could create our own client-server application that allows us to receive and send messages in real time, processing these messages in an asynchronous way. The main alternative to the use of sockets within Python we can find [in] the asyncio module that allows us to execute asynchronous calls concurrently with the aim of launching requests in parallel. You need to refer OSI Model in Depth for deep understanding https://www.geeksforgeeks.org/computer-network-tutorials/?ref=lbp
A file descriptor is a number that uniquely identifies an open file in a computer's operating system. It describes a data resource, and how that resource may be accessed. When a program asks to open a file — or another data resource, like a network socket — the kernel:
Standard input
The default data stream for input, for example in a command pipeline. In the terminal, this defaults to keyboard input from the user. stdin Standard output
The default data stream for output, for example when a command prints text. In the terminal, this defaults to the user's screen. stdout Standard error
The default data stream for output that relates to an error occurring. In the terminal, this defaults to the user's screen. stderr
A three-way handshake is a method used in a TCP/IP network to create a connection between a local host/client and server. It is a three-step method designed to allow both communicating ends to initiate and negotiate the parameters of the network TCP socket connection at the same time before data such as HTTP and SSH is transmitted. Multiple TCP socket connections can be transmitted in both directions simultaneously. A three-way handshake is also known as a TCP handshake or SYN-SYN-ACK, and requires both the client and server to exchange SYN (synchronization) and ACK (acknowledgment) packets before actual data communication begins. In fact, its name originates from the three messages transmitted by TCP before a session between the two ends is initiated. A three-way handshake is primarily used to create a TCP socket connection to reliably transmit data between devices. For example, it supports communication between a web browser on the client side and a server every time a user navigates the Internet. As soon as a client requests a communication session with the server, a three-way handshake process initiates TCP traffic by following three steps.
First, a connection between server and client is established, so the target server must have open ports that can accept and initiate new connections. The client node sends a SYN (Synchronize Sequence Number) data packet over an IP network to a server on the same or an external network. This SYN packet is a random sequence number that the client wants to use for the communication (for example, X). The objective of this packet is to ask/infer if the server is open for new connections.
When the server receives the SYN packet from the client node, it responds and returns a confirmation receipt – the ACK (Acknowledgement Sequence Number) packet or SYN/ACK packet. This packet includes two sequence numbers. The first one is ACK one, which is set by the server to one more than the sequence number it received from the client (e.g. X+1). The second one is the SYN sent by the server, which is another random sequence number (for example, Y). This sequence indicates that the server correctly acknowledged the client’s packet, and that is sending its own to be acknowledged as well.
The client node receives the SYN/ACK from the server and responds with an ACK packet. Once again, each side must acknowledge the sequence number received by incrementing it by one. So now it’s the turn of the client to acknowledge the server’s packet by adding one to the sequence number (in this case, Y+1), and resend it to the server. Upon completion of this process, the connection is created and the host and server can communicate. All these steps are necessary to verify the serial numbers originated by both sides, guaranteeing the stability of the connection. Since both hosts must acknowledge the connection parameters of the other side, a missing or out-of-order segment can be quickly detected before the actual data transfer process is initiated.
Client/server applications such as Web servers and browsers use TCP/IP to communicate. These Internet applications perform TCP/IP communications using the Berkeley Sockets interface (so named because the socket interface was introduced in Berkeley UNIX around 1982). The sockets interface consists of a library of routines that an application developer can use to create applications that can communicate with other applications on the Internet. There is even a Windows Sockets API (application programming interface—a fancy name for a library of programming functions that can be called by C programs) modeled after the Berkeley Sockets interface. The Winsock interface, as it’s known, provides a standard API that Windows programmers can use to write network applications. Even if you do not write network applications using sockets, you have to use many network applications. Knowledge of sockets can help you understand how network-based applications work, which, in turn, helps you find and correct any problems with these applications.
destination. Nor does UDP ensure that datagrams are delivered in the order they have been sent. UDP is used by applications that exchange small amounts of data at a time or by applications that do not need the reliability and sequencing of data delivery. For example, SNMP (Simple Network Management Protocol) uses UDP to transfer data. UDP is generally used by applications where each message is largely self-contained so that even if some of the messages don’t get through, it’s not critical. In the sockets model, a socket that uses UDP is referred to as a datagram socket. Basis Transmission control protocol (TCP) User datagram protocol (UDP) Type of Service TCP is a connection-oriented protocol. Connection-orientation means that the communicating devices should establish a connection before transmitting data and should close the connection after transmitting the data. UDP is the Datagram-oriented protocol. This is because there is no overhead for opening a connection, maintaining a connection, and terminating a connection. UDP is efficient for broadcast and multicast types of network transmission. Reliability TCP is reliable as it guarantees the delivery of data to the destination router. The delivery of data to the destination cannot be guaranteed in UDP. Error checking mechanism TCP provides extensive error- checking mechanisms. It is because it provides flow control and acknowledgment of data. UDP has only the basic error checking mechanism using checksums. Acknowledgment An acknowledgment segment is present. No acknowledgment segment. Sequence Sequencing of data is a feature of Transmission Control Protocol (TCP). this means that packets arrive in order at the receiver. There is no sequencing of data in UDP. If the order is required, it has to be managed by the application layer. Speed TCP is comparatively slower than UDP. UDP is faster, simpler, and more efficient than TCP. Retransmission Retransmission of lost packets is possible in TCP, but not in UDP. There is no retransmission of lost packets in the User Datagram Protocol (UDP). Header Length TCP has a (20-60) bytes variable length header. UDP has an 8 bytes fixed-length header. Weight TCP is heavy-weight. UDP is lightweight.
Basis Transmission control protocol (TCP) User datagram protocol (UDP) Handshaking Techniques Uses handshakes such as SYN, ACK, SYN-ACK It’s a connectionless protocol i.e. No handshake Broadcasting TCP doesn’t support Broadcasting. UDP supports Broadcasting. Protocols TCP is used by HTTP, HTTPs, FTP, SMTP and Telnet. UDP is used by DNS, DHCP, TFTP, SNMP, RIP, and VoIP. Stream Type The TCP connection is a byte stream. UDP connection is message stream. Overhead Low but higher than UDP. Very low. Ethernet header contains both Source and Destination MAC address , after which the payload of the frame is present. The last field is CRC which is used to detect the error. What is contained in an IP header? The header contains information about IP version, source IP address, destination IP address, time-to-live, etc. The payload of an IP packet is typically a datagram or segment of the higher-level transport layer protocol, but may be data for an internet layer (e.g., ICMP or ICMPv6) or link layer (e.g., OSPF) instead.