Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to write a UDP connectionless client-server pair in the language of your

ID: 3810298 • Letter: Y

Question

You are to write a UDP connectionless client-server pair in the language of your choice. The client will send a UDP message to the server, e.g. "Hello". The server will start and run on port number 5432 and return the original message concatenated with a humorous message as an ASCII string to the client on port 5433. The client will then present a formatted string to the monitor. As an example, the basic output should look like Your client program sends "Hello" Your client program w receive the string "Hello Don't eat yellow snow" from the Server.

Explanation / Answer

#ifndef SRI_UDP__H
#define SRI_UDP__H

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdexcept>

namespace sri_client-service
{

class sri_client-service_runtime_error : public std::runtime_error
{
public:
sri_client-service_runtime_error(const char *w) : std::runtime_error(w) {}
};


class sri_client
{
public:
sri_client(const std::string& addr, int port);
~sri_client();

int get_sri_socket() const;
int get_sri_port() const;
std::string get_sri_addr() const;

int send(const char *msg, size_t size);

private:
int f_sri_socket;
int f_port;
std::string f_addr;
struct addrinfo * f_addrinfo;
};


class sri_service
{
public:
sri_service(const std::string& addr, int port);
~sri_service();

int get_sri_socket() const;
int get_sri_port() const;
std::string get_sri_addr() const;

int recv(char *msg, size_t max_size);
int timed_recv(char *msg, size_t max_size, int max_wait_ms);

private:
int f_sri_socket;
int f_port;
std::string f_addr;
struct addrinfo * f_addrinfo;
};

} // namespace sri_client-service
#endif

#include "sri_client-service.h"
#include <string.h>
#include <unistd.h>

namespace sri_client-service
{

sri_client::sri_client(const std::string& addr, int port)
: f_port(port)
, f_addr(addr)
{
char decimal_port[16];
snprintf(decimal_port, sizeof(decimal_port), "%d", f_port);
decimal_port[sizeof(decimal_port) / sizeof(decimal_port[0]) - 1] = '';
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
int r(getaddrinfo(addr.c_str(), decimal_port, &hints, &f_addrinfo));
if(r != 0 || f_addrinfo == NULL)
{
throw sri_client-service_runtime_error(("invalid address or port: "" + addr + ":" + decimal_port + """).c_str());
}
f_sri_socket = socket(f_addrinfo->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
if(f_sri_socket == -1)
{
freeaddrinfo(f_addrinfo);
throw sri_client-service_runtime_error(("could not create socket for: "" + addr + ":" + decimal_port + """).c_str());
}
}


sri_client::~sri_client()
{
freeaddrinfo(f_addrinfo);
close(f_sri_socket);
}


int sri_client::get_sri_socket() const
{
return f_sri_socket;
}


int sri_client::get_sri_port() const
{
return f_port;
}


std::string sri_client::get_sri_addr() const
{
return f_addr;
}


int sri_client::send(const char *msg, size_t size)
{
return sendto(f_sri_socket, msg, size, 0, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen);
}


sri_service::sri_service(const std::string& addr, int port)
: f_port(port)
, f_addr(addr)
{
char decimal_port[16];
snprintf(decimal_port, sizeof(decimal_port), "%d", f_port);
decimal_port[sizeof(decimal_port) / sizeof(decimal_port[0]) - 1] = '';
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
int r(getaddrinfo(addr.c_str(), decimal_port, &hints, &f_addrinfo));
if(r != 0 || f_addrinfo == NULL)
{
throw sri_client-service_runtime_error(("invalid address or port for UDP socket: "" + addr + ":" + decimal_port + """).c_str());
}
f_sri_socket = socket(f_addrinfo->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
if(f_sri_socket == -1)
{
freeaddrinfo(f_addrinfo);
throw sri_client-service_runtime_error(("could not create UDP socket for: "" + addr + ":" + decimal_port + """).c_str());
}
r = bind(f_sri_socket, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen);
if(r != 0)
{
freeaddrinfo(f_addrinfo);
close(f_sri_socket);
throw sri_client-service_runtime_error(("could not bind UDP socket with: "" + addr + ":" + decimal_port + """).c_str());
}
}


sri_service::~sri_service()
{
freeaddrinfo(f_addrinfo);
close(f_sri_socket);
}


int sri_service::get_sri_socket() const
{
return f_sri_socket;
}


int sri_service::get_sri_port() const
{
return f_port;
}


std::string sri_service::get_sri_addr() const
{
return f_addr;
}


int sri_service::recv(char *msg, size_t max_size)
{
return ::recv(f_sri_socket, msg, max_size, 0);
}


int sri_service::timed_recv(char *msg, size_t max_size, int max_wait_ms)
{
fd_set s;
FD_ZERO(&s);
FD_SET(f_sri_socket, &s);
struct timeval timeout;
timeout.tv_sec = max_wait_ms / 1000;
timeout.tv_usec = (max_wait_ms % 1000) * 1000;
int retval = select(f_sri_socket + 1, &s, &s, &s, &timeout);
if(retval == -1)
{
// select() set errno accordingly
return -1;
}
if(retval > 0)
{
// our socket has data
return ::recv(f_sri_socket, msg, max_size, 0);
}

// our socket has no data
errno = EAGAIN;
return -1;
}

} // namespace sri_client-service

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote