KEMBAR78
CN Lab File Lab File | PDF | Computer Network | Network Switch
0% found this document useful (0 votes)
18 views27 pages

CN Lab File Lab File

The document outlines a series of experiments focused on networking devices and configurations, including the study of hubs, switches, routers, and gateways, as well as hands-on activities with RJ-45 connectors and CAT-6 cables. It details procedures for designing LANs using Cisco Packet Tracer, including connecting multiple computers and routers, configuring IP addresses, and testing connectivity. The experiments emphasize practical skills in networking and the importance of understanding different network devices and topologies.

Uploaded by

Priyanshu Rajput
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)
18 views27 pages

CN Lab File Lab File

The document outlines a series of experiments focused on networking devices and configurations, including the study of hubs, switches, routers, and gateways, as well as hands-on activities with RJ-45 connectors and CAT-6 cables. It details procedures for designing LANs using Cisco Packet Tracer, including connecting multiple computers and routers, configuring IP addresses, and testing connectivity. The experiments emphasize practical skills in networking and the importance of understanding different network devices and topologies.

Uploaded by

Priyanshu Rajput
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
You are on page 1/ 27

1 lOMoARcPSD|50682290

2
3 lOMoARcPSD|50682290

2 Experiment No. 1
3 1. Study of different network devices (Hub, Switch, Router, and Gateway) in detail.

4 Objective:
5 To study different network devices (Hub, Switch, Router, and Gateway) in detail.
6
7 Resource Requirement:
8 H/W Requirements: Network devices such as hubs, switches, routers, and gateways for hands-on learning,
9 network cables to connect the device.
10 S/W Requirements: Cisco Packet Tracer.
11
12 Theory:
13 Hub:
14 A hub is a basic networking device that connects multiple devices in a network. It operates on the physical
15 layer of the OSI model and is responsible for simply repeating any incoming signal it receives to all the other
16 devices connected to it, regardless of whether the intended recipient is present. Hubs are now outdated and
17 have been largely replaced by switches. This is because hubs are prone to collisions, which can cause
18 significant network congestion.
19
20

21
22 Figure 1.1: Hub
23
24 Switch:
25 A switch is a networking device that operates on the data link layer of the OSI model. It is designed to connect
26 multiple devices in a network and selectively forward data packets to their intended destinations based on the
27 MAC addresses of the devices. Switches provide a more efficient and secure way of transferring data
28 compared to hubs. This is because switches create a dedicated path between the source and destination devices,
29 reducing network congestion and preventing data collisions.
30
31
32

33
34 Figure 1.2: Switch
35
36
37 Router:
38 A router is a networking device that operates on the network layer of the OSI model. It is responsible for
39 forwarding data packets between different networks or subnets based on the IP addresses of the devices.
40 Routers can connect different types of networks, including LANs, WANs, and the internet. They can also
41 perform functions such as network address translation (NAT) and firewalling, which help to protect the
42 network from unauthorized access and attacks.

4
5 lOMoARcPSD|50682290

43
44 Figure 1.3: Router
45
46

47 Gateway:
48 A gateway is a networking device that serves as the entry or exit point for a network. It can connect different
49 types of networks that use different communication protocols, allowing them to communicate with each other.
50 For example, a gateway can connect a LAN to the internet, translating between the protocols used by the LAN
51 and the internet. Gateways can also perform functions such as routing, filtering, and protocol conversion.
52

53
54 Figure 1.4: Gateway

55
56 Conclusion:
57 In conclusion, studying different network devices such as hubs, switches, routers, and gateways is critical for
58 building and maintaining effective network.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

6 2
7

8
9 lOMoARcPSD|50682290

77 Experiment No. 2
78
79 2. Study of handling and configuration of networking hardware like RJ-45 connector, CAT-6 cable, crimping tool,
80 and LAN tester.
81
82 Objective:
83 To study handling and configuration of networking hardware like RJ-45 connector, CAT-6 cable, crimping tool, and
84 LAN tester.
85
86 Resource Requirement:
87 H/W Requirements: RJ45, CAT cables, Crimping Tool, Printer, and Internet
88 Connection.
89
90 S/W Requirements: Cisco Packet Tracer.
91
92 Theory:
93 RJ-45 connector:
94 RJ45 cable is used for connecting the ALL HMI and engineer station through a switch to communicate with each
95 other. It is used to download the any modification, and which is made in graphics in engineering station. RJ45 cable
96 is also used for communicating the printer with computer. There are four pairs of wires in an Ethernet cable, and an
97 Ethernet connector (8P8C) has eight pin slots. Each pin is identified by a number, starting from left to right, with the
98 clip facing away from you.
99
100
101
102

