哪位高手帮我看看这个c++组播程序哪个地方出问题了,急急急!
结合网上的组播程序自己修改了一下,在VC++环境中写了个组播程序的服务器端和客户端。操作的过程是这样的,先分别建立了工作空间MulticServer和MulticClie...
结合网上的组播程序自己修改了一下,在VC++环境中写了个组播程序的服务器端和客户端。操作的过程是这样的,先分别建立了工作空间MulticServer和MulticClient,然后再这两个空间中以同样的名字建立了两个新的工程,最后新建了两个c++源文件Server.c和Client.c.编译过程中出现了错误,好像是不能识别socket的某些函数,哪位高手帮忙给我改一下程序,如能够运行的话,再追加50分,谢谢。附上源程序:
Server.c:
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define Multiaddr "224.0.0.99"
#define Multiport 5000
#define BUFFER 2048
int main(int argc, char** argv)
{
WSADATA wsd;
int length;
struct sockaddr_in remote;
SOCKET sock,server;
TCHAR buf[BUFSIZE];
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(BUFFER),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;
} 展开
Server.c:
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define Multiaddr "224.0.0.99"
#define Multiport 5000
#define BUFFER 2048
int main(int argc, char** argv)
{
WSADATA wsd;
int length;
struct sockaddr_in remote;
SOCKET sock,server;
TCHAR buf[BUFSIZE];
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(BUFFER),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;
} 展开
1个回答
展开全部
// MultiServer.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#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是一个宏啊,一个整数!
//
#include "StdAfx.h"
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#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是一个宏啊,一个整数!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询