www.vchelp.net里有一篇文章将的很好

解决方案 »

  1.   

    1、创建一个MFC对话框文档
    2、在App类中的
    BOOL CMyApp::InitInstance()
    {
    if( !AfxSocketInit() )
    {
    AfxMessageBox("IDP_SOCKETS_INIT_FAILED");
    return FALSE;
    }
    ……
    }
    3.Add New Class —— 从CSocket下派生一个CMySocket,并增加一个成员变量
    class CMySocket : public CSocket
    {
    public :
        CMyView *m_pView;
    }
    4.在CMyView内加入一个CMySocket类成员函数m_socket
    5.加入一个Button,为创建一个套接字
    void CMyView::OnButtonCreateSocket()
    {
        m_socket.m_pView = this;
        m_socket.Create(port,SOCK_DGRAM);
    }
    6.加入一个Button,让后加入该Button的消息响应函数
    void CMyView::OnButtonSend() 
    {
        char  buffer[] = "Test";
        m_socket.SendTo(buffer,strlen(buffer),"192.168.1.1",9000);
    }
    7.加入一个函数如下
    void CMyView::OnReceived()
    {
        char buffer[2048];
        CString hostaddr="";
        UINT    hostport=0;
        int len = m_socket.ReceiveFrom(buffer,2048,hostaddr,hostport);
        buffer[len]='0';
        MessageBox(buffer);
    }
    8.加入CMySocket的一虚函数OnReceive
    {
        m_pView->OnReceived();
    }Test it
    ~~~~~~~~~~~哇哈哈~~~~~~~~~~~泡泡糖~~~~~~~
    搞定
      

  2.   

    #if !defined(AFX_SERVERSOCK_H__9CF40F89_7AE5_467B_BBA2_84D838587CE3__INCLUDED_)
    #define AFX_SERVERSOCK_H__9CF40F89_7AE5_467B_BBA2_84D838587CE3__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // Serversock.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CServersock command targetclass CServersock : public CAsyncSocket
    {
    // Attributes
    public:
     CString m_user; //用户名
         CString m_subject; //考试科目
     CString m_style;  //考试方式
     CString            m_state;
     CString m_score;
     CString m_f;
     bool               m_isedit;
     char rx_buf[100];
     int ServeNo;
    // Operations
    public:
    CServersock();
    virtual ~CServersock();// Overrides
    public:
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CServersock)
    public:
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL

    // Generated message map functions
    //{{AFX_MSG(CServersock)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    protected:
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_SERVERSOCK_H__9CF40F89_7AE5_467B_BBA2_84D838587CE3__INCLUDED_)
    // Serversock.cpp : implementation file
    //#include "stdafx.h"
    #include "NT_server.h"
    #include "Serversock.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CServersockCServersock::CServersock()
    {
    m_f="f";
    m_subject="未知";  //考试科目
    m_style="未知";    //考试方式
        m_state="未准备好"; //考试状态
    m_score="0";
    m_isedit=FALSE;
    }CServersock::~CServersock()
    {
    }
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CServersock, CAsyncSocket)
    //{{AFX_MSG_MAP(CServersock)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0/////////////////////////////////////////////////////////////////////////////
    // CServersock member functionsvoid CServersock::OnReceive(int nErrorCode) 
    {
    m_isedit=true;
    //AfxMessageBox("r");
    // TODO: Add your specialized code here and/or call the base class
    if(Receive(rx_buf,100))
    {

    if((rx_buf[0]=='u')&&(rx_buf[1]=='='))
    {
    m_user=rx_buf+2;
    //AfxMessageBox("u  "+m_user);
    for(int i=0;i<100;i++)
    {
    rx_buf[i]=0;
    }
    }
    if((rx_buf[0]=='s')&&(rx_buf[1]=='='))
    {
    m_subject=rx_buf+2;
    //AfxMessageBox("s  "+m_subject);
    for(int i=0;i<100;i++)
    {
    rx_buf[i]=0;
    }
    //AfxMessageBox(ServeNo);
    }
        
    if((rx_buf[0]=='t')&&(rx_buf[1]=='='))
    {
    m_state=rx_buf+2;
    for(int i=0;i<100;i++)
    {
    rx_buf[i]=0;
    }
    }
    if((rx_buf[0]=='y')&&(rx_buf[1]=='='))
    {
    m_style=rx_buf+2;
    //AfxMessageBox("Y  "+m_style);
    for(int i=0;i<100;i++)
    {
    rx_buf[i]=0;
    }
    }
    if((rx_buf[0]=='c')&&(rx_buf[1]=='='))
    {
    m_score=rx_buf+2;
    //AfxMessageBox(rx_buf);
    for(int i=0;i<100;i++)
    {
    rx_buf[i]=0;
    }

    }
    if((rx_buf[0]=='q')&&(rx_buf[1]=='='))
    {
    m_user="";
    m_score="";
    m_style="";
    m_state="";
    m_subject="";
    Close();
    //AfxMessageBox("quit"); }

    //AfxMessageBox(m_user);
    }
    else
    AfxMessageBox("fail");
    CAsyncSocket::OnReceive(nErrorCode);
    }#if !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
    #define AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // mysock.h : header file
    //
    #include "serversock.h"
    /////////////////////////////////////////////////////////////////////////////
    // Cmysock command targetclass Cmysock : public CAsyncSocket
    {
    // Attributes
    public:
     CServersock serversocket[5];
     int AcceptNo;
     int ConnectNo;
     char rx_buf[100];
     int ServeNo;
     CString m_user; //用户名
         CString m_subject; //考试科目
     CString m_style;  //考试方式
     CString            m_state;
     CString m_score;
     CString m_f;
    // Operations
    public:
    Cmysock();
    virtual ~Cmysock();// Overrides
    public:
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(Cmysock)
    public:
    virtual void OnAccept(int nErrorCode);
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL // Generated message map functions
    //{{AFX_MSG(Cmysock)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    protected:
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
    // mysock.cpp : implementation file
    //#include "stdafx.h"
    #include "NT_server.h"
    #include "mysock.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CmysockCmysock::Cmysock()
    {

    }Cmysock::~Cmysock()
    {
    }
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(Cmysock, CAsyncSocket)
    //{{AFX_MSG_MAP(Cmysock)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0/////////////////////////////////////////////////////////////////////////////
    // Cmysock member functions
    void Cmysock::OnAccept(int nErrorCode)
    {
    if(AcceptNo>4) return;
    if(!Accept(serversocket[AcceptNo]))
    {//接收请求失败
    AfxMessageBox("accept fail!",12);
    }
    else
    {//接收请求成功
    serversocket[AcceptNo].ServeNo=AcceptNo;
    //AfxMessageBox("accept ok",12);
    switch(AcceptNo)
    {//根据接收次序,向客户端发出信息
    case 0:
    {
    serversocket[AcceptNo].Send("Message 0",10);
    break;
    }
    case 1:
    {
    serversocket[AcceptNo].Send("Message 1",10);
    break;
    }
    case 2:
    {
    serversocket[AcceptNo].Send("Message 2",10);
    break;
    }
    case 3:
    {
    serversocket[AcceptNo].Send("Message 3",10);
    break;
    }
    case 4:
    {
    serversocket[AcceptNo].Send("Message 4",10);
    break;
    }
    default:
    break;
    }
    // AfxMessageBox("accept client!",14);
    AcceptNo++;
    }
    CAsyncSocket::OnAccept(nErrorCode);
    } void Cmysock::OnReceive(int nErrorCode) 
    {
    // TODO: Add your specialized code here and/or call the base class

    CAsyncSocket::OnReceive(nErrorCode);
    }以下是客户端:
    #if !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
    #define AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // mysock.h : header file
    //
    #include "serversock.h"
    /////////////////////////////////////////////////////////////////////////////
    // Cmysock command targetclass Cmysock : public CAsyncSocket
    {
    // Attributes
    public:
     CServersock serversocket[5];
     int AcceptNo;
     int ConnectNo;
     char rx_buf[100];
     CString m_f;
     CString m_user; //用户名
     CString m_subject;  //考试科目
     CString m_style;    //考试方式
     CString            m_state; //考试状态
     CString m_score;    //得分
     bool               m_start;
     bool m_end;
     int ServeNo;
    // Operations
    public:
    Cmysock();
    virtual ~Cmysock();// Overrides
    public:
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(Cmysock)
    virtual void OnAccept(int nErrorCode);
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL // Generated message map functions
    //{{AFX_MSG(Cmysock)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    protected:
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
    #if !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
    #define AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // mysock.h : header file
    //
    #include "serversock.h"
    /////////////////////////////////////////////////////////////////////////////
    // Cmysock command targetclass Cmysock : public CAsyncSocket
    {
    // Attributes
    public:
     CServersock serversocket[5];
     int AcceptNo;
     int ConnectNo;
     char rx_buf[100];
     CString m_f;
     CString m_user; //用户名
     CString m_subject;  //考试科目
     CString m_style;    //考试方式
     CString            m_state; //考试状态
     CString m_score;    //得分
     bool               m_start;
     bool m_end;
     int ServeNo;
    // Operations
    public:
    Cmysock();
    virtual ~Cmysock();// Overrides
    public:
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(Cmysock)
    virtual void OnAccept(int nErrorCode);
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL // Generated message map functions
    //{{AFX_MSG(Cmysock)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    protected:
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYSOCK_H__CF286E22_DF71_48CA_B043_A93068257A3A__INCLUDED_)
      

  3.   

    还要注意Include 
    #include <afxsock.h>和在class CMySocket前加上class CMyView;
    在class CMyView上加上#include "MySocket.h"
      

  4.   

    void CMyView::OnButtonCreateSocket()
    {
        m_socket.m_pView = this;
        m_socket.Create(port,SOCK_DGRAM);
    }
    的port改为9000
      

  5.   

    高手:可以搞定这个!!!
    http://www.csdn.net/Expert/topic/470/470143.shtm