0% found this document useful (0 votes)
7 views6 pages

Server

The document contains C++ code for a UDP server and client. The server listens for two numbers from the client, compares them, and sends back a response indicating whether they are equal or not. The client sends two numbers to the server and receives the response, displaying it to the user.
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)
7 views6 pages

Server

The document contains C++ code for a UDP server and client. The server listens for two numbers from the client, compares them, and sends back a response indicating whether they are equal or not. The client sends two numbers to the server and receives the response, displaying it to the user.
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/ 6

UDP

Server
#include <iostream>

#include <cstring> // For strlen

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <unistd.h>

#include <arpa/inet.h>

using namespace std;

int main() {

// Create a socket

int server_socket = socket(AF_INET, SOCK_DGRAM, 0);

if (server_socket == -1) {

cout << "Failed to create socket" << endl;

return -1;

// Define server address structure

struct sockaddr_in my_addr;

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(4999);

my_addr.sin_addr.s_addr = INADDR_ANY; // Listen on any available network interface

// Bind the socket

if (bind(server_socket, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1) {

cout << "Bind failed" << endl;


return -1;

while (true) {

int number1, number2;

struct sockaddr_in client_addr;

socklen_t addr_len = sizeof(client_addr);

if (recvfrom(server_socket, &number1, sizeof(number1), 0, (struct sockaddr*)&client_addr,


&addr_len) == -1) {

cout << "Failed to receive the first number" << endl;

continue;

if (recvfrom(server_socket, &number2, sizeof(number2), 0, (struct sockaddr*)&client_addr,


&addr_len) == -1) {

cout << "Failed to receive the second number" << endl;

continue;

cout << "Received numbers: " << number1 << " and " << number2 << endl;

const char* response;

if (number1 == number2) {
response = "The numbers are equal";

} else {

response = "The numbers are not equal";

if (sendto(server_socket, response, strlen(response), 0, (struct sockaddr*)&client_addr, addr_len) ==


-1) {

cout << "Failed to send the response" << endl;

continue;

close(server_socket);

return 0;

Client

#include <iostream>

#include <cstring> // For strlen

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <unistd.h>

#include <arpa/inet.h>
using namespace std;

int main() {

int network_socket = socket(AF_INET, SOCK_DGRAM, 0);

if (network_socket == -1) {

cout << "Failed to create socket" << endl;

return -1;

struct sockaddr_in server_addr;

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(4999);

server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

int number1 = 0, number2 = 0;

cout << "Enter first number: ";

cin >> number1;

cout << "Enter second number: ";

cin >> number2;

if (sendto(network_socket, &number1, sizeof(number1), 0, (struct sockaddr*)&server_addr,


sizeof(server_addr)) == -1) {

cout << "Failed to send the first number" << endl;

return -1;

if (sendto(network_socket, &number2, sizeof(number2), 0, (struct sockaddr*)&server_addr,


sizeof(server_addr)) == -1) {
cout << "Failed to send the second number" << endl;

return -1;

char msg[100];

memset(msg, 0, sizeof(msg)); // Initialize buffer

socklen_t addr_len = sizeof(server_addr);

if (recvfrom(network_socket, msg, sizeof(msg), 0, (struct sockaddr*)&server_addr, &addr_len) == -1) {

cout << "Failed to receive a message" << endl;

} else {

cout << "Server response: " << msg << endl;

close(network_socket);

return 0;

You might also like