[ Pobierz całość w formacie PDF ]
.OnClose This function is called on a socket to signal that the application on the other end of theconnection has closed its socket or that the connection has been lost.This should be fol-lowed by closing the socket that received this notification.OnConnect This function is called on a socket to signal that the connection with another applicationhas been completed and that the application can now send and receive messages throughthe socket.OnReceive This function is called to signal that data has been received through the socket connec-tion and that the data is ready to be retrieved by calling the Receive function.OnSend This function is called to signal that the socket is ready and available for sending data.This function is called right after the connection has been completed.Usually, the othertime that this function is called is when your application has passed the Send functionmore data than can be sent in a single packet.In this case, this is a signal that all of thedata has been sent, and the application can send the next buffer-full of data.Detecting ErrorsWhenever any of the CAsyncSocket member functions return an error, either FALSE formost functions or SOCKET_ERROR on the Send and Receive functions, you can call theGetLastError method to get the error code.This function returns only error codes, andyou have to look up the translation yourself.All the Winsock error codes are definedwith constants, so you can use the constants in your code to determine the error messageto display for the user, if any.You can use the GetLastError function as follows:int iErrCode;iErrCode = m_sMySocket.GetLastError();switch (iErrCode){case WASNOTINITIALISED:.} 026 31240-9 CH20 4/27/00 10:34 AM Page 505Internet Applications and Network Communications 505Building a Networked ApplicationFor the sample application that you will build today, you ll create a simple dialog appli-cation that can function as either the client or server in a Winsock connection.This willallow you to run two copies of the sample application, one for each end of the connec-tion, on the same computer or to copy the application to another computer so that youcan run the two copies on separate computers and see how you can pass messages acrossa network.Once the application has established a connection with another application,you will be able to enter text messages and send them to the other application.When themessage has been sent, it will be added to a list of messages sent.Each message that isreceived will be copied into another list of all messages received.This will allow you tosee the complete list of what is sent and received.It will also allow you to compare whatone copy of the application has sent and what the other has received.(The two listsshould be the same.)Creating the Application ShellFor today s sample application, just to keep things simple, you ll create a dialog-styleapplication.Everything that you are doing in today s application can be done in an SDIor MDI application just as easily as with a dialog-style application.By using a dialog-style application today, we are getting everything that might distract from the basicsocket functionality (such as questions about whether the socket variable belongs in thedocument or view class, how much of the application functionality belongs in which ofthese two classes, and so on) away from the sample application.To start today s sample application, create a new MFC AppWizard project, giving theproject a suitable name, such as Sock.On the first step of the AppWizard, specify thatthe application will be a dialog-based application.On the second step of the AppWizard,specify that the application should include support for Windows Sockets, as in Figure20.3.You can accept the default settings for the rest of the options in the AppWizard.20Window Layout and Startup FunctionalityOnce you create your application shell, you can lay out the main dialog for your applica-tion.On this dialog, you ll need a set of radio buttons to specify whether the applicationis running as the client or server.You ll also need a couple of edit boxes for thecomputer name and port that the server will be listening on.Next, you ll need a com-mand button to start the application listening on the socket or opening the connection tothe server, and a button to close the connection.You ll also need an edit box for enteringthe message to be sent to the other application and a button to send the message.Finally,you ll need a couple of list boxes into which you can add each of the messages sent and 026 31240-9 CH20 4/27/00 10:34 AM Page 506506 Day 20received.Place all these controls on the dialog, as shown in Figure 20.4, setting all of thecontrol properties as specified in Table 20.2.FIGURE 20.3.Including socketssupport.FIGURE 20.4.The main dialoglayout.TABLE 20.2.CONTROL PROPERTY SETTINGS [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •