Question: having trouble with a UDP c program where I need to make a knock knock joke where the client initiates the conversation with the conversation

having trouble with a UDP c program where I need to make a knock knock joke where the client initiates the conversation with the conversation being:
Client > Server: Knock! Knock!
Server > Client: Who is there?
Client > Server: Robin.
Server > Client: Robin who?
Client > Server: Robin you! Hand over your cash!
the files i need to complete are udpknockknockjokeclient.c and udpknockknockjokeserver.c. I should also note that the program needs to be manually terminated and not automatically after program is completed.
they need to be utilized with the files:
AddressUtility.c
#include
#include
#include
#include
void PrintSocketAddress(const struct sockaddr *address, FILE *stream){
// Test for address and stream
if (address == NULL || stream == NULL)
return;
void *numericAddress; // Pointer to binary address
// Buffer to contain result (IPv6 sufficient to hold IPv4)
char addrBuffer[INET6_ADDRSTRLEN];
in_port_t port; // Port to print
// Set pointer to address based on address family
switch (address->sa_family){
case AF_INET:
numericAddress = &((struct sockaddr_in *) address)->sin_addr;
port = ntohs(((struct sockaddr_in *) address)->sin_port);
break;
case AF_INET6:
numericAddress = &((struct sockaddr_in6*) address)->sin6_addr;
port = ntohs(((struct sockaddr_in6*) address)->sin6_port);
break;
default:
fputs("[unknown type]", stream); // Unhandled type
return;
}
// Convert binary to printable address
if (inet_ntop(address->sa_family, numericAddress, addrBuffer,
sizeof(addrBuffer))== NULL)
fputs("[invalid address]", stream); // Unable to convert
else {
fprintf(stream,"%s", addrBuffer);
if (port !=0)// Zero not valid in any socket addr
fprintf(stream,"-%u", port);
}
}
bool SockAddrsEqual(const struct sockaddr *addr1, const struct sockaddr *addr2){
if (addr1== NULL || addr2== NULL)
return addr1== addr2;
else if (addr1->sa_family != addr2->sa_family)
return false;
else if (addr1->sa_family == AF_INET){
struct sockaddr_in *ipv4Addr1=(struct sockaddr_in *) addr1;
struct sockaddr_in *ipv4Addr2=(struct sockaddr_in *) addr2;
return ipv4Addr1->sin_addr.s_addr == ipv4Addr2->sin_addr.s_addr
&& ipv4Addr1->sin_port == ipv4Addr2->sin_port;
} else if (addr1->sa_family == AF_INET6){
struct sockaddr_in6*ipv6Addr1=(struct sockaddr_in6*) addr1;
struct sockaddr_in6*ipv6Addr2=(struct sockaddr_in6*) addr2;
return memcmp(&ipv6Addr1->sin6_addr, &ipv6Addr2->sin6_addr,
sizeof(struct in6_addr))==0 && ipv6Addr1->sin6_port
== ipv6Addr2->sin6_port;
} else
return false;
}
DieWithMessage.c
#include
#include
void DieWithUserMessage(const char *msg, const char *detail){
fputs(msg, stderr);
fputs(": ", stderr);
fputs(detail, stderr);
fputc('
', stderr);
exit(1);
}
void DieWithSystemMessage(const char *msg){
perror(msg);
exit(1);
}
Practical.h
#ifndef PRACTICAL_H_
#define PRACTICAL_H_
#include
#include
#include
// Handle error with user msg
void DieWithUserMessage(const char *msg, const char *detail);
// Handle error with sys msg
void DieWithSystemMessage(const char *msg);
// Print socket address
void PrintSocketAddress(const struct sockaddr *address, FILE *stream);
// Test socket address equality
bool SockAddrsEqual(const struct sockaddr *addr1, const struct sockaddr *addr2);
// Create, bind, and listen a new TCP server socket
int SetupTCPServerSocket(const char *service);
// Accept a new TCP connection on a server socket
int AcceptTCPConnection(int servSock);
// Handle new TCP client
void HandleTCPClient(int clntSocket);
// Create and connect a new TCP client socket
int SetupTCPClientSocket(const char *server, const char *service);
enum sizeConstants {
MAXSTRINGLENGTH =128,
BUFSIZE =512,
};
#endif // PRACTICAL_H_

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!