Write the appropriate netcat command to interface with the server (udpserv01.c).
ID: 3880683 • Letter: W
Question
Write the appropriate netcat command to interface with the server (udpserv01.c). You will possibly need to try it out.
udpserv01.c
int main (int argc, char **argv) {
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
}
dg_echo.c
void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) {
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
Sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
Answer should be short.
Explanation / Answer
The appropriate command is as follows:
nc <server_ip> <SERV_PORT>
We have 2 variables here server_ip and SERV_PORT. SERV_PORT is same as that defined in udpserver01.c. On the other hand since server_ip was specified as INADDR_ANY in the code, you can use 2 values for it depending on your situation - if both server and client are no the same pc, use 127.0.0.1, otherwise use the ip address of the server (use ifconfig command to check the ip).
Examples:
Sample pc at port 4000 : nc 127.0.0.1 4000
Different pc at port 4000 : nc 192.168.20.3 4000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.