103
104 Figure 2.1: RJ-45 Connector Pin Out
105
106
107 There are two kinds of Ethernet cable used for communication.
108 • Straight Through
109 • Crossover cable
110
111 Straight Through Cable:
112 Straight Through Ethernet cables are the standard cable used for almost all purposes and are often called “patch
113 cables”. It is highly recommended you duplicate the color order as shown on the left. Note how the green pair is not
114 side-by- side as are all the other pairs. This configuration allows for longer wire runs.

10
11 lOMoARcPSD|50682290

115 Figure

116 2.2: Straight Cable


117

118 Crossover Cable:


119 The purpose of a Crossover Ethernet cable is to directly connect one computer to another computer (or device)
120 without going through a router, switch, or hub.
121
122

123
124 Figure 2.3: Crossover Cable
125
126 CAT Cables:
127 The Cat, as you might know, is short for "Category." The term "Category" refers to the different levels of
128 performance in signal bandwidth, attenuation and crosstalk associated with each cable's design.
129 Category 1 cables are a 2-pair copper UTP designed for POTS (plain old telephone systems). CAT 5e is currently the
130 most used cable, mainly due to its low production cost and support for speeds faster than Cat 5 cables.
12 4
13

14
15 lOMoARcPSD|50682290

131
132 A Cat6 cable has a bandwidth capacity of 250 MHz, for example, and it offers you speeds of up to 10 Gbps. It's also
133 compatible with both Cat5 and Cat5E cables. “A Cat6 cable is used mainly for computer networks reaching a GB,
134 1000 Mbps or one Gbps of data transfer speed (DTR) or higher,”.
135

136
137 Figure 2.4: Different CAT cables
138
139 Crimping Tool:
140 Crimpers are tools used to make cold weld joints between two wires or a wire and a connector, such as lugs. Ideally,
141 the electrical and mechanical properties of the weld joint are as strong as the parent materials. Crimping tools are
142 sized according to the wire gauges (using AWG - American Wire Gauge) they can accept. Some come with
143 interchangeable die heads that allow for a wider range of wire sizes and connectors.
144
145 How to use:
146 First you will need to strip the length of wire that you want to crimp. Then, attach the connector. For crimping tools
147 with interchangeable dies, you will need to select the right die head for the connector by matching wire gauge ratings.
148 For die less crimpers, you will need to match to the proper groove. Finally, apply pressure, take out the newly
149 crimped connector, and give a few tugs to make sure you have a solid and secure connection.
150

151
152 Figure 2.5: Crimping Tool
153
154 LAN tester:
155 A LAN tester is an essential tool for any computer network lab to ensure that the LAN is functioning correctly and to
156 troubleshoot any problems that may arise.

16
17 lOMoARcPSD|50682290

157
158 Figure 2.6: LAN Tester
159
160 Conclusion:
161 Hence, we have implemented RJ45 and CAT cabling and connections using a crimping tool, and we have tested this
162 using a LAN tester.

18 6
19

20
21 lOMoARcPSD|50682290

163 Experiment No. 3


164
165 3. Design a LAN having 10 computers and connect them using Switch using start topology. Simulate your work
166 using CISCO Packet tracer.
167
168 Objective:
169 To design a LAN having 10 computers and connect them using a switch using star topology. The experiment will be
170 simulated using Cisco Packet Tracer.
171
172 Apparatus (Software) Requirement: Cisco Packet Tracer.
173
174 Procedure:
175 The steps involved in the experiment are as follows:
176
177 Step 1: Open Cisco Packet Tracer.
178 To open Cisco Packet Tracer, go to the Start menu and search for "Packet Tracer". Click on the Cisco Packet Tracer
179 icon to open the application.
180
181 Step 2: Create a new project.
182 To create a new project, click on the "File" menu and select "New Project". In the "New Project" dialog box, enter a
183 name for your project and click on the "Create" button.
184

185
186 Figure 3.1: Creating a new project.

