Question
DAYTIME SERVER CODE #include #include #include #include #include #include #include #include Practical.h #include #include static const int MAXPENDING = 5; //Maximum outstanding connection requests int
DAYTIME SERVER CODE
#include
static const int MAXPENDING = 5; //Maximum outstanding connection requests int main(int argc, char *argv[]) { time_t ticks; char sendbuffer[BUFSIZE]; //Buffer for sending data to the client
if (argc != 2) //Test for correct number of arguments DieWithUserMessage("Parameter(s)", "
in_port_t servPort = atoi(argv[1]); //First arg, local port
//Create socket for incoming connections int servSock; if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithSystemMessage("socket() failed");
//Construct local address structure struct sockaddr_in servAddr; // Local address memset(&servAddr, 0, sizeof(servAddr)); // Zero out structure servAddr.sin_family = AF_INET; // IPv4 address family servAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface servAddr.sin_port = htons(servPort); // Local port
//Bind to the local address if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0) DieWithSystemMessage("bind() failed");
//Mark the socket so it will listen for incoming connections if (listen(servSock, MAXPENDING) < 0) DieWithSystemMessage("listen() failed");
for (;;) { // Run forever //Wait for client to connect int clntSock = accept(servSock, (struct sockaddr *) NULL, NULL); if (clntSock < 0) DieWithSystemMessage("accept() failed");
//clntSock is connected to client! snprintf(sendbuffer, sizeof(sendbuffer), "%.24s ", ctime(&ticks)); //Create data and time string in outgoing buffer ssize_t numBytesSent = send(clntSock, sendbuffer, strlen(sendbuffer), 0); //Send date and time string to the client if (numBytesSent < 0) DieWithSystemMessage("send() failed");
close(clntSock); //Close client socket
} //NOT REACHED }
---------
DAYTIME CLIENT CODE
#include
int main(int argc, char *argv[]) { char recvbuffer[BUFSIZE]; // I/O buffer int numBytes = 0; //numBytes is the return value from recv
if (argc < 3) // Test for correct number of arguments DieWithUserMessage("Parameter(s)", "
char *servIP = argv[1]; // First arg: server IP address (dotted quad) in_port_t servPort = atoi(argv[2]); // Third arg: server port (numeric).
// Create a reliable, stream socket using TCP int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) DieWithSystemMessage("socket() failed");
// Construct the server address structure struct sockaddr_in servAddr; // Server address memset(&servAddr, 0, sizeof(servAddr)); // Zero out structure servAddr.sin_family = AF_INET; // IPv4 address family // Convert address int rtnVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr); if (rtnVal == 0) DieWithUserMessage("inet_pton() failed", "invalid address string"); else if (rtnVal < 0) DieWithSystemMessage("inet_pton() failed"); servAddr.sin_port = htons(servPort); // Server port
// Establish the connection to the echo server if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) DieWithSystemMessage("connect() failed");
while ((numBytes = recv(sock, recvbuffer, BUFSIZE - 1, 0)) > 0) { recvbuffer[numBytes] = '\0'; // Terminate the string! fputs(recvbuffer, stdout); // Print the echo buffer /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */ } if (numBytes < 0) DieWithSystemMessage("recv() failed"); // else if (numBytes == 0) // DieWithUserMessage("recv()", "connection closed prematurely"); fputc(' ', stdout); // Print a final linefeed
close(sock); //close the connected socket exit(0); } -----------
In relation to the Daytime application:
a)Outline the purpose of this application and describe the Semantics associated with the communications between the client and server applications. In your answer identify the correct sequence of socket primitive calls (without arguments) invoked by each application.
b)What addressing information does the TCP entity on the client host require in order to make contact with the server application and from where does it obtain this information?
c)What addressing information does the TCP entity on the server host need to return data to the client application and from where does it obtain this information?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started