一个C语言聊天系统的问题
我设计一个聊天程序,服务端代码如下,总是提示一个错误,不知道哪里错了,还请高手指点一下#include<WINSOCK2.H>#include<iostream>usin...
我设计一个聊天程序,服务端代码如下,总是提示一个错误,不知道哪里错了,还请高手指点一下
#include<WINSOCK2.H>
#include<iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
#define IP"127.0.0.1"
#define Port (16000)
int bind(
SOCKET s,
const struct sockaddr *name,
int namelen
);
int recvfrom(
SOCKET s,
char FAR*buf,
int len,
int flags,
struct sockaddr FAR *from,
int FAR *fromlen
);
int main()
{
WSADATA stWSAData;
int ir=::WSAStartup(MAKEWORD(2,0),&stWSAData);
if(ir!=0)
{
cout<<"初始化Winsock失败!错误代码:"<<ir<<endl;
return 0;
}
//设置地址,端口信息
SOCKADDR_IN svrAddr;
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
svrAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
svrAddr.sin_port=htons(16000);
SOCKET svr;
//创建套接字
svr=socket(AF_INET,SOCK_DGRAM,0);
if(svr==INVALID_SOCKET)
{
ir=::WSAGetLastError();
cout<<"创建套接字失败!错误代码:"<<ir<<endl;
goto EXIT;
}
//绑定
ir=::bind(svr,(SOCKADDR*)&svrAddr,sizeof(SOCKADDR_IN));
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"绑定失败!错误代码:"<<ir<<endl;
}
else
{
while(TRUE)
{
SOCKADDR_IN addrClient;
char buf[1024];
memset(buf,0,1024);
int size=sizeof(addrClient);
memset(&addrClient,0,size);
//读取数据
ir=::recvfrom(svr,buf,1024,0,(sockaddr*)&addrClient,&size);
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"读取数据失败!错误代码:"<<ir<<endl;
}
else
{
cout<<"读取"<<ir<<"字节!"<<endl;
if(ir>0)
{
//把收到的数据原封不动发回去
::sendto(svr,buf,ir,0,(sockaddr*)&addrClient,size);
}
}
}
}
//关闭套接字并清理内存
EXIT: closesocket(svr);
ir=::WSACleanup();
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"反初始化Winsock失败!错误代码:"<<ir<<endl;
}
return 0;
} 展开
#include<WINSOCK2.H>
#include<iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
#define IP"127.0.0.1"
#define Port (16000)
int bind(
SOCKET s,
const struct sockaddr *name,
int namelen
);
int recvfrom(
SOCKET s,
char FAR*buf,
int len,
int flags,
struct sockaddr FAR *from,
int FAR *fromlen
);
int main()
{
WSADATA stWSAData;
int ir=::WSAStartup(MAKEWORD(2,0),&stWSAData);
if(ir!=0)
{
cout<<"初始化Winsock失败!错误代码:"<<ir<<endl;
return 0;
}
//设置地址,端口信息
SOCKADDR_IN svrAddr;
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
svrAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
svrAddr.sin_port=htons(16000);
SOCKET svr;
//创建套接字
svr=socket(AF_INET,SOCK_DGRAM,0);
if(svr==INVALID_SOCKET)
{
ir=::WSAGetLastError();
cout<<"创建套接字失败!错误代码:"<<ir<<endl;
goto EXIT;
}
//绑定
ir=::bind(svr,(SOCKADDR*)&svrAddr,sizeof(SOCKADDR_IN));
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"绑定失败!错误代码:"<<ir<<endl;
}
else
{
while(TRUE)
{
SOCKADDR_IN addrClient;
char buf[1024];
memset(buf,0,1024);
int size=sizeof(addrClient);
memset(&addrClient,0,size);
//读取数据
ir=::recvfrom(svr,buf,1024,0,(sockaddr*)&addrClient,&size);
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"读取数据失败!错误代码:"<<ir<<endl;
}
else
{
cout<<"读取"<<ir<<"字节!"<<endl;
if(ir>0)
{
//把收到的数据原封不动发回去
::sendto(svr,buf,ir,0,(sockaddr*)&addrClient,size);
}
}
}
}
//关闭套接字并清理内存
EXIT: closesocket(svr);
ir=::WSACleanup();
if(ir==SOCKET_ERROR)
{
ir=::WSAGetLastError();
cout<<"反初始化Winsock失败!错误代码:"<<ir<<endl;
}
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询