187 Step 3: Add a switch to the project.


188 To add a switch to the project, click on the "Devices" menu and select "Add > Switch". In the "Add Switch" dialog
189 box, select the type of switch you want to add and click on the "Add" button.
190
191
192
193

194
195
196 Figure 3.2: Virtual Switch
197 Step 4: Connect 10 computers to the switch using Ethernet cables.

22
23 lOMoARcPSD|50682290

198 To connect 10 computers to the switch using Ethernet cables, click on the "Devices" menu and select "Add > PC". In
199 the "Add PC" dialog box, select the type of PC you want to add and click on the "Add" button. Repeat this step 9
200 more times to add 10 PCs to the project.
201 Once you have added 10 PCs to the project, connect each PC to the switch using an Ethernet cable. To do this, drag
202 the Ethernet cable from the PC to the switch port.
203
204
205

206
207 Figure 3.3: Connection to switch using Ethernet cables.
208
209 Step 5: Configure the IP addresses of the computers.
210 To configure the IP addresses of the computers, click on the "Config" tab of the PC's properties window. In the "IP
211 Address" field, enter the IP address of the PC. In the "Subnet Mask" field, enter the subnet mask for the network.
212 Repeat this step for all 10 PCs.
213

214
215 Figure 3.4: IP Configuration
216
217 Step 6: Test the connectivity between the computers by pinging each computer from another computer.
218 To test the connectivity between the computers, open a command prompt on one of the computers. In the command
219 prompt, type "ping" followed by the IP address of another computer. If the computer is reachable, you will see a
220 response message. Repeat this step for all 10 computers.

24 8
25

26
27 lOMoARcPSD|50682290

221
222
223 Figure 3.5: Testing Connectivity
224 Finally, we should now have a LAN with 10 computers connected to a switch using star topology in Cisco Packet
225 Tracer. We can verify this by opening the Topology window, where we should see all 10 computers connected to the
226 switch.
227
228 Conclusion:
229 In conclusion, the experiment was a success. The 10 computers were successfully connected to the switch using star
230 topology in Cisco Packet Tracer. The connectivity between the computers was also verified by pinging each
231 computer from another computer.
232
233 This experiment has demonstrated that star topology is a reliable and efficient way to connect computers in a LAN. It
234 is also a very scalable topology, as it can be easily expanded by adding more computers.
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

28
29 lOMoARcPSD|50682290

261 Experiment No. 4


262
263 4. Design a network having two LAN connected via a Router? Configure the router using CISCO Packet Tracer.
264
265 Objective:
266 To design a network with two LANs connected via a router and configure the router using Cisco Packet Tracer.
267
268 Apparatus Requirement:
269 Cisco Packet Tracer software, Two switches, Four PCs, Ethernet cables.
270
271 Procedure:
272 Step 1: Open Cisco Packet Tracer and create a network with two switches.
273 o Launch Cisco Packet Tracer.
274 o Drag and drop two switches onto the workspace.
275
276
277
278

279
280 Figure 4.1: Virtual Switches
281
282
283 Step 2: Assign IP addresses to the PCs.
284 o Drag and drop two PCs for each switch onto the workspace.
285 o Double-click on each PC and assign IP addresses, subnet masks, and default gateways to the network
286 interfaces of the PCs.
287
288

289
290 Figure 4.2: Assigning IP
291
292
293 Step 3: Establish connections between the two switches using a router.
294 o Drag and drop a router onto the workspace.
295 o Connect the LAN-1 switch's interface (e.g., GigabitEthernet0/1) to one of the router's interfaces (e.g.,
296 GigabitEthernet0/0).
297 o Connect the LAN-2 switch's interface (e.g., GigabitEthernet0/1) to another interface on the router (e.g.,
298 GigabitEthernet0/1).
299

30 10
31

32
33 lOMoARcPSD|50682290

300
301 Figure 4.3: Establishing connection using Router.
302
303 Step 4: Configure the router.
304 Double-click on the router to open the configuration dialog box.
305 Click on the "CLI" (Command Line Interface) tab to access the router's command line interface.
306
307 Step 5: Configure the router interfaces.
308 Enter the following commands to configure the interfaces of the router:
309
310 Router> enable
311 Router# configure terminal
312 Router(config)# interface GigabitEthernet0/0
313 Router(config-if)# ip address 10.10.10.3 255.0.0.0
314 Router(config-if)# no shutdown
315 Router(config-if)# exit
316 Router(config)# interface GigabitEthernet0/1
317 Router(config-if)# ip address 192.168.1.3 255.255.255.0
318 Router(config-if)# no shutdown
319 Router(config-if)# exit
320 Router(config)# exit
321

