0% found this document useful (0 votes)
96 views10 pages

Socket Programming in Network Applications

This document outlines a laboratory work activity focused on network programming with sockets, aiming to teach students how to write and debug socket applications. It covers theoretical aspects of socket programming, including TCP and UDP communication, and provides practical activities for students to implement various network topologies using their chosen programming language. The document also includes implementation templates and hints for creating relay nodes and other network configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views10 pages

Socket Programming in Network Applications

This document outlines a laboratory work activity focused on network programming with sockets, aiming to teach students how to write and debug socket applications. It covers theoretical aspects of socket programming, including TCP and UDP communication, and provides practical activities for students to implement various network topologies using their chosen programming language. The document also includes implementation templates and hints for creating relay nodes and other network configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LABORATORY WORK

Application Layer: Network programming with sockets

1. Objectives
Prerequisite: Use a working software environment for your preferred programming language
(Java, C#, Python, C/C++, etc.)
At the end of the activity, students will be able to write software for socket applications and
debug network applications using Wireshark.

2. Theoretical considerations
The current practical work focuses on the Transport and Application layers of the ISO/OSI
stack (Figure 8.1).

Figure 8.1 Network stack models and PDU naming in each level. The arrows indicate the
addressed layers in the current activity

This practical activity addresses the programming side of software engineering and
communication offered through the use of network sockets in a desktop environment. Socket
programming is available in any high level programming language and sockets are
transmitting information at the Application Layer. Sockets are used in different types of
applications, such as: Client-Server, peer-2-peer systems, inter-process communication (on
the same machine).
Network sockets can be constructed to use both IPv4 and IPv6 addresses. A socket is the
combination of an IP address and a port number for use in a network application. A network
application provides connectivity between different network devices. It is not possible to bind
a socket to a port that is already in use by any other application, however the same port may
be used concurrently by TCP and UDP transport layer protocols. The IP addresses identify the
network device, but the port number uniquely identifies each running application on the
current network device.
The operations that an application can perform on a socket are the following:
 Create - Creation of a socket object
 Bind - Configure the socket object to use a local pair of IP address and port
number to accept connections
 Listen - Program the socket to wait for incoming connections
 Accept - Accept the incoming connection
 Connect - This operation is used by a client that wants to connect to a server
 Send - Used to send data over the socket to the remote destination
 Receive - Used to receive data which is sent from a remote location
 Close - close the connection between the two sockets

2.1. Working with sockets on the local machine


 In order to simulate a network on the local machine, the entire available loopback
range: [Link] - [Link] can be used. The loopback network interface is
available only on the local host and is mainly used for diagnostics and standalone
network applications. Therefore, a simulated local network can use these IP addresses
for communication. In order to test and confirm that this range can be used, a ping
command can be run from the local terminal to verify connectivity to said IP addresses
(Figure 8.2):

Figure 8.2 Loopback addresses testing

 It is also possible to assign multiple valid IP addresses on the local interface, but this
has to be done manually by statically allocating IP addresses to the interface. In this
case, running the ipconfig command would show all the IP addresses assigned to the
same interface. See an example below (Figure 8.3):
Figure 8.3 IP configuration view - CLI

 After having assigned the IP addresses, sockets can now be created to use these IP
addresses.

2.2. TCP Sockets


 TCP (Transmission Control Protocol) sockets are connection oriented and represent a
reliable data transmission mechanism that allows data to be received and processed in
the same order it was transmitted.
 The Figure 8.4 shows a Wireshark traffic capture on the “Adaptor for loopback traffic
capture”. The screenshot shows a client-server communication via sockets using the
loopback addresses. The applied filter is [Link] == 1234. The server is bound to the
[Link] address and awaits connections on port number 1234 while the client binds on
the [Link] address sending a payload of 14 bytes to the server.
Figure 8.4 Wireshark capture of TCP socket communication

 The screenshot highlights the TCP mechanism represented by acknowledgement (ACK)


packets. The first three packet exchanges (Figure 8.4) represent the 3-way handshake
which is needed to establish the connection for any TCP connection (Figure 8.5) and
following that the packet sending the payload is visible. This handshake assures that both
hosts want to communicate and acknowledge the other host’s intention to communicate.

Figure 8.5 TCP 3-way handshake

 The Wireshark image shows a socket communication which remains open.


o Answer the following question while working on the practical activity: If the
socket connection is closed, what are the TCP flags that are set in order to
close the connection?
2.3. UDP Sockets
 In contrast with the TCP sockets, UDP (User Datagram Protocol) sockets are not
connection oriented and they do not provide reliable communication. This means they
do not guarantee that network packets are delivered to the destination. The Figure 8.6
represents a Wireshark capture (again on the “Adaptor for loopback traffic capture”) of
a UDP communication between two hosts. The applied filter is [Link] == 1234. As
can be seen, there is no handshake performed and there aren’t any ACK packets being
transmitted.

Figure 8.6 Wireshark capture of UDP socket communication

TCP and UDP socket communications are presented in Figure 8.7 a and b.

a. TCP socket communication b. UDP socket communication

Figure 8.7 Socket communications


1. Implementation template
 A network device can function in 3 modes:
o Server: Receiving device
o Client: Sending device
o Relay: Acting as an intermediary node in a communication and acts as both
sending and receiving device. This type of network node can be encountered in
Wireless Sensor Networks (WSN) where not all sensor nodes are in the
wireless range of the collector device, therefore some nodes need to forward
the information to the sink node (the collector node).

 This chapter provides a template for implementing the relay communication node
using OOP concepts (the template is written in pseudocode, not in a particular
programming language). This is not the only possibility to organize the code, students
can choose any software design methodology they are comfortable with.

Relay node implementation template


class RelayNode {
public:
RelayNode(IPAddress, serverPortNr) {
m_server.listen(IPAddress, serverPortNr);
m_client.bind(IPAddress);

m_server.onReceive() => {
ByteArray receivedData = m_server.readData();
m_client.connectToHost(m_nextHopIpAddress, m_nextHopPortNr);
m_client.sendData(receivedData);
m_client.close();
}
}
void setNextHopInformation(nextHopIPAddress, nextHopServerPortNr) {
m_nextHopIpAddress = nextHopIPAddress;
m_nextHopPortNr= nextHopServerPortNr;
}

private:
Server m_server; // server instance accepting connections
Client m_client; // client instance sending data to the next hop
IPAddress m_nextHopIpAddress; // next hop address used by the client instance
int m_nextHopPortNr; // next hop port nr used by the client instance
}

void main() {
RelayNode relay([Link], 1234);
[Link]([Link], 2345)

// run application event loop
}
3. Practical activity
 Each student will be assigned one of the topologies below and the simulation scenario
has to be implemented in software.
 Besides the constraints imposed by each simulation scenario, the common tasks for
each implementation are the following:
o Use a programming language of choice to implement the network simulation
o Use the loopback address range for addressing: [Link] – [Link]
o Test the implementation using Wireshark
o Deliver the implementation (source code or link to online code versioning
repository)
o Provide a Wireshark capture to prove the communication between different IP
addresses
o Inspect the ratio between total delivered payload against the relevant
application layer traffic / the ratio between the total packet length (headers and
data) compared to the length of data sent (use Wireshark statistics or manual
packet inspection)
o Depending on the implemented simulation, research the headers for TCP
and/or UDP protocols. Using Wireshark, identify the header elements in the
captured traffic

3.1 Ring communication


 Three computers are communicating in a single direction creating a loop (Figure 8.8)
 One of the computers initiates the communication sending the value ‘1’
 Upon receipt, each network device increments the received value and sends it to the
next device
 The communication ends when the delivered payload reaches the value ‘100’
 Implementation hints:
o Implement a single class which is instantiated 3 times with different
communication parameters (reuse the code and do not duplicate it for each
instance)
o All communication uses TCP sockets (optional)
Figure 8.8 Ring communication network topology

3.2 Node selector


 There are three nodes in the topology: N1, N2, N3 (Figure 8.9)
 N1 increments a value 100 times and after every increment it sends the value to either
N2 or N3 which are selected randomly for transmission
 When N2 receives an integer value which is a multiple of 3 it will send an ACK packet
back to N1
 When N3 receives an integer value which is a multiple of 5 it will send an ACK packet
back to N1
 Implementation hints:
o Implement a single class for N2 and N3 which is instantiated with different
communication parameters (reuse the code and do not duplicate it for each
instance)
o All communication uses UDP sockets (optional)
Figure 8.9 Node selector network topology

3.3 Relay nodes


 There are four nodes in the topology (Figure 8.10), Sender and three possible
destinations (D1, D2, and D3)
 The Sender node is transmitting 100 packets containing an integer number randomly
to one of the 3 possible destinations (D1, D2 or D3)
 After each packet transmission the integer number is incremented
 Every node can only send data to the next hop to which it is connected to, therefore a
packet from the Sender to D3 must pass through D1 and D2

Figure 8.10 Relay nodes network topology

 Implementation hints:
o The data payload that is transmitted via the socket has to contain the target IP
address, so the payload has the following format (Figure 8.11):

Target IP address Value


Figure 8.11 Payload format
o Every time a node receives a packet it verifies whether the received payload’s
target IP address is the same as the current node IP address. If it is identical,
the communication stops here, otherwise the data is forwarded to the next hop.
o Implement a single class for D1, D2 and D3 which is instantiated with different
communication parameters (reuse the code and do not duplicate it for each
instance)

Common questions

Powered by AI

A relay node in a network acts both as a sender and receiver to facilitate data transmission between devices not directly reachable due to range or network topology limitations. It listens for incoming connections, reads data, and forwards this data to the next hop. This is implemented through a design using a server instance to accept connections and a client instance to send the data to the next hop. By using relay nodes, networks such as Wireless Sensor Networks can ensure data reaches its destination by routing through intermediary nodes .

The loopback address plays a crucial role in network programming by allowing developers to simulate network environments on a single machine. It uses the IP range 127.0.0.0 to 127.255.255.255, where 127.0.0.1 is the most commonly used address for local host testing. This provides a way to test network applications without requiring actual network hardware, facilitating debugging and development in a controlled environment .

Using OOP concepts for implementing relay nodes offers several advantages, such as encapsulation, which allows for modular code that is easier to understand and maintain. It also facilitates code reuse, allowing the same class to be instantiated for different roles or stages in a network. However, potential drawbacks include increased overhead in terms of memory and processing time due to the abstraction layers involved, as well as the complexity of managing object lifecycles and interactions in a multi-threaded network environment .

In relay node networks, the payload format is critical for correctly routing packets to their intended destination. The format typically includes the target IP address alongside the data to ensure that each node in the network can determine whether to process the data or forward it to the next hop. This organization of packets is essential in relay systems where multiple nodes are responsible for forwarding data through various routes, ensuring that the intended recipient ultimately receives complete and correct information .

In Wireshark, the successful establishment of a TCP connection is identified by the sequence of SYN, SYN-ACK, and ACK packets, which constitutes the three-way handshake. For connection closure, the presence of FIN (finish) and ACK packets signifies the process of terminating a TCP session. Each side of the communication sends a FIN packet to signal termination, and an ACK in response confirms the closure . Overall, these flags in packet headers ensure that both connection establishment and closure are appropriately managed .

The three-way handshake in TCP communication is a method used to establish a reliable connection between a client and a server. It involves three steps: First, the client sends a SYN (synchronize) message to the server. Second, the server responds with a SYN-ACK (synchronize-acknowledge) message to acknowledge the receipt of the SYN request. Finally, the client sends an ACK (acknowledge) message to confirm the SYN-ACK from the server. This process ensures that both parties are ready to communicate, and it allows the connection to be established in a manner that confirms each side’s intention and capability to proceed .

TCP sockets are connection-oriented and provide reliable data transmission, ensuring data is received and processed in the same order it was sent. This is suitable for applications where data integrity is crucial, like file transfers or web page loading. In contrast, UDP sockets are connectionless and do not confirm or guarantee the delivery of packets, making them suitable for applications like streaming or gaming, where speed is favored over reliability . The choice between TCP and UDP depends on the application's need for data accuracy versus transmission speed .

In the node selector topology, node N1 increments a value 100 times and sends it randomly to either N2 or N3. When N2 receives a value that is a multiple of 3, it sends an ACK back to N1. Similarly, when N3 receives a value that is a multiple of 5, it sends an ACK back to N1 . This process involves both random transmission selection by N1 and conditional acknowledgment by N2 and N3, illustrating the use of logic in network protocols .

The loopback address range allows developers to create and test network applications locally without the need for external network setup, facilitating easier debugging and iteration in a software development environment. However, its use is limited to the local machine, meaning it cannot be utilized for testing interactions with remote systems or network configurations involving multiple devices. This makes it ideal for initial development but inadequate for full-scale testing requiring real-world network interactions .

Binding a socket to an IP address and port number is crucial as it allows the socket to accept incoming connections on a specified interface and port, making it a contact point for network communication. The restriction is that a socket cannot bind to a port that is already in use by another application, ensuring that each network service remains distinct and does not interfere with others. However, the same port number can be reused by both TCP and UDP protocols since they are distinct transport layers, allowing similar services to run concurrently under different protocols .

You might also like