Remember this memorable one months

Source: Internet
Author: User

When I first wrote this program, do not know how to be mixed with a little excitement, all day thinking is how to design the server, how to design the database, the database in which fields, how to design the client, how to let the server save the user's friend information and so on, too much, and constantly think of methods, And constantly overthrow themselves, although I did not understand anything, but also did not go online, I think I like the design of the feeling (of course, later proved to know too little, or from the Internet to know how the database design). The first thing I have to tangle with the client's message is not through the server, asked a lot of people, each have their own argument, finally Huang to me that horse seniors do a program, went to ask him, he told me to log in and server communication, and then chat with friends directly with the UDP mode to send information to friends, Do not go through the server (although this said, but I still do not understand, we sometimes can not send sensitive words on QQ, if not through the server, then how he detected it. )

Before writing this program, network programming aspects of the program, I wrote the book on those several, the most basic "you point send, I point receive" program, even the C/S mode has not been written, let alone the program's asynchronous socket. It was very naïve at first, think that the book on the kind of communication mode written in C/s mode can be, the reality is cruel, I encountered the obstruction function again, when the time in reading the seniors told me accept and receive will be blocked, to tell the truth at that time did not have a profound understanding, Later, when I saw the line Chengna, I wanted to create a thread to receive the message from the user. At that time just wanted to think, this time to put into action, just at hand, is the only one to bring back a VC book-Sun Xin "VC in-depth detailed", which I borrowed from the seniors there books, These one months thanks to this book, a lot of things are borrowed from the above, including the following asynchronous sockets; In the face of blocking functions, I want to solve this problem with multithreading, in the process, encountered a lot of problems, such as:

Because the foundation is not good, so at that time after seeing this, think is whether the function of their own definition or declaration of the problem, and constantly looking for, can not find that error, and later because in the definition of member functions,

Must be entered manually and cannot be copied!!!

In debugging the first communication code, found that the client side of the line is not blocked, and then the Internet asked to know because there is no binding socket (at that time even this error is to be made, some conceptual things are really not mastered)

Also encountered in the middle of the socket to assign value problem, because the CSocket class does not overload =, so can not be socket1=socket2 this simple to Socket2 to Socket1, the Internet can be used detach and attach to achieve the transmission of sockets, But after detach, the original socket was closed, not what I wanted.

One day on the Internet an article, said:

"Once saw someone create their own thread, create a CSocket object in the thread to listen, Accept, if Accept success then another thread to continue listen, Accept ... It can be said that he completely does not understand csocket, in fact, CSocket has built-in multithreaded mechanism, you only need to derive from the CSocket, and then overload onaccept: "

I've always been the one who doesn't understand CSocket, I've always been Accept success is another thread to continue listen, Accept ... Because I didn't even know that the listening socket was just for listening (because of the problem, I was yelled at on the CSDN) ... Two days later, I found a lot of details after the thread was created, for example, the entry function must be a global function or static function, and static function has a lot to pay attention to the details, to tell the truth, their foundation is too bad, in the network programming this piece of children have not mastered the premise, and then involved in threading these things, really a bit difficult. Later read Sun Xin's "VC + in-depth detailed", thread synchronization This chapter later, found a message based on the asynchronous socket Network Chat room program, and constantly study that program, at that time did not use the socket class, because the previous book network programming this piece is used by the CSocket class, So when you see the load socket, and the type of socketadd_in will be particularly confused, but later to see a few times is used to. In this program, first to use the WSAAsyncSelect () function for the specified socket register a network event, this place made a last found problem, that is, in the server side I actually for listening to the socket Register network events, not only registered Fd_connect, but also registered FD _read this incident, really because this knowledge is too little, I thought that as long as the registration of network events, no matter which socket, as long as there is news, will trigger the event, and later caused a number of problems, these later. According to the gourd on the book, I also drew a preliminary scoop, a simple message based asynchronous socket. The first stage is over.