322 Step 6: Configure RIP (Routing Information Protocol) on the router.


323 Enter the following commands to configure RIP:
324
325 Router(config)# router rip
326 Router(config-router)# network 10.10.10.3
327 Router(config-router)# network 192.168.1.3
328 Router(config-router)# exit
329 Router(config)# exit
330

331 Step 7: Test the connection.


332 Open a command prompt on each PC and try to ping the IP address of the other PC.
333 For example, on PC1, enter the following command:
ping
10.10.10.3
334
335 On PC2, enter the following command:
ping
192.168.1.3
336
337 Result:

34
35 lOMoARcPSD|50682290

338
339 The pings are successful, it means that the two LANs are connected via the router.
340
341 Conclusion:
342 We have successfully designed a network with two LANs connected via a router using Cisco Packet Tracer and tested
343 the connectivity between the PCs.

36 12
37

38
39 lOMoARcPSD|50682290

344 Experiment No. 5


345

346 5. Design a network having three LAN and each LAN connected to a Router. Each Router is connected to each
347 other using Serial connection. Configure the Router using command line interface of Packet Tracer?
348
349 Objective:
350 To design a network consisting of three LANs, with each LAN connected to a router, and to establish connectivity
351 between the LANs by interconnecting the routers using serial connections.
352
353 Apparatus Requirement:
354 Cisco Packet Tracer software, Switches, PCs, Ethernet cables.
355
356 Procedure:
357 Step 1: Launch Cisco Packet Tracer
358 Start by launching Cisco Packet Tracer on your computer.
359
360 Step 2: Add Devices
361 Click on the "Devices" tab located on the left sidebar. Drag and drop the following devices onto the workspace:
362 o 3 PCs (for the LANs) o 3 Routers
363
364

365
366 Figure 5.1: Adding PCs and Routers
367
368 Step 3: Connect Devices
369 To connect the devices, select the "Connections" tab from the left sidebar. Choose the appropriate cable type and
370 connect the devices as follows:
371 o Connect each LAN to its respective router using
372 Copper Straight-through cables. o Connect the routers to
373 each other using Serial DCE/DTE cables.
374

40
41 lOMoARcPSD|50682290

375
376 Figure 5.2: Connecting the devices.
377
378

379 Step 4: Configure IP Addresses


380 Double-click on each PC to open the configuration dialog box. Set the IP address, subnet mask, and default gateway
381 for each PC's network interface in their respective LANs.
382
383 Step 5: Configure the Routers
384 Double-click on each router to open the configuration dialog box. Click on the "CLI" (Command Line Interface) tab
385 to access the router's command line interface.
386
387 Step 6: Configure Router Interfaces
388 On each router's command line interface, enter the following commands to configure the interfaces connected to the
389 LANs:
390
391 Router 1:
392 Router>enable
393 Router#configure terminal
394 Router(config)#interface FastEthernet0/0
395 Router(config-if)#ip address 192.168.1.3 255.255.255.0
396 Router(config-if)#no shutdown
397 Router(config-if)#exit
398 Router(config)#interface Serial0/0/0
399 Router(config-if)#ip address 192.168.4.1 255.255.255.0
400 Router(config-if)#no shutdown
401 Router(config-if)#exit
402
403 Router 2:
404 Router>enable
405 Router#configure terminal
406 Router(config)#interface FastEthernet0/0
407 Router(config-if)#ip address 192.168.2.3 255.255.255.0
408 Router(config-if)#no shutdown
409 Router(config-if)#exit
410 Router(config)#interface Serial0/0/0
411 Router(config-if)#ip address 192.168.4.2 255.255.255.0
412 Router(config-if)#no shutdown
413 Router(config-if)#exit
414 Router(config)#interface Serial0/1/0
415 Router(config-if)#ip address 192.168.5.1 255.255.255.0

42 14
43

44
45 lOMoARcPSD|50682290

416 Router(config-if)#no shutdown


417 Router(config-if)#exit

