Question
Modify TCPEchoServer4.c as follows: Make the server display its name or address and its listening socket identifier Make the server display the phrases received from
Modify TCPEchoServer4.c as follows:
Make the server display its name or address and its listening socket identifier
Make the server display the phrases received from the client. It should display all phrases from all clients that use the port number that you run your server on. This server will run on coca (xxx.xx.xxx.68)
Continue to run the server on coca, with the single command line argument of the port.
Modify TCPEchoXXXServer.c as follows:
Make the server display its name or address and its listening socket identifier
Make the server display the phrases received from the client. It should display all phrases from all clients that use the port number that you run your server on. This server will run on coca (xxx.xx.xxx.68)
Continue to run the server on coca, with the single command line argument of the port.
* These IPs and server names are a Unix server in our institution, and we can pass a listening port into the C program. We are then supposed to open another telnet session and have the program accept and display messages passed into it from the client. After many hours, I've confused myself and NOT succeeded a dozen times. A working example would help. It calls to a number of other files, whose basic functions are implied in the code and comments.
#include #include #include #include #include #include #include #include "Practical.h"
static const int MAXPENDING = 5; // Maximum outstanding connection requests
int main(int argc, char *argv[]) { extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
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; // Socket descriptor for server 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 getsockname() getsockname()(bind getpeername()(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"); else printf("Listening %s/%d ", inet_ntoa(servAddr.sin_addr), ntohs(servAddr.sin_port));
for (;;) { // Run forever struct sockaddr_in clntAddr; // Client address // Set length of client address structure (in-out parameter) socklen_t clntAddrLen = sizeof(clntAddr);
// Wait for a client to connect int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen); if (clntSock < 0) DieWithSystemMessage("accept() failed");
// clntSock is connected to a client!
p hugely.*
ORIGINAL PROGRAM:
Apologies, did not include
//practical.h
#ifndef PRACTICAL_H_ #define PRACTICAL_H_
#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_
//DieWithMessage
#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); }
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