Write network applications in vc6.0

Source: Internet
Author: User
Tags htons
Compile a network application in vc6.0}

{In today's increasingly evolving network technology, if you can write a practical network application, you can not only stimulate your interest in the network, but also promote your pursuit of network knowledge, at the same time, the development process is also a good learning process.
In vc6.0, MFC has good support for network programming. For network applications of different purposes, VC supports different encapsulation classes, such as FTP and HTTP, this allows users to quickly develop corresponding programs. However, it also makes users lose some opportunities to understand the underlying mechanism of network program running, more importantly, it also loses some flexibility. This section describes the general steps for development using Socket Sockets for your reference.
Compiling network applications in windows and Unix basically uses Socket Sockets for data communication. Socket sockets are inherited from the Unix environment, its role in the program can be understood as a proxy for Network Data Communication. Its Design Concept in Windows is basically not much different from that in Unix. It is divided into two parts: server socket and client socket, the design concept is as follows:
Part 1 Server
1. Create a server socket ).
2. Bind the server socket and start listening for the listen ).
3. Accept the client's connection request (accept) and create a receiving process.
4. Start data transmission (send and receive ).
5. Close the socket (closesocket ).
Part 2 client
1. Create a client socket ).
2. Connect to the remote server. If accepted, a receiving process is created.
3. Start data transmission (send and receive ).
4. Close the socket ).
The above design idea is the basic step of our development and also the basic method for running most network applications. The following describes how to implement it in VC.
Server:
1. Establish a support socket project.
The App Wizard is used to create the mfc exe project. During step 4 of the wizard, when "What features wocould you like include ?" Select "Windows Sockets. You can select options for other steps based on the actual application. The created Project supports socket and has been initialized.
To add socket support to an existing project, you only need to perform the following two tasks:
1? Bi? The stdafx. h file contains the header file Winsock. H (# include "Winsock. H ").
2? Inback? Socket, add the following initialization socket code in the application class member function: ": initinstance.
If (! Afxsocketinit ())
{Afxmessagebox (idp_sockets_init_failed );
Return false ;}
2. Create a service socket and a listening thread.
// Create a service socket
Socket sercon = socket (pf_inet, sock_stream, 0 );
// Determine whether the creation is successful
If (sercon = invalid_socket)
{Afxmessagebox ("server wrong !");
Return-1 ;}
// Configure socket address and other information
Sockaddr_in sin;
Sin. sin_family = af_inet;
// Specify the local address
Sin. sin_addr.s_addr = htonl (inaddr_any );
// Specify the server port number nport, which can be customized
Int nport = 5080;
Sin. sin_port = htons (nport );
// Bind the address information to the socket.
If (BIND (sercon, (lpsockaddr) & sin, sizeof (SIN) = socket_error)
{Afxmessagebox ("bind wrong !");
Return-1 ;}
// Create a listening Queue (3 in size) and start listening
If (Listen (sercon, 3) = socket_error)
{Afxmessagebox ("Listen wrong !");
Return-1 ;};
① Implement the listening thread and create a data receiving thread.
// Create and implement a listening thread where the program needs to start listening for connections.
// Create a listening thread (in program start or button event implementation)
Afxbeginthread (waitconnect, null );
// Implement the listening thread
Uint waitconnect (lpvoid lpparm)
{Socket conn [3];
Int lenc = sizeof (sockaddr );
Int alreadycon = 0;
// Sercon is the server socket created earlier
While (1)
{If (alreadycon <= 3)
{// Accept the 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 the running of the main thread
Sleep (200 );}
}
}
② Implement the data receiving thread.
Uint waitconnect (lpvoid SS)
{Socket * readsock;
Readsock = (socket *) ss;
Char Buf [2000];
Int revnum = 0;
// Start to receive data cyclically
While (revnum! =-1)
{// Revnum <= 0 indicates that the connection is disconnected!
Revnum = Recv (* readsock, Buf, 2000,0 );
If (revnum> 0)
Buf [revnum] = 0; // truncates the buffer
// Store accepted data in Buf .}
}
③ Send data
// Conn [1] is the socket used to accept connections, and sendstr is the data sent.
Send (conn [1], lpctstr (sendstr), sendstr. getlength (), 0 );
④ Close the socket.
// Conn [1] is the socket used to accept the connection
Closesocket (conn [1]);
Client client:
Many client programs have the same, similar, or even the same code as the server.
1. Establish a support socket project.
The method is the same as that on the server side.
2. Create a customer socket and connect to the server.
// Nhost must be a remote server, IP address, or domain name specified by the user.
Cstring nhost;
// H is the address information
Struct hostent * h;
H = gethostbyname (nhost );
// Nhost requires the remote service port number specified by the user
Int nport;
Socket con_client;
Sockaddr_in Csin;
If (H! = NULL)
{// Create a socket
Con_client = socket (af_inet, sock_stream, 0 );
Csin. sin_family = af_inet;
Memcpy (& (Csin. sin_addr.s_addr), H-> h_addr, sizeof (INT ));
Csin. sin_port = htons (nport );
// Start the connection
If (connect (con_client, (lpsockaddr) & Csin, sizeof (Csin )))
{// Afxmessagebox ("Connect wrong !");
Return-1 ;}
Else
{// Connection successful, data receiving thread created
Afxbeginthread (readdata, & con_client );}
}
3. Implement the data receiving thread.
The code is exactly the same as that on the server side.
4. Send data.
// Con_client is the socket connecting to the server.
Send (con_client, lpctstr (sendstr), sendstr. getlength (), 0 );
5. Close the socket.
// Con_client is the socket connecting to the server.
Closesocket (conn [1]);
In actual application, adjust and change the scope of some variables as needed.
The above programs are successfully debugged in vc6.0, win NT4.0, and Windows 98.
Finally, the csocket class in vc6.0 MFC is an MFC encapsulation of the socket, and it supports document serialization to facilitate transmission of different data types. The reason why csocket is not introduced earlier in this article is that the implementation method of csocket is the same as that described above and is simpler. Another more important reason is the ease of porting to Unix programming because Unix supports socket .}
 
Related Article

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.