// CRevPlayWnd message handlers
LRESULT CRevPlayWnd::OnConnect(WPARAM wParam,LPARAM lParam)
{
int status;
int SendLen;
int socket;
char szRev[80];
char szBuff[80];
char Msg[]="请发送数据";
u_long block=0;
socket=(SOCKET)wParam;
if (WSAGETSELECTERROR(lParam))
{//建立连接失败
MessageBox("不能连接服务器", "连接失败", MB_OK);
if(WaitDlg)
WaitDlg.EndDialog(IDCANCEL);
//关闭socket
closesocket(socket);
return -1;
}
if (WSAGETSELECTEVENT(lParam) == FD_CONNECT)
{//成功建立连接
if(WaitDlg)
WaitDlg.EndDialog(IDCANCEL);
//发送请求发送数据命令给发送端
SendLen=send(socket,Msg,sizeof(Msg),0);
if(SendLen!=sizeof(Msg))
{//请求数据发送失败
MessageBox("请求错误" "Send");
closesocket(socket);
return -1;
}
if(SendLen==sizeof(Msg))
{
WSAAsyncSelect(socket,m_hWnd, 0, 0 );
status=ioctlsocket(socket,FIONBIO,&block);
if(status==SOCKET_ERROR)
{
sprintf(szBuff,"Err: %d",WSAGetLastError());
MessageBox(szBuff);
return -1;
}
//接收数据
status =recv(socket, szRev,sizeof(szRev),0);
if (status)
{
//得到组播IP地址和端口
sscanf(szRev,"%s%d",strDestAddr,&DestPort);
sprintf(szBuff,"请加入组:%s,端口:%d",strDestAddr,DestPort);
MessageBox(szBuff,"接收请求");
sprintf(szBuff,"接收播放:组:%s,端口:%d",strDestAddr,DestPort);
::SetWindowText(GetParent()->m_hWnd,szBuff);
//关闭socket
closesocket(socket);
}
if(status==0)
{
MessageBox("对方关闭连接");
closesocket(socket);
return -1;
}
}
}
return 0;
}