VC++6.0 Network Programming Socket programming

Source: Internet
Author: User

Tutorials for the software downloaded from CSDN:

As a beginner, it's hard to get started with socket programming, but when I figure out some of the problems, I look back at some of the problems we encountered before and find out that socket programming is not that complicated. Then I will tell you some of the difficulties I encountered, and fix the solution.

First we want to achieve a simple point-to-point network communication, there should be a customer and a server

Let's do the client first. Set up the client dialog module as shown first:

First create a project based on MFC Appwizard[exe], the project is named Socket__002 (here is the name of my project, you can also name the project.) Click OK to select the Basic dialog box,

Click Next and click "Done" after you hook up on Windows Sockets[w].

Established Client dialog box:

Control properties:

Description: Idc_edit1 Port number edit box, idc_edit3 send text box, Idc_ipaddress1 IPD Address box, idc_list1 list box.

Now that the module has been built, the next step is to write the code, but before we write the code, we need to clear the idea.

How the client and the server to achieve communication, in fact, is the data sender client will send the information to write a socket, in the intermediate link to the receiving end of the server socket, it can be taken from the receiving end of the application socket socket. Therefore, the data transfer between the two applications is done via sockets, and as to how the two sockets are transmitted, we don't have to, we just need to load the information into the socket.

As a client, you should have the following features:

1, send a connection request to the server at the specified address;

2. Send information to a server that has been successfully connected;

3, the active disconnection with the server connection;

When we already know the function of the client, we should think of how we can implement these functions in code, of course, for beginners to write these code is still a bit difficult, we could first refer to the information on the network to understand the principle, this is what I wrote this article intended.

VC6.0 the workspace in the interface click the ClassView tab, right-click the "socket_002 Classes"-----"newclass" To add a new class "Client_socket" to the base class generic CWnd.

Right-click Csocket_002dlg to add a common member variable M_STARTC to the Csocket_002dlg class;

In order for the new class to be associated with a dialog class, you need to include the Socket_002dlg.h header file in the

#include "ClientSocket.h" remember to put it on the first line.  Add Cclientsocket M_cclientsocket to the public of the class definition; In the same way, add # include "Socket_002dlg.h" to the "ClientSocket.h" header file.

Add the member function OnConnect () to the newly added class Client_socket, function prototype void OnConnect (int nerrorcode). Add the following code in the OnConnect () function:

voidCclientsocket::onconnect (intNerrorcode)//Casyncsocket::onconnect{    if(Nerrorcode) {AfxMessageBox ("connection failed, please try again! "); return ; } ((Csocket_002dlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.addstring ("The connection server is successful! "); ((Csocket_002dlg*) (AfxGetApp ()->m_pmainwnd))M_listwords.settopindex (((Csocket_002dlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.getcount ()-1);}

The next step is to create a click function on the dialog box Connection button, which reads as follows:

voidCchat_clientdlg::onbutton1 () {//Todo:add your control notification handler code here    if(!M_STARTC) {BYTE nfild[4];        CString SIP;        UpdateData (); M_serverip.getaddress (nfild[0],nfild[1],nfild[2],nfild[3]); Sip.format ("%d.%d.%d.%d", nfild[0],nfild[1],nfild[2],nfild[3]); M_cclientsocket.create ();//Create socketM_cclientsocket.connect (Sip,m_sport);//ConnectionM_startc=~false;//Start Connection}} Disconnect button program:voidCsocket_002dlg::onbutton2 () {//Todo:add your control notification handler code hereM_cclientsocket.close ();//Close Soket ConnectionM_listwords.addstring ("Disconnect from server"); M_STARTC=false;//Disconnect Connectionsend button Program:voidCsocket_002dlg::onbutton3 () {//Todo:add your control notification handler code hereUpdateData (); M_cclientsocket.send (M_swords,m_swords.getlength ());//Send StringM_listwords.addstring ("Send:"+m_swords); M_listwords.settopindex (M_listwords.getcount ()-1);//scroll baralso add the following code in Csocket_002dlg::oninitdialog () bool Csocket_002dlg::oninitdialog () {... m_startc=false;//a connection was not established when the form was initialized...}

Server dialog Box Char_server module

Create a Char_server project like the client module,

Features of the server:

1, monitoring;

2, accept the client request;

3, stop monitoring;

4, receive information;

The principle is basically similar to the client setup. Create two classes a listener class Clistensocket, a class Cserversocket to receive information, is also based on the generic CWnd class as the base class. Add in "Chat_serverdlg.h"

#include "ServerSocket.h"

#include "ListenSocket.h"

Add two more public member variables

Cserversocket M_serversocket;

Clistensocket M_listensocket;

Then add the include "Chat_serverdlg.h" in the "ServerSocket.h", "ListenSocket.h" header file, and note the location where the include "Chat_serverdlg.h" is placed before pre-compilation In case of an error.

Server dialog box

The next step is to add member functions in the created Clistensocket class, void onaccept (int nerrorcode), and I won't go into the details. The member function code is as follows:

voidClistensocket::onaccept (intNerrorcode) {Accept ((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))m_serversocket);((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_serversocket.asyncselect (fd_read|fd_close);((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.addstring ("a connection request was accepted for a client");//Accept connection Requests((ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))M_listwords.settopindex (((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.getcount ()-Next , add two member functions in the Cserversocket class:voidOnReceive (intNerrorcode),voidOnClose (intnerrorcode), the code is as follows:voidCserversocket::onreceive (intNerrorcode) {    Charsztemp[ $];//character Array    intN=receive (Sztemp, $);//The received string is saved to sztemp[]. sztemp[n]=' /';    CString stemp; Stemp.format ("Received:%s", sztemp);//the received character array content, formatted as a string((ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.addstring (stemp);//display the received content to the main dialog box((ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))M_listwords.settopindex (((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.getcount ()-1);}voidCserversocket::onclose (intNerrorcode) {(Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.addstring ("Client Disconnects"); ((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))M_listwords.settopindex (((Ccsock_chatserverdlg*) (AfxGetApp ()->m_pmainwnd))->m_listwords.getcount ()-1); Close ();//Disconnect Connection}

The next step is to run and the results are as follows:

Task completion, if you do not understand where you can refer to the Visual C + + network programming----Editor-in-chief is Archie;

VC++6.0 Network Programming Socket Programming (RPM)

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.