辅导TCP留学生、讲解C/C++程序、辅导MAC address、C/C++设计讲解

- 首页 >> C/C++编程

Question 1 (2 marks each and 12 marks total): Please answer True/False in the space provided to each question.

_ In TCP socket programming, a successful return from send( ) means that the data has been delivered to the underlying network.

_ A deadlock possibly occurs if large quantities of data are sent simultaneously in both directions.

      _ We can adjust the default buffer size of a socket through calling setsockopt( ). When the call succeeds, the value passed to setsockopt( ) is guaranteed to be the new size of the socket buffer.

_ The close mechanism in TCP allows data transfers in each direction to be terminated independently.

_ As soon as an application calls the close( ), the underlying TCP implementation will drop any data remaining in the send buffer.

_ In the close mechanism in TCP, the closing handshake message itself is not passed to the receiving application.



Question 2 (48 marks total): Essay

2A (6 marks): It is well known that a network device in the TCP/IP protocol can be identified by a globally unique IP address. Explain why a network device needs also a MAC address, besides a globally unique IP address.



2B (6 marks):

(a)Describe the services that TCP and UDP provide.

(b)Explain the differences in TCP and UDP socket programming due to the differences in TCP- and UDP-based services.


2C (6 marks):

(a)What is a socket?

(b)In socket programming, what goes on in the underlying protocol when socket ( ) is invoked successfully in an application program such as TCPEchoServer.c (in the textbook)?



2D (6 marks): For TCPEchoServer.c (in the textbook), we explicitly specify a local address to the echo server’s socket using bind( ). We say that a socket must have an address for communication, yet we do not call bind( ) in TCPEchoClient.c (in the textbook).

(a)How would an echo client’s socket acquire its local address?

(b)Why do we not call bind( ) in TCPEchoClient.c or what is the advantage of your solution provided in (a)?


2E (6 marks):

(a)Explain why most TCP-based applications need a framing mechanism.

(b)Describe two techniques that are used in the framing mechanism.

(c)Please provide an example to explain the potential risk if we do not adopt the framing mechanism.


2F (6 marks): In socket programming, what might happen when a signal is delivered?


2G (6 marks):

(a)Explain the concepts of iterative and concurrent servers.

(b)State precisely the conditions under which an iterative server is preferable to a multiprocessing server.

(c)State three techniques to implement a concurrent server.



2H (6 marks):

(a)In socket programming, what might happen if a signal is delivered while the program is blocked in a socket call (such as recv( ) or connect( )) and a handler for that signal has been specified?

(b)How to deal with this situation so as to run the program normally.


Question 3 (40 marks): Programming

3A (12 marks): Understand the following program and then answer the questions.



#include <stdio.h> typedef struct {

char a ; int b; short c ;

}MSG;



int main (void) {

MSG *mp, m = {'d', 1, 0};

char *fp, *tp;

mp = (MSG *)malloc(sizeof(MSG));

for (fp = (char *)&m.b, tp = (char *)&(mp->b); tp < (char *)(mp+1);)


*tp++ = *fp++; return 1;

}

Assume that sizeof(char) = 1, sizeof(int) = 4, and sizeof(short) =2.

(1) What does the above program do?



(2) Let tp = (char *)&(mp->b). If tp = 0x0000, tp+1 = .



(3)Let mp = (MSG *)malloc(sizeof(MSG)). If mp = 0x0000, mp+1 = .



(4)To save memory space, you are required to rearrange the structure MSG so that MSG will occupy the minimal memory space. Hereafter, we denote the MSG occupying the minimal memory space by optMSG.

a)Provide an implementation of optMSG and calculate sizeof(optMSG).


b)Explain why such a rearrangement will enable optMSG to occupy the minimal memory space.


c)Plot the memory layout of the MSG and the optMSG.


3B (8 marks): Understand the following program and then answer the questions.



#include<unistd.h> #include<sys/types.h> int main ( )

{

pid_t pid = fork( ); if (pid < 0)

printf("error in fork!"); else if (pid == 0)

printf("the child process id= %d\n", getpid( ));

else

printf("the parent process id= %d\n", getpid( ));

printf("process id = %d\n",getpid( ));


return 1;

}


Assume that the fork( ) is invoked successfully. After forking, the parent process id is 1234 and the child process id is 1235.

(1)How many messages will be printed?


(2)State the output results.


3C (20 marks): We wish to design a program that can operate as both a TCP echo server and a UDP echo server. The program can provide services to many clients at the same time, but involves a single process that does not start up any other threads. Provide a high- level description of this program (a few lines describing how the program works), and then provide as much of the code as possible. Pseudo code is acceptable (we will not be


compiling your code!), but make sure you mention the name of any system calls you are using.


站长地图