(a) The C code below shows an abridged extract from a TCP/IP server implemented usin the standards sockets library: int serversocket; struct sockaddr_in server; serverSocket - socket (PF_INET, SOCK_STREAM, IPPROTO_TCP) ; if(serverSocket < 0) I/ Handle error... memset (kserver, 0, sizeof (server)); server.sin_family = AF_INET; server.sin_port= htons (4444); server.sin_addr.s_addr = INADDR_ANY; if(bind(serverSocket, (struct sockaddr *)&server, sizeof(server)) < 0) // Handle error... What is the purpose of the htons () function used in the above code?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
2 This question is about Networks
(a) The C code below shows an abridged extract from a TCP/IP server implemented using
the standards sockets library:
int serversocket;
struct sockaddr_in server;
serverSocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP):
if(serversocket < 0)
// Handle error...
memset (&server, 0, sizeof (server) );
server.sin_family = AF_INET;
server.sin_port = htons (4444);
server.sin_addr.s_addr = INADDR_ANY;
if (bind (serverSocket, (struct sockaddr *) &server, sizeof (server)) < 0)
// Handle error...
What is the purpose of the htons () function used in the above code?
Transcribed Image Text:2 This question is about Networks (a) The C code below shows an abridged extract from a TCP/IP server implemented using the standards sockets library: int serversocket; struct sockaddr_in server; serverSocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP): if(serversocket < 0) // Handle error... memset (&server, 0, sizeof (server) ); server.sin_family = AF_INET; server.sin_port = htons (4444); server.sin_addr.s_addr = INADDR_ANY; if (bind (serverSocket, (struct sockaddr *) &server, sizeof (server)) < 0) // Handle error... What is the purpose of the htons () function used in the above code?
(b) A further extract from the same server is shown below, the function
ServerConnection ( ) contains the code to implement the server's protocol and is not
shown.
while (1)
{
struct sockaddr_in client;
int alen, clientConnection;
alen = sizeof (client);
clientConnection = accept (serverSocket, (struct sockaddr*)&client, &alen);
ServerConnection (clientConnection);
close (clientConnection);
(1) Explain the purpose, in terms of handling network connections, of the while loop
in the code shown.
(ii) How would the above code handle the situation where two computers connect to
the server simultaneously? How would the two connections be handled?
(c) When a client connects to the server, the server should respond by sending a line of
text to the client, terminated by a carriage return and a line feed. The line of text to be
sent is shown below:
+OK POP3 Mail server ready.
Write the C code that would be placed in the function ServerConnection () called
from the code above to send this line over the network to the client. You can assume
the handle for the open socket has been passed into the function as an integer vari-
Acti
able called sd.
Transcribed Image Text:(b) A further extract from the same server is shown below, the function ServerConnection ( ) contains the code to implement the server's protocol and is not shown. while (1) { struct sockaddr_in client; int alen, clientConnection; alen = sizeof (client); clientConnection = accept (serverSocket, (struct sockaddr*)&client, &alen); ServerConnection (clientConnection); close (clientConnection); (1) Explain the purpose, in terms of handling network connections, of the while loop in the code shown. (ii) How would the above code handle the situation where two computers connect to the server simultaneously? How would the two connections be handled? (c) When a client connects to the server, the server should respond by sending a line of text to the client, terminated by a carriage return and a line feed. The line of text to be sent is shown below: +OK POP3 Mail server ready. Write the C code that would be placed in the function ServerConnection () called from the code above to send this line over the network to the client. You can assume the handle for the open socket has been passed into the function as an integer vari- Acti able called sd.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY