哪位高手帮我看看这个c++组播程序哪个地方出问题了,急急急!

2024-12-01 11:16:24
推荐回答(1个)
回答1:

// MultiServer.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include
#include
#include
#include
#define Multiaddr "224.0.0.99"
#define Multiport 5000
#define BUFFER 2048

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char** argv)
{
WSADATA wsd;
int length;
struct sockaddr_in remote;
SOCKET sock,server;
TCHAR buf[BUFFER];
length=sizeof(struct sockaddr_in);
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("Initial faied!\n");
return -1;
}
if((sock=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_MULTIPOINT_C_LEAF|WSA_FLAG_MULTIPOINT_D_LEAF|WSA_FLAG_OVERLAPPED))==INVALID_SOCKET)
{
printf("Creat socket fail:%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
remote.sin_family=AF_INET;
remote.sin_port=htons(Multiport);
remote.sin_addr.s_addr=inet_addr(Multiaddr);
if((server=WSAJoinLeaf(sock,(SOCKADDR*)&remote,sizeof(remote),NULL,NULL,NULL,NULL,JL_BOTH))==INVALID_SOCKET)
{
printf("JOIN The TEAM Failed:%d\n",WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
while(1)
{
printf("DATA IS SENDING");
scanf("%s",buf);
if(sendto(server,(char*)buf,strlen(buf),0,(struct sockaddr*)&remote,sizeof(remote))==SOCKET_ERROR)
{
printf("DATA SENDINF FAILED:%d\n",WSAGetLastError());
closesocket(server);
closesocket(sock);
WSACleanup();
return -1;
}
if(strcmp(buf,"exit")==0) break;
Sleep(100);
}
closesocket(server);
closesocket(sock);
WSACleanup();
return 0;
}

//第一:你的问题是不是socket函数,没有引入库,所以找不到。我在程序开头用#pragma comment(lib, "ws2_32.lib") 加入了lib库
//第二:你的TCHAR buf[BUFSIZE]语句中,BUFSIZE没定义
//第三:strlen(BUFFER) 这语句我不知道你想干嘛?求长度吗?BUFFER是一个宏啊,一个整数!