可以每和一台服务器发送数据完成后,就DisConnect
然后再和另外的服务器Connect

解决方案 »

  1.   

    当然是在客户端建立一个服务器列表
    并且对每个服务器都建立一个连接(a new SOCKET)不久搞定
      

  2.   

    好像是用BOOL SetSockOpt( int nOptionName, const void* lpOptionValue, int nOptionLen, int nLevel = SOL_SOCKET );来设置socket的广播属性,具体的请查msdn!
      

  3.   

    还有一个问题,我用GetLastError()函数返回Connect的错误,调试信息为10056,这是什么意思,msdn中没有有关的解释呀
      

  4.   

    这说明:
          在一个已经连接的套接字上做了一个连接请求!
    在VC中用Error Lookup工具可以查到
      

  5.   

    不是跟你说了吗,你先disconnect然后再connect
      

  6.   

    不好意思DISCONNECT 是那个类的函数,好象不是Casynccsocket类的
      

  7.   

    我就是要分,有分吗?哈哈,[email protected]
    记得分哟,老本都给你了多播
    #if !defined(AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_)
    #define AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_#if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000
    // MulticastSocket.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CMulticastSocket command targetclass CMulticastSocket : public CAsyncSocket
    {
    // Attributes
    public:// Operations
    public:
    CMulticastSocket();
    virtual ~CMulticastSocket();// Overrides
    public:
    BOOL bDataReceived;
    BOOL JoinGroup(CString, UINT, UINT, BOOL);
    BOOL LeaveGroup();
    BOOL SendTo(const void*, int);
    void SetLoopBack(BOOL);
    BOOL SetTTL(UINT nTTL);
    BOOL CreateSendingSocket(UINT, BOOL);
    BOOL CreateReceivingSocket(LPCTSTR, UINT);
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMulticastSocket)
    public:
    virtual void OnReceive(int nErrorCode);
    //}}AFX_VIRTUAL // Generated message map functions
    //{{AFX_MSG(CMulticastSocket)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG// Implementation
    public:
    char m_strBuffer[32000]; // Receiving buffer for the packet that has arrived
    SOCKADDR_IN m_saHostGroup; // SOCKADDR structure to hold IP/Port of the Host group to send data to it
    ip_mreq m_mrMReq; // Contains IP and interface of the host group
    UINT m_nSendersPort; // Holds Port No. of the socket from which last packet was received
    CString m_strSendersIP; // Hold IP of the socket from which the last packet was received
    UINT m_nLocalPort; // Ephemeral port number of the sending port
    CString m_strLocalIP; // IP Address of the local host or your machine
    BOOL bForceNoLoopback; // If interface does not support lopback and the service is required, the bool is set to true
    CAsyncSocket m_SendSocket; // Socket for sending data to the host group
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MULTICASTSOCKET_H__269E2C7F_2037_11D3_8EF3_0000C0FD25F8__INCLUDED_)// MulticastSocket.cpp : implementation file
    //#include "stdafx.h"
    #include "CMulticastSocket.h"
    #include "MulticastSocket.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CMulticastSocketCMulticastSocket::CMulticastSocket()
    {
    bForceNoLoopback = FALSE;
    bDataReceived = TRUE; /* Variable defined for this project. Not necessarily part of CMulticastSocket */
    }CMulticastSocket::~CMulticastSocket()
    {
    }
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CMulticastSocket, CAsyncSocket)
    //{{AFX_MSG_MAP(CMulticastSocket)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0/////////////////////////////////////////////////////////////////////////////
    // CMulticastSocket member functionsBOOL CMulticastSocket::CreateReceivingSocket(LPCTSTR strGroupIP, UINT nGroupPort)
    {
    /* Create socket for receiving packets from multicast group */
    if(!Create(nGroupPort, SOCK_DGRAM, FD_READ))
    return FALSE; BOOL bMultipleApps = TRUE; /* allow reuse of local port if needed */
    SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET); /* Fill m_saHostGroup_in for sending datagrams */
    memset(&m_saHostGroup, 0, sizeof(m_saHostGroup));
    m_saHostGroup.sin_family = AF_INET;
    m_saHostGroup.sin_addr.s_addr = inet_addr(strGroupIP);
    m_saHostGroup.sin_port = htons((USHORT)nGroupPort); /* Join the multicast group */
    m_mrMReq.imr_multiaddr.s_addr = inet_addr(strGroupIP); /* group addr */ 
    m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY); /* use default */ 
    if(setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
    return FALSE; return TRUE;
    }BOOL CMulticastSocket::CreateSendingSocket(UINT nTTL, BOOL bLoopBack)
    {
    if(!m_SendSocket.Create(0, SOCK_DGRAM, 0)) // Create an unconnected UDP socket
    return FALSE; if(!SetTTL(nTTL)) // Set Time to Live as specified by user
    AfxMessageBox("Warning! Error Setting TTL"); SetLoopBack(bLoopBack); // Enable/Disable Loopback return TRUE;
    }BOOL CMulticastSocket::SetTTL(UINT nTTL)
    {
    /* Set Time to Live to parameter TTL */
    if(m_SendSocket.SetSockOpt(IP_MULTICAST_TTL, &nTTL, sizeof(int), IPPROTO_IP) == 0)
    return FALSE; /* Error Setting TTL */
    else
    return TRUE; /* else TTL set successfully */
    }void CMulticastSocket::SetLoopBack(BOOL bLoop)
    {
    /* Set LOOPBACK option to TRUE OR FALSE according to IsLoop parameter */
    int nLoopBack = (int)bLoop;
    if(m_SendSocket.SetSockOpt(IP_MULTICAST_LOOP, &nLoopBack, sizeof(int), IPPROTO_IP) == 0)
    {
    if(!bLoop) /* if required to stop loopback */
    {
    bForceNoLoopback = TRUE; /* Internally making a note that loopback has to be disabled forcefilly */ // Get IP/Port for send socket in order to disable loopback forcefully */
    char localHost[255];
    gethostname(localHost, 255);
    struct hostent *host = gethostbyname(localHost); /* Get local host IP */
    m_strLocalIP = inet_ntoa (*(struct in_addr*)*host->h_addr_list);
    CString Dummy; // Dummy string to be passed to the GetSockName function
    m_SendSocket.GetSockName(Dummy, m_nLocalPort); /* Get Port Number for Sending Port */
    }
    }
    }void CMulticastSocket::OnReceive(int nErrorCode)
    {
    int nError = ReceiveFrom (m_strBuffer, 32000, m_strSendersIP, m_nSendersPort);
    if(nError == SOCKET_ERROR)
    AfxMessageBox("Error receiving data from the host group");
    else
    {
    if (!bForceNoLoopback || (bForceNoLoopback && !(m_strSendersIP == m_strLocalIP && m_nSendersPort == m_nLocalPort)))
    {
    // 1. If loopbackback is not to be forced then interface handles the loopback itself
    // 2. If you have to loopback and SOCKOPT LOOPBACK fails, no problem, interfaces loopback by default
    // 3. If you have to stop loopback and SOCKOPT LOOPBACK fails, ignore messages coming from your own sending socket // TODO : Add your code for here. The packet received is in m_strBuffer
    bDataReceived = TRUE; /* Making note that a message has arrived */
    }
    } CAsyncSocket::OnReceive(nErrorCode); 
    }BOOL CMulticastSocket::LeaveGroup()
    {
    if(setsockopt (m_hSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq)) < 0)
    return FALSE; m_SendSocket.Close(); // Close sending socket
    Close(); // Close receving socket
    return TRUE;
    }BOOL CMulticastSocket::SendTo(const void* strMessage, int nSize)
    {
    if(m_SendSocket.SendTo(strMessage, nSize, (SOCKADDR*)&m_saHostGroup, sizeof(SOCKADDR), 0) == SOCKET_ERROR)
    return FALSE;
    else
    return TRUE;
    }BOOL CMulticastSocket::JoinGroup(CString GroupIP, UINT nGroupPort, UINT nTTL, BOOL bLoopback)
    {
    if(!CreateReceivingSocket(GroupIP, nGroupPort)) /* Create Socket for receiving and join the host group */
    return FALSE;
    if(!CreateSendingSocket(nTTL, bLoopback)) /* Create Socket for sending */
    return FALSE; return TRUE;
    }
    //CMulticastSocketDlg.h
    private:
    CMulticastSocket m_Socket;//CMulticastSocketDlg.cpp
    void CCMulticastSocketDlg::OnSend() 
    {
    UpdateData(TRUE);
    if(!m_Socket.SendTo(LPCTSTR(m_ChatMessage), m_ChatMessage.GetLength() + 1)) // Send message + NULL
    AfxMessageBox("Send Failed!"); m_ChatMessage = ""; /* Clear the Send message field */
    UpdateData(FALSE);
    }