The second stage is the operation of the database. Especially unfortunately, Sun Xin's "VC + + in-depth detailed", about the operation of the database basically did not say, and found that the "21 days to learn through VC + +", the main talk about the database with ODBC operation, but spoke very shallow, ran to our side of the bookstore turned, incredibly no database programming related books, There is no way only to try with ODBC, and then there is another problem that bothers me, is the database design, mainly friends this piece, I had not thought of using two tables, think is in a table memory user's basic information, as well as his friends, later found that more than one friend, it is not to build a lot of fields (friend 1, Friend 2, Friend 3 ... , of course, this is not practical, but also think that the database field is similar to the type of the array, and finally there is no way, asked the seniors, seniors said that built two tables, a user information, a friend relationship. Although this said, but still have confusion, the user is more, the friend that table data is not very big. And the server has to traverse once more laborious ah. At that time two tables of the key words are set QQ, and then found a problem, it's my buddy. The QQ field of the list may have duplicate records, but the keyword is not allowed to repeat, and at this point I tried to traverse a table (there are only two data), strange, the result is 4, This is the two data of different combinations, the Internet check, found that the Cartesian product, the two tables connected, will follow the rules of Cartesian product. One idea at the time was how to operate only in a single table, on the Internet, a good friend answered a lot, but the ODBC book is too little, they say that I really do not see, no way, around can read books have read, only left the "VC in-depth detailed" In that little bit about ADO operation of the database, and began to think, on such a few pages, see certainly useless, or go back to school after the book to borrow a special database to see it. But the idea of holding a dead horse as a living horse, or to see a point, who knows that night so a look, fully understand why ADO now so prevalent, indeed, than the ODBC is too convenient, after reading immediately from the bed down to open the computer began to write the book on the program, when you see ADO through the Rst1-> Open (   "SELECT *FROM QQ account Information Sheet", _variant_t  (idispatch*) con,true), adOpenStatic, adlockoptimistic,-1 ); When you get a pointer to a specified table, there's a feeling of desperation.

The third stage is the design of their own agreement (do not know can not be considered an agreement). At that time, the problem is that users want to send a lot of information to the server, such as login and password messages, such as the message when registering, or add friends information. In fact, this problem has been bothering me from the very beginning, until I tried to send the structure, because the send and receive function buf are char * type, so I did not know how to send the structure, the network said the cast on the line, but many details still do not understand, Finally, a netizen on the csdn taught me, with the experience of sending the structure, plus see the online said can customize the header to distinguish these messages, Baotou is a marker. To tell you the truth, I have no idea about Baotou, but my first thought was to use a struct to define a member flag to mark the messages. For example, the message sent by the client flag 10, is the login information, 20 is the registration information, 30 is to add the friend information, 40 is the request to refresh the friend's information. I suddenly felt a little understood what the agreement meant. Body in Bupt, but did not learn the course of communication, all day listening to other students say what the agreement, I remember I once asked a letter to the classmate, the agreement is what, he gave me the answer is a set of rules. I was thinking, literally, but I had no idea what the actual deal was, and this time I didn't think I would have defined a "deal". But I still feel wrong, this procedure is simple, so on several types of news, if later function more, still can this agreement. I think there might be a better way. Some of the big problems that have been encountered in this period have been said, there is also a user how to obtain their own friends real-time message, think for a long time, finally or using a timer to achieve, every once in a while (the program is 1 minutes, convenient debugging point) to send a request to the server, request to refresh their friends, The server goes to the database to look up the information of the person who is online in his friend and sends it to the user.

There are several issues with the program that are perfect, the first is how the server to determine whether the user is online, and then the client is in the friend interface, how to remind users to a message, because I chat socket is created in the Chat window, so if the chat window is not open, the socket will not be created, Also will not receive the news that the friend sends;

Finally found a tray program on the Internet, to achieve the program can be minimized to the lower right corner of the tray area;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.