418 Router 3:
419 Router>enable
420 Router#configure terminal
421 Router(config)#interface FastEthernet0/0
422 Router(config-if)#ip address 192.168.3.3 255.255.255.0
423 Router(config-if)#no shutdown
424 Router(config-if)#exit
425 Router(config)#interface Serial0/0/0
426 Router(config-if)#ip address 192.168.5.2 255.255.255.0
427 Router(config-if)#no shutdown
428 Router(config-if)#exit

429 Step 7: Configure Static Routes for Inter-LAN Communication


430 Access the command line interface (CLI) of each router by double-clicking on the router and selecting the "CLI" tab.
431 Enter the following command to configure a static route on each router:
432
433 Router 1:
434 Router>enable
435 Router#configure terminal
436 Router(config)#ip route 192.168.2.0 255.255.255.0 192.168.4.2
437 Router(config)#ip route 192.168.5.0 255.255.255.0 192.168.4.2
438 Router(config)#ip route 192.168.3.0 255.255.255.0 192.168.4.2
439
440 Router 2:
441 Router>enable
442 Router#configure terminal
443 Router(config)#ip route 192.168.1.0 255.255.255.0 192.168.4.1
444 Router(config)#ip route 192.168.3.0 255.255.255.0 192.168.5.2
445
446 Router 3:
447 Router>enable
448 Router#configure terminal
449 Router(config)#ip route 192.168.2.0 255.255.255.0 192.168.5.1
450 Router(config)#ip route 192.168.4.0 255.255.255.0 192.168.5.1
451 Router(config)#ip route 192.168.1.0 255.255.255.0 192.168.5.1
452
453 Step 8: Test Connectivity
454 Now that the network is set up and configured, you can test the connectivity between the PCs. Open a command
455 prompt on each PC and try to ping the IP address of PCs in different LANs.
456
457 For example, on PC1 (192.168.1.2), enter the following command:
458 > ping 192.168.2.2
459
460 And on PC2 (192.168.2.2), enter the following command:
461 > ping 192.168.3.1
462
463 Result:
464 The pings are successful, it means that the LANs are connected via the routers and can communicate with each other.
465
466 Conclusion:
467 We have successfully designed and configured a network with three LANs, each connected to a router, and the
468 routers interconnected using serial connections.

46
47 lOMoARcPSD|50682290

469 Experiment No. 6


470 6. Design a wireless LAN using CISCO Packet Tracer.

471 Objective:
472 To design and implement a wireless LAN using CISCO Packet Tracer.
473
474 Apparatus Requirement:
475 Cisco Packet Tracer (Software), Wireless router (e.g., CISCO 1841), Wireless access points (e.g., CISCO Aironet
476 3700 series), Ethernet Cables, Wireless devices (e.g., laptops, smartphones).
477
478 Procedure:
479 The Step-by-Step Process are:
480 Step 1: Launch CISCO Packet Tracer software on your computer.
481 Step 2: Create a new project and select the appropriate workspace.
482 Step 3: Drag and drop a wireless router from the device panel onto the workspace.
483 Step 4: Connect the laptops to the wireless router using wireless connections.
484 Step 5: Configure the basic parameters of the router, including hostname, IP address, and subnet mask.
485 Step 6: Double-click on each Laptop to open the configuration dialog box. Set the IP address and subnet mask for
486 each Laptop’s network interface in their respective LANs.
487
488

489
490 Figure 6.1: Wireless LAN Connection
491
492
493
494 Step 7: Verify that the laptops are able to connect to the wireless network.

495
48 16
49

50
51 lOMoARcPSD|50682290

496 Figure 6.2: Wireless LAN Simulation using CMD


497
498

499 Result:
500 We will have successfully designed a wireless LAN using Cisco Packet Tracer.
501
502 Conclusion:
503 The experiment was successful in that the laptops were able to connect to the wireless network and access the
504 internet.

52
53 lOMoARcPSD|50682290

505 Experiment No. 7


506 7. Write a program to implement UDP Datagram socket.
507
508 Objective:
509 To learn about the basic concepts of UDP Datagram sockets and to implement a UDP Datagram socket in C++.
510
511 Requirement:
512 A text editor, basic understanding of C++ and basic understanding of computer networks.
513
514 C++ Program to implement UDP Datagram socket:
515
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a UDP socket.
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd <
0) { cout << "Error creating socket: " << strerror(errno)
<< endl; return 1;
}

