UDP实现类似qq聊天功能代码(利用多线程可完全实现,这里不是)

#server

#include<WinSock.h>
#include<iostream>
#include<string>
#pragma comment(lib,"ws2_32.lib")


using namespace std;
void Rec(SOCKET recvd,SOCKADDR_IN &add,int len)
{
char recBuf[512];
int byte=0;
memset((void *)recBuf,'\0',512);
while(1&&!(recBuf[0]=='e'&&recBuf[1]=='n'&&recBuf[2]=='d'))
{
byte= recvfrom(recvd,recBuf,300,0,(SOCKADDR *)&add,&len);
if(byte<=0)
break;
printf("%s\n",recBuf);
};
}
//客户端发送到服务端;前面是发送端
void Send(SOCKET send,SOCKADDR_IN readd)
{
string sendBuf="";
int byte=0;
while(1&&sendBuf!="end")
{
  cin>>sendBuf;
byte= sendto(send,sendBuf.c_str(),sendBuf.length()+1,0,(SOCKADDR*)&readd,sizeof(SOCKADDR));
if(byte<=0)
break;
Sleep(1000);
};
}
int main()
{
//启动socket
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1,1);
int err;
err = WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
return 1;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!= 1)
{
WSACleanup();
return 1;
}
//创建套接字
SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr =htonl(INADDR_ANY);//监听有没有人发消息过来
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);


//server端a
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
SOCKADDR_IN addrClient;
addrClient.sin_addr.S_un.S_addr =htonl(INADDR_ANY);//监听有没有人发消息过来
addrClient.sin_family = AF_INET;
addrClient.sin_port = htons(6000);


int len = sizeof(SOCKADDR);
char recvBuffer[300];
memset((void *)recvBuffer,'\0',300);
cout <<"Welcome,The Host is running "<<endl;
//接受
while(1){
cout<<"对方说:"<<endl;
Rec(sockSrv,addrClient,len);
cout <<"当输入“结束”后,发送数据结束"<<endl;
Send(sockSrv,addrClient);
}
closesocket(sockSrv);
WSACleanup();
system("pause");

}

#client

#include<WinSock.h>
#include<iostream>
#include<Windows.h>
#include<string>
#pragma comment(lib,"ws2_32.lib")


using namespace std;
//客户端接受数据,前面是收放,后面是发方地址
void Rec(SOCKET recvd,SOCKADDR_IN &add,int len)
{
char recBuf[512];
int byte=0;
memset((void *)recBuf,'\0',512);
while(1&&!(recBuf[0]=='e'&&recBuf[1]=='n'&&recBuf[2]=='d'))
{
byte= recvfrom(recvd,recBuf,300,0,(SOCKADDR *)&add,&len);
if(byte<=0)
break;
printf("%s\n",recBuf);
};
}
//客户端发送到服务端;前面是发送端
void Send(SOCKET send,SOCKADDR_IN readd)
{
string sendBuf="";
int byte=0;
while(1&&sendBuf!="end")
{
  cin>>sendBuf;
byte= sendto(send,sendBuf.c_str(),sendBuf.length()+1,0,(SOCKADDR*)&readd,sizeof(SOCKADDR));
if(byte<=0)
break;
Sleep(1000);
};
}
int main()
{
//启动socket
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1,1);
int err;
err = WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
return 1;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!= 1)
{
WSACleanup();
return 1;
}
//创建套接字
SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr =inet_addr("127.0.0.1");//监听有没有人发消息过来
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
while(1){
//client端
cout<<"当输入“end”后,完成数据床送"<<endl;
Send(sockClient,addrSrv);


//
char recvBuffer[300];
memset((void *)recvBuffer,'\0',300);
int len=sizeof(SOCKADDR);
cout<<"对方说:"<<endl;
Rec(sockClient,addrSrv,len);
}
WSACleanup();
system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值