vc实现简单那UDP通信程序在编译是出现错误,请高手帮忙看一下啊,谢谢!
//Client端代码#include<winsock2.h>#include<iostream.h>#include<stdio.h>voidinitClient();...
//Client端代码
#include <winsock2.h>
#include <iostream.h>
#include <stdio.h>
void initClient();
int main()
{
initClient();
return 0;
}
void initClient()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(2,2),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSend;
sockSend.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockSend.sin_port=htons(4000);
sockSend.sin_family=AF_INET;
char buff[1024];
strcpy(buff,"hello,it's the first!");
int i=500;
while(--i)
{
int lenword;
lenword=sendto(s,buff,strlen(buff)+1,0,(sockaddr *)&sockSend,sizeof(sockaddr));
cout<<lenword<<","<<sockSend.sin_port<<":"<<sockSend.sin_addr.S_un.S_addr<<endl;
}
closesocket(s);
WSACleanup();
}
//Server端代码
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
using namespace std;
void initNet();
int main()
{
initNet();
return 0;
}
void initNet()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(1,1),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSrc;
sockSrc.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
sockSrc.sin_port=htons(4000);
sockSrc.sin_family=AF_INET;
bind(s,(SOCKADDR *)&sockSrc,sizeof(SOCKADDR));
char recBuff[1025];
memset(recBuff,0,1024);
SOCKADDR_IN sockRec;
int len=sizeof(SOCKADDR);
int x=-1;
cout<<sockSrc.sin_port<<":"<<sockSrc.sin_addr.S_un.S_addr<<endl;
while(x==-1)
{
x=recvfrom(s,recBuff,sizeof(recBuff),0,(sockaddr *)&sockRec,&len);
}
printf("the receive is:%s,%d \n",recBuff,x);
closesocket(s);
WSACleanup();
}
c:\documents and settings\administrator\桌面\hello\lll.cpp(113) : fatal error C1010: unexpected end of file while looking for precompiled header directive 展开
#include <winsock2.h>
#include <iostream.h>
#include <stdio.h>
void initClient();
int main()
{
initClient();
return 0;
}
void initClient()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(2,2),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSend;
sockSend.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockSend.sin_port=htons(4000);
sockSend.sin_family=AF_INET;
char buff[1024];
strcpy(buff,"hello,it's the first!");
int i=500;
while(--i)
{
int lenword;
lenword=sendto(s,buff,strlen(buff)+1,0,(sockaddr *)&sockSend,sizeof(sockaddr));
cout<<lenword<<","<<sockSend.sin_port<<":"<<sockSend.sin_addr.S_un.S_addr<<endl;
}
closesocket(s);
WSACleanup();
}
//Server端代码
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
using namespace std;
void initNet();
int main()
{
initNet();
return 0;
}
void initNet()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(1,1),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSrc;
sockSrc.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
sockSrc.sin_port=htons(4000);
sockSrc.sin_family=AF_INET;
bind(s,(SOCKADDR *)&sockSrc,sizeof(SOCKADDR));
char recBuff[1025];
memset(recBuff,0,1024);
SOCKADDR_IN sockRec;
int len=sizeof(SOCKADDR);
int x=-1;
cout<<sockSrc.sin_port<<":"<<sockSrc.sin_addr.S_un.S_addr<<endl;
while(x==-1)
{
x=recvfrom(s,recBuff,sizeof(recBuff),0,(sockaddr *)&sockRec,&len);
}
printf("the receive is:%s,%d \n",recBuff,x);
closesocket(s);
WSACleanup();
}
c:\documents and settings\administrator\桌面\hello\lll.cpp(113) : fatal error C1010: unexpected end of file while looking for precompiled header directive 展开
展开全部
在工程选项里去掉“使用预编译头文件”之类的选择
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
出现,就身形一晃,自身
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
winsock2和winsock在定义上是有冲突的,所以你在编译的时候如果调用方法是套用的winsock的话肯定是通不过的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询