// Bind the socket to a port.


struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
< 0) { cout << "Error binding socket: " << strerror(errno) <<
endl; return 1;
}

// Receive a message from the client. char buffer[1024]; int


bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL,
NULL); if (bytes_received < 0) { cout << "Error receiving message: "
<< strerror(errno) << endl; return 1;
}

// Print the message. cout << "Received


message: " << buffer << endl;

// Send a message back to the client.


char message[] = "Hello, client!";
sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr));

// Close the socket.


close(sockfd);

return 0;
}

54 18
55

56
57 lOMoARcPSD|50682290

516 Result:
517 We were able to successfully implement a UDP Datagram socket in C++. We were able to create a socket, bind it to
518 a port, receive a message from a client, and send a message back to the client. We also tested our code thoroughly to
519 make sure it worked correctly.
520
521 Conclusion:
522 We implemented a UDP Datagram socket in C++. We were able to create a socket, bind it to a port, receive a
523 message from a client, and send a message back to the client. We also tested our code thoroughly to make sure
524 it worked correctly.

58
59 lOMoARcPSD|50682290

525 Experiment No. 8


526 8. Write a program to create TCP Client and Server.
527
528 Objective:
529 To implement a TCP Client and Server in C++.
530
531 Requirement:
532 A text editor, basic understanding of C++ and basic understanding of computer networks.
533
534 C++ Program to create a TCP Client and Server:
535 Server:
536
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a TCP socket. int sockfd = socket(AF_INET,
SOCK_STREAM, 0); if (sockfd < 0) { cout << "Error
creating socket: " << strerror(errno) << endl; return 1;
}

// Bind the socket to a port.


struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
< 0) { cout << "Error binding socket: " << strerror(errno) <<
endl; return 1;
}

// Listen for connections.


listen(sockfd, 1);

// Accept a connection.
int clientfd = accept(sockfd, NULL, NULL); if (clientfd < 0)
{ cout << "Error accepting connection: " << strerror(errno)
<< endl; return 1;
}

// Receive a message from the client.


char buffer[1024]; int bytes_received = recv(clientfd,
buffer, sizeof(buffer), 0); if (bytes_received < 0) { cout
<< "Error receiving message: " << strerror(errno) << endl;
return 1;
}

// Print the message. cout << "Received message from


client: " << buffer << endl;
// Send a message back to the client. char message[] = "Hello,
60 20
61

62
63 lOMoARcPSD|50682290

client!"; send(clientfd, message, strlen(message), 0);

// Close the sockets. close(sockfd);


close(clientfd);
return 0;
}
537

538 Client:
539
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a TCP socket. int sockfd = socket(AF_INET,
SOCK_STREAM, 0); if (sockfd < 0) { cout << "Error
creating socket: " << strerror(errno) << endl; return 1;
}

// Connect to the server. struct


sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
< 0) { cout << "Error connecting to server: " << strerror(errno)
<< endl; return 1;
}

// Send a message to the server. char


message[] = "Hello, server!";
send(sockfd, message, strlen(message),
0);

// Receive a message from the server.


char buffer[1024]; int bytes_received = recv(sockfd,
buffer, sizeof(buffer), 0); if (bytes_received < 0) { cout
<< "Error receiving message: " << strerror(errno) << endl;
return 1;
}

// Print the message.


cout << "Received message from server: " << buffer << endl;

// Close the sockets.


close(sockfd);

return 0;
}

64
65 lOMoARcPSD|50682290

540 To run the program, compile the server and client programs separately. Then, run the server program first, followed
541 by the client program. The client program will send a message to the server, and the server program will send a
542 message back to the client.
543
544 Result:
545 We were able to successfully create a TCP Client and Server in C++.
546
547 Conclusion:
548 We learned how to create TCP sockets in C++, how to bind sockets to ports, how to listen for connections, how to
549 accept connections, how to receive messages, how to send messages, and how to close sockets.

66 22
67

68
69 lOMoARcPSD|50682290

550 Experiment No. 9


