Echo UDP
Echo UDP
: 04
Date: 22.08.2024
echoUDP and echoTCP communication
Aim:
To implement the echoUDP communication and echoTCP communication.
4.a.Procedure:
Server (server.c):
1. Initialize: Define the server port as 8877, and set up `server_address` with address
family `AF_INET`, port using `htons(SERVER_PORT)`, and IP as `INADDR_ANY`.
2. Create UDP Socket: Use `socket(PF_INET, SOCK_DGRAM, 0)` to create a UDP
socket. If socket creation fails, print an error message and exit.
3. Bind Socket: Bind the socket to `server_address`; print an error and exit if bind fails.
4. Communication Loop: Enter a loop to handle client messages:
a. Use `recvfrom` to receive data from the client, then print the message and
client IP.
b. Use `sendto` to send the received message back to the client.
5. End: The server continues to run, echoing messages for each client.
Client (client.c):
Initialize: Define server name as "localhost" and port as 8877.
Set Server Address: Create `server_address` with address family `AF_INET`, set
port using `htons(server_port)`, and set IP using `inet_pton`.
Create UDP Socket: Use `socket(PF_INET, SOCK_DGRAM, 0)` to create a UDP
socket; exit if socket creation fails.
Send Data: Define the message `data_to_send` and use `sendto` to send it to the
server.
Receive Echo: Use `recvfrom` to receive the echoed message from the server, then
print it.
Close Socket: Close the socket to complete the client session.
4.a.Server.c
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int SERVER_PORT = 8877;
1
2022503524
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
if ((bind(sock, (struct sockaddr *)&server_address,sizeof(server_address))) < 0) {
printf("could not bind socket\n");
return 1;
}
struct sockaddr_in client_address;
int client_address_len = 0;
while(true)
{
char buffer[500];
int len = recvfrom(sock, buffer, sizeof(buffer), 0,(struct sockaddr
*)&client_address, &client_address_len);
buffer[len] = '\0';
printf("received: '%s' from client %s\n", buffer, inet_ntoa(client_address.sin_addr));
sendto(sock, buffer, len, 0, (struct sockaddr *)&client_address,
sizeof(client_address));
}
return 0;
}
4.a.Output:
4.a.Client.c
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
2
2022503524
int main() {
const char* server_name = "localhost";
const int server_port = 8877;
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
inet_pton(AF_INET, server_name, &server_address.sin_addr);
server_address.sin_port = htons(server_port);
int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
const char* data_to_send = "Hi! I'm the echo UDP client";
int len =sendto(sock, data_to_send, strlen(data_to_send), 0, (struct
sockaddr*)&server_address, sizeof(server_address));
char buffer[100];
recvfrom(sock, buffer, len, 0, NULL, NULL);
buffer[len] = '\0';
printf("send: '%s' \n", data_to_send);
close(sock);
return 0;
}
4.a.Output:
4.b.Procedure:
Server (server.c):
1. Set up `server_addr` with IP `INADDR_ANY`, port `8877`, and create a TCP socket with
`socket(PF_INET, SOCK_STREAM, 0)`.
2. Bind and set to listen using `bind` and `listen`.
3. Accept clients in a loop with `accept`, displaying client IPs.
4. For each client, receive data using `recv`, echo it back with `send`, and close the client
socket.
3
2022503524
5. Close the server socket when done.
Client (client.c):
1. Define `server_address` with IP `localhost` and port `8877`.
2. Create a TCP socket and connect to the server with `connect`.
3. Send a message with `send`, receive the server’s echo with `recv`, and print it.
4. Close the socket to end the session.
4.b.Server.c
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int SERVER_PORT = 8877;
struct sockaddr_in server_addr;
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(SERVER_PORT);
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
int listen_sock;
if((listen_sock = socket(PF_INET,SOCK_STREAM,0)) < 0)
{
printf("counld not create listen socket\n");
return 1;
}
if((bind(listen_sock,(struct sockaddr*)&server_addr,sizeof(server_addr))) < 0)
{
printf("could not bing socket\n");
return 1;
}
int wait_size = 16;
if(listen(listen_sock,wait_size)<0)
{
printf("could not open socket for listening\n");
return 1;
4
2022503524
}
struct sockaddr_in client_address;
int client_address_len = 0;
while (true) {
int sock;
if ((sock = accept(listen_sock, (struct sockaddr
*)&client_address,&client_address_len)) < 0)
{
printf("could not open a socket to accept data\n");
return 1;
}
"server.c" 58L, int n = 0;
int len = 0, maxlen = 100;
char buffer[maxlen];
char *pbuffer = buffer;
printf("client connected with ip address: %s\
n",inet_ntoa(client_address.sin_addr));
while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
pbuffer += n;
maxlen -= n;
len += n;
printf("received: '%s'\n", buffer);
send(sock, buffer, len, 0);
}
close(sock);
}
close(listen_sock);
return 0;
}
4.b.Output:
4.b.Client.c
#include <arpa/inet.h>
5
2022503524
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
const char* server_name = "localhost";
const int server_port = 8877;
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
inet_pton(AF_INET, server_name, &server_address.sin_addr);
server_address.sin_port = htons(server_port);
int sock;
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
6
2022503524
}
4.b.Output
Result:
The echoTCP and echoUDP communication have been implemented and the message
have been sent successfully.
Ex.No.: 05
Date: 05.09.2024
Implementation of LAN and VAN
Aim:
To implement LAN and VPN using Cisco Packet Tracer.
1.LAN
Algorithm:
1. Open Cisco Packet Tracer and add the PCs, switch, and server to the workspace as
shown in the diagram.
2. Connect each PC and the server to the switch using Copper Straight-Through cables,
attaching one end to the Ethernet port of each device and the other end to an available port
on the switch.
3. For each device, go to the Desktop tab, select IP Configuration, and assign a unique IP
address within the same subnet:
- Example IPs:
7
2022503524
- My PC: 192.168.1.2
- Work PC: 192.168.1.3
- Game PC: 192.168.1.4
- My Server: 192.168.1.10
- Set the subnet mask to the same value for all devices, e.g., 255.255.255.0.
4. Open the Command Prompt on each PC and use the `ping` command to verify
connectivity with the server and other PCs. For example, type `ping 192.168.1.10` to test
the connection to the server.
5. Save the project in Cisco Packet Tracer to retain the configuration.
6. This setup enables all devices on the LAN to communicate through the switch.
Structure:
Output:
8
2022503524
2.VPN
Algorithm:
1. Configure IP addresses for all routers' interfaces and assign IP addresses and default
gateways for PC0 and PC1 to match their connected routers.
2. Set up a routing protocol like RIP or OSPF on all routers to ensure they can reach each
other’s networks:
3. On Router0 and Router3, create an Access Control List (ACL) to define the traffic that
should be encrypted
Router(config)# access-list 100 permit ip [source network] [source wildcard]
[destination network] [destination wildcard]
4. Define the Internet Key Exchange (IKE) Phase 1 policy on Router0 and Router3:
5. Set the pre-shared key for the VPN connection on Router0 and Router3:
Router(config)# crypto isakmp key [key] address [peer IP address]
6. Configure IPsec Phase 2 (Transform Set) on Router0 and Router3:
Router(config)# crypto ipsec transform-set MYSET esp-aes esp-sha-hmac
7. Create a Crypto Map on Router0 and Router3 to link the VPN settings with the ACL and
apply it to the outgoing interface:
Router(config)# crypto map MYMAP 10 ipsec-isakmp
Router(config-crypto-map)# set peer [peer IP address]
Router(config-crypto-map)# set transform-set MYSET
Router(config-crypto-map)# match address 100
Router(config-crypto-map)# exit
Router(config)# interface [interface name]
Router(config-if)# crypto map MYMAP
8. Test the VPN connection by sending pings from PC0 to PC1. If configured correctly,
traffic should be encrypted and transmitted securely over the VPN.
Structure:
9
2022503524
Output:
Result:
LAN and VPN is implemented using Cisco Packet Tracer.
10
2022503524
Ex.No.:06
Date:12.09.2024
HTTP web server
Aim:
To implement the HTTP web server connection.
Procedure:
1. Create an `HttpServer` on port `8000` and set the context for the root URL (`"/"`) with
`MyHandler` to handle requests.
2. Start the server with `server.start()`.
Handler (MyHandler):
1. Define a response message in the `handle` method.
2. Send the response with `sendResponseHeaders` and write it to the output stream.
3. Close the output stream to finish the response.
Program:
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class SimpleHttpServer
11
2022503524
{
public static void main(String[] args) throws IOException
{
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null);
server.start();
System.out.println("Server is running on port 8000");
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException
{
String response = "Hello, this is a simple HTTP server response!";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Output:
Result:
The implementation of HTTP web server connection is done and the message is displayed
successfully.
12
2022503524