可以每和一台服务器发送数据完成后,就DisConnect
然后再和另外的服务器Connect
然后再和另外的服务器Connect
解决方案 »
- 某一个MFC exe程序,既可以独立运行,也可以在作为dll,用别的进程运行?
- 在vc6中如何删除一个类?
- 弹出窗口拦截是怎么作出来的?
- 高分在线求档!
- 画线,画圆只能在OnDraw()或OnPaint()函数中吗?菜鸟问题,大伙帮帮忙。
- 有没有实现过Visual C++实现Flash动画播放
- 怎么在ListCtrl中加入位图,而不是图标?
- 十万火急!请教各位大侠,怎样用VC6读写数据库中的图片
- 小弟请教 请写下你的答案,非常感谢!!
- 用VC完成了一个俄罗斯方块的游戏,高兴!散分!(我是VC菜鸟!别笑我)
- 在SDK中如何调用其他的可执行文件?
- 请高手指教:我在CSpliteView中动态创建的CComboBox对象,如何在OnCommand中捕获它的CBN_EDITCHANGE事件
并且对每个服务器都建立一个连接(a new SOCKET)不久搞定
在一个已经连接的套接字上做了一个连接请求!
在VC中用Error Lookup工具可以查到
记得分哟,老本都给你了多播
#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);
}