#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");
}