Pop3The pop3 client-side program is intend to fulfil the following three main tasks:1.Resolve the pop3 server domain name and convert it into IP address to establish a TCP connection between the client and the server.2.Guide the user log in the E-mail .3.Provide common functions of E-mail for the user, including get the number and list of the E-mails, display the content of mails,delete the mails, search text in mails and display the mails by subject.Specifically speaking, the log-in process 's requirements:After establishing the connection with the server successfully, require user name and password from the user and check whether they are valid.The five common functions ' requirements are:1.Get the number and list of the E-mailsWhen user enters the corresponding command, the program can display the number or the list of the E-mails orderly on screen.2.Display the content of mailsAllow users to choose the mail they want to check by providing two choices --- download and print it on screen.3.Delete the mailsAllow users to choose the mail they want to delete and delete it from the mailbox. 4.search text in mailsWhen user enters the texts they want to search, the program will display the file names of the files contain it.5.Display the mails by subjectDisplay the E-mail by subject, meaning files with the same subjects will be grouped together.Therefore, I decompose the whole program into three parts by writing two other functions apart from main() -- void logOn() and void logged(). The first task is implemented in main() by calling the function int getIP(); the second task is implemented in the function void logged(); the last one is implemented in the function void logOn(). Since there are too many functions need to be achievedin the logOn() , five other functions are called in it, they are: int printMail(); int chkEnd(int); int makeChoice(char*); void srch(); void sortSub().Thus, the whole program is decomposed into 10 modules (10 functions ) as shown in the below chart:int main(): the main function is where a program starts execution; it calls three functions in it: int getIP(), void logOn() and void logged().int getIP(char []): it is used to convert the received pop3 server domain name to IP address. When called in main() function, an argument in char [] type will be passed to it and then converted to IP address in dotted decimal notation.void logOn() : it is called in function main() and used for guiding the user log on to E-mail. The function int mygetch() is called in it to get each entered character implicitly.void logged() : it is called in function main() to provide 7 common functions of E-mails to the user.int mygetch(): it is called in function void logOn() to make the entered character from the user invisible.int printMail(): it is called in function void logged() to print and download the E-mail. Function int chkEnd(int ) is called by it to check the end of the message.int chkEnd(int ): it is used to check wether the received message from the server has reached to the end. When called in the function int printMail(), the length of the received in int type is passed to it. It will then return 0 or 1 to indicate whether it is the end of the mail.int makeChoice(char *a): it is used to check which command does the user input. When called in the function logged(), the user ' s command in char * type is passed to it. Then, it return the corresponding integer in int type.void srch(): it is called by the function logged() to accomplish the " search by text " task.void sortSub(): it is called by the function logged() to accomplish the " display by subject " task.Overall flowchartDesign of data structureIn the program, there are eleven global varibles:FILE_MODE 0644---- Define the mode of opening a file.PORT 110---- Port number for the TCP connection.char *IP---- hold the server ' s IP address.msg[250]---- hold the receivedcomd[2][200]={"USER ","PASS "},command[5][200]={"STAT\n","LIST\n","RETR ","DELE ""QUIT\n"}, mail[250];int recvMsgSize,sock,qt=0,dl=0;int main():First, I use a while loop, in which the IP resolve and log on tasks are implemented. A global varible ip in int type is used to control the loop. It is set to 0 at the beginning. When the function int getIP() successfully convert the received pop3 server domain name into the IP address, it will return 1 to the varible ip , the ip will be set to 1 so that the while loop can be ended. Then, if the expression strstr(msg,"+OK")!=NULL is true, the function void logOn() and void logged() will be called consequently to finish the other two tasks, if it is false, the socket will be closed and the program will be ended.int printMail();It is called in function void logged() to print and download the E-mail. As the flow chart shows, the program first provide two choices for the user ---- 1 and 2. If the userenter 1, a file name is required from the user in order to create a file holding the mail. If the choice is 2, the mail will be printed out on the screen. To receive the mail properly, a while loop is used which is controlled by the variable i in int type. It is set to zero at the beginning. Then 250 characters are receivedin the global variable char mail[250] each time , if the length of mail is less than 250, the function int chkEnd() is called to check wether it reaches the end of the mail. If it is the end, the function will return 1 , which is given to i to end the while loop.int makeChoice(char *a);It is used to check which command does the user input. When called in the function logged(), the user ' s command in char * type is passed to it. Then, it return the corresponding integer in int type. As the flowchart shows, when the user ' s command is "stat", "ls", "find" or "sort", the variable int choice will be set to 0, 1, 5 and 6 orderly. When the command is "get" or "dele", the program will require the user toenter the number of the mail they want to get or delete, then set choice to2 and 3 correspondingly. If the entered command is "quit", the global variable qt is set to 1 and the choice is set to 4. If the command is not match to all the command mentioned above, the choice is set to 7. At last, return choice.void logOn()It is called in function main() and used for guiding the user log on to the E-mail. The function int mygetch() is called in it to get each character implicitly.As the flowchart shows, a while loop is used to allow the user enter the username and password repeatedly. It first require a username from the user. Then generate and send the pop3 command out. If the strstr(msg,"+OK")!=NULL, then require the user enter the password. To make the password entered implicitly, the function int mygetch() is called in a for loop. If the received character is '\n', the for loop is ended if not print '*'. Then, send the password in pop3 style, if the strstr(msg,"+OK")!=NULL, print out thereceived message and step out the while loop.void logged() :It is called in function main() to provide 7 common functions of the E-mail to the user.A while loop is used in it to allow the user repeating choose the command. The global variable qt in int type is used to control the while loop. It is set to 0 at the beginning. In the loop, the program first provide some informations about how to choose different commands to realize different functions. Then, it calls the function makeChoice() by passing the string got from user ' s to it, the return value of the function is given to a int type variable n. According to the value of n, corresponding pop3 command is sent out and corresponding functions are called. The while loop will continue to run unless the entered command from the user is "quit". Because when the string passed in the function int makeChoice() is "quit", the global variable qt will be set to 1 so that the while loop can be ended.sortSub()it is called by the function logged() to accomplish the " display by subject " task. This function is written on the condition that the file name are all numbers so that a for loop can be adopted to open each of them to sort their subjects. An array of strings -- char s[7][100] is defined to hold the subject of each file in order to sort the subjects in the second for loop. A variable fs in FILE * type is defined to set up a file steam. In the first for loop, I didin' t set a condition to control the loop so that it can continue running until a failure of openning the file accurs. Each time in the loop, I use the function snprintf to convert the variable n in int type to a string stored in variable j and open the file named "m". If the fs is NULL, break to step out of the loop to do the second for loop. If not, enter a while loop, in which I use fgets to read a line in the file each time. If the first character in the line is 'S', get the remaining part after the string ''Subject" by using the strtok function and copy it to s[i]. Then close the file steam and break the while loop. In the second for loop, I use bubble sort method to sort the subjects and print them out. To compare the subjects, the function strcmp is used.void srch()This function is written on the condition that the file name are all numbers. First, the program prompt a message to ask the user enter the text they want search and save the text into the variable content in char[] type. Then, a for loop is adopted to open each file to find the text. In the loop, I first use function snprintf(j, 2, "%ld", i) to convert the variable i in unsigned long type to j in char[] type. Then open the file named j, if the file can not open, print "Done!" and break the loop, if not, read the file into the variable buf in char[]. Use the function strstr to find the text in the buf. If the expression -- strstr(buf,content)==NULL is true, print "j is none" where j is the file name, if it is false,just print out the file name j.int chkEnd(int)It is used to check wether the received message from the server has reached to the end. When called in the function int printMail(), the length of the received in int type is passed to it. A switch is used to include five cases for the ending. Because the message from the server is received in 250 characters each time, so the standard ending --"\r\n.\r\n"may be separated, which causes five cases as shown in the flow chart below. If it is not the five cases, the function will return 0, which can not end the loop it is involved in. If it is one of the five cases, the function will return 1, which indicate the end of the mail and end the loop it is involved in.int getIP(char []):It is used to convert the received pop3 server domain name to IP address. When called in main() function, an argument in char [] type will be passed to it and then converted to IP address in dotted decimal notation.It defines two structures-- struct sockaddr_in addr and struct hostent *host. First it converts the passed argument -- the server ' s domain name, to IP address in host byte order using the function gethostbyname and save the IP address to the variable host. Then if the host is NULL, print "The server is not found." and return 0, if not, convertthe IP address in host byte order to network byte order using function inet_ntoa and return 1.Self evaluation:In this project, I took the responsibility for writing pop3 client-side program.To write this program efficiently and orderly, I first carefully reviewed the communication of E-mail through pop3 and socket programming. Then, I analysed all the functions need to implement and decided to divide the whole program into several functions. In the process of writing this program, I faced many problems, including failfure of domain name-to-IP conversion, disorder of the received packets and annoying segmentation fault. By searching on the Internet, learning from other students and asking teachers, I solved these problems one by one.But there are still some problems waiting for better solutions:1.The pop3 client will lose connections to the server if no command is sent out to the server in a certain time.2.It can only implement the "search text" and "display by subject" tasks on the condition that the files 's names are all numbers.To pursue a more consummated pop3 client program, there are also some improvement to make, such as download a batch of mails to the local machine at one time.Anyway,through accomplishing this program, I had a better understanding of the communication for E-mail, especially the communication defined by pop3 protocol. Besides, I also picked up some programming knowledge learned earlier.Results:1.Log on process:As the figure shows, if the user name or password is invalid it will ask the user re-enter it. Then the user will see the main menu.Using wireshark to capture the packets, we can clearly see the process of the authentication and log on.2."stat" and "ls" functionsWiresshark packet capture results:3."get" function4."delete" functionAs the figure shows, after choose the command "dele", the number of mails in the mailbox changes from 5 to 4.5."find" function6."sort" functionAfter "sort", the mails will be displayed by subjects with the file name.7."quit" function。