Write network applications in VC6.0

zhaozj2021-02-08  222

{Write network applications in VC6.0}

"

{Today, today, if you can also write a practical web application, then not only inspiring the interest of the network, not only the pursuit of network knowledge, and the development process itself is also a good learning process. . In VC6.0, MFC has good support for network programming. For different network applications, VCs have different package classes, such as FTP, HTTP, etc., users can quickly develop corresponding procedures. But at the same time, the user has lost some opportunities for the underlying mechanism of the network program to run, and more importantly, there is also a certain extent of flexibility. Here we introduce the general steps developed by Socket socket for readers. Writing a network application under Windows and UNIX, basically uses the Socket socket for data communication, the Socket socket is inherited from the UNIX environment, it can understand one of the network data communication in the program. Agency, its design idea in Windows is not substantially changed compared to UNIX, divided into two design parts of server socket and client sleeve. The design idea is as follows: The first part server side, create server socket CREATE. Second, server sockets are in information binding (BIND) and start listening to connectivity. Third, accept the connection request from the client, and create a reception process. Fourth, start data transmission (Send, Receive). V. Close the socket (CloseSocket). Part II Client End First, Create Create. Second, connect with the remote server (Connect), if you are accepted, create a receiving process. Third, start data transmission (Send, Receive). Fourth, close the socket (CloseSocket). The above design idea is the basic steps we develop, and it is also the basic way to run in most network applications. Here we specify that it is implemented in the VC. Server: 1. Establish a support socket project. When you create an MFC EXE project with App Wizard, when you go to Wizard, in "What Features Would You Like Include?", Select the "Windows Sockets" item. Other steps Each option can be selected according to the actual application. This created project has supported Socket and has been initialized. If you want to add Socket support in an existing project, you only have to do two works: 1  In the stdafx.h file, you can include the header file winsock.h (#include "winsock.h"). 2 Initialization socket, add the following initialization socket code in the member function of the application class: ":: InitInstance ()". IF (! AFXSocketinit ()) {AFXMessageBox (idMessageBox); Return False;} II. Creating a service socket and creates a listening thread.

// Create a service socket Socket SerCon = Socket (PF_INET, SOCK_STREAM, 0); // Determine if it is successfully created if (Server Wrong! ") {AFXMESSAGEBOX (" Server Wrong! "); Return -1;} // Configuration Socket address and other information SOCKADDR_IN SIN; SIN.SIN_FAMILY = AF_INET; / / Specify local address sin.sin_addr.s_addr = htonl (inaddr_any); // Specify server port number Nport, you can customize int nport = 5080; sin.sin_port = HTONS (NPORT); // address information is bound to the socket. IF (Bind (LPSOCKADDR) == SOCKET_ERROR) {AFXMESSAGEBOX ("Bind Wrong!"); Return -1;} // Established a monitor queue (size 3), start listening to IF ( Listen (SERCON, 3) == Socket_ERROR) {AFXMessageBox ("Listen Wrong!"); return -1;}; 1 implement listening thread and creates data receiving threads. // Create a listening thread where the program needs to start listening to the connection, and implement it. // Create a listening thread (in the program start or button event implementation) AFXBEGINTHREAD (WaitConnect, NULL); // Implement listening thread uint waitconnect (lpvoid lpparm) {socket conn [3]; int LENC = Sizeof (SockAddr); int Alreadycon = 0; // Sercon is the previous server socket case while (1) {if (alleadycon <= 3) {// Accept Connection request conn [alreadycon] = accept (sercon, & cin, & lenc); if (conn); alreadycon] == INVALID_SOCKET) {AfxMessageBox ( "accept WRONG!");} else {// create a data receiving thread AfxBeginThread (readdata, & connn [alreadycon]); alreadycon = alreadycon 1; return 0;}} else {// avoid affecting Main thread running SLEEP (200);}}} 2 Implement data receiving thread. Uint WaitConnect (LPVOID SS) {socket * readsock; readsock = (socket *) ss; char buf [2000]; int revnum = 0; // Start cycle accept data while (revnum! = - 1) {// revnum <= 0 means the connection has been broken! Revnum = Recv (* ReadSock, BUF, 2000, 0); if (Revnum> 0) BUF [Revnum] = 0; // Storage has been accepted in the buffer // BUF. }} 3 Send Data // Conn [1] is used to accept the socket, and sendSTR is the sent data. Send (conn [1], lpctstr (sendstr), sendStr.getLength (), 0); 4 Close socket.

转载请注明原文地址:https://www.9cbs.com/read-1706.html

New Post(0)