551 9. Write a program to create UDP Client and Server.
552
553 Objective:
554 To implement a UDP Client and Server in C++.
555
556 Requirement:
557 A text editor, basic understanding of C++ and basic understanding of computer networks.
558
559 C++ Program to create a TCP Client and Server:
560 Server:
561
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a UDP socket. int sockfd = socket(AF_INET,
SOCK_DGRAM, 0); if (sockfd < 0) { cout << "Error creating
socket: " << strerror(errno) << endl; return 1;
}

// Bind the socket to a port.


struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
< 0) { cout << "Error binding socket: " << strerror(errno) <<
endl; return 1;
}

// Receive a message from the client.


char buffer[1024]; int bytes_received = recvfrom(sockfd, buffer,
sizeof(buffer), 0, NULL, NULL); if (bytes_received < 0) { cout <<
"Error receiving message: " << strerror(errno) << endl; return 1;
}

// Print the message. cout << "Received message from


client: " << buffer << endl;

// Send a message back to the client.


char message[] = "Hello, client!";
sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr));

// Close the sockets.


close(sockfd);

return 0;
}
562 Client:

70
71 lOMoARcPSD|50682290

563
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a UDP socket. int sockfd = socket(AF_INET,
SOCK_DGRAM, 0); if (sockfd < 0) { cout << "Error creating
socket: " << strerror(errno) << endl; return 1;
}

// Set the destination address.


struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;

// Send a message to the server.


char message[] = "Hello,
server!";
sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr));

// Receive a message from the server. char buffer[1024]; int


bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL,
NULL); if (bytes_received < 0) { cout << "Error receiving message: "
<< strerror(errno) << endl; return 1;
}

// Print the message.


cout << "Received message from server: " << buffer << endl;

// Close the sockets.


close(sockfd);
return 0;
}
564

565 To run the program, compile the server and client programs separately. Then, run the server program first, followed
566 by the client program. The client program will send a message to the server, and the server program will send a
567 message back to the client.
568
569 Result:
570 we were able to successfully create a UDP Client and Server in C++. The client and server programs were able to
571 send and receive messages to each other.
572
573 Conclusion:
574 We learned how to create UDP sockets in C++, how to bind sockets to ports, how to receive messages, how to send
575 messages, and how to close sockets. We also learned how to use UDP sockets to create a simple chat application.
576
577
578
72 24
73

74
75 lOMoARcPSD|50682290

579 Experiment No. 10


580 10. Write a program to simulating PING and TRACEROUTE commands.
581
582 Objective:
583 To create a program that simulates the PING and TRACEROUTE commands.
584
585 Requirement:
586 A text editor, basic understanding of C++ and basic understanding of computer networks.
587
588 C++ Program to simulate PING and TRACEROUTE commands:
589
#include <iostream>
#include <sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> using
namespace std;

int main() {
// Create a UDP socket.
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd <
0) { cout << "Error creating socket: " << strerror(errno)
<< endl; return 1;
}

// Set the destination address.


struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr =
INADDR_ANY;

// Send a PING request.


char message[] = "PING";
sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr));

// Receive a PING response.


char buffer[1024]; int bytes_received = recvfrom(sockfd, buffer,
sizeof(buffer), 0, NULL, NULL); if (bytes_received < 0) { cout <<
"Error receiving message: " << strerror(errno) << endl; return 1;
}

// Print the PING response.


cout << "Received PING response from: " << buffer << endl;

// Send a TRACEROUTE request.


int ttl = 1; while (ttl <=
30) { char message[] =
"TRACEROUTE";
sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&servaddr,
sizeof(servaddr));

// Receive a TRACEROUTE response.


bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL,
NULL); if (bytes_received < 0) {

76
77 lOMoARcPSD|50682290

cout << "Error receiving message: " << strerror(errno) << endl;
return 1;
}

// Print the TRACEROUTE response. cout << "Received TRACEROUTE response from: "
<< buffer << " with TTL: " << ttl << endl;
ttl++;
}

// Close the sockets.


close(sockfd);
return
0;
}

590
591 Result:
592 We were able to successfully create a program that simulates the PING and TRACEROUTE commands. The
593 program was able to send and receive PING and TRACEROUTE messages, and it was able to print the results of the
594 PING and TRACEROUTE commands.
595
596 Conclusion:
597 We learned how to create a program that simulates the PING and TRACEROUTE commands in C++. We also
598 learned how to use the PING and TRACEROUTE commands to troubleshoot network problems.

78 26
79

80

You might also like