目录
前言
1 实验题目
2 实验目的
3 实验内容
3.1 客户端
3.1.1 步骤
3.1.2 关键代码
3.2 服务器
3.2.1 步骤
3.2.2 关键代码
4 实验结果与分析
5 代码
5.1 客户端
5.2 服务器
前言
本实验为计算机网络课程设计内容,基本上所有代码都是根据指导书给的附录写出来的。有些实验需要实现图形界面,但是出于期末考试压力,我所有实验均是在控制台输入输出的,没有花额外时间去学习qt了,有精力的同学可以自学一下qt实现简单的图形界面。同时,该博客内容为部分报告内容,仅为大家提供参考,请勿直接抄袭。另外,本次实验所用平台是dev c++5.11
1 实验题目
实验一 网络聊天程序的设计与实现
2 实验目的
使用 WinSock API 的编程,了解 Socket 通信的原理,在此基础上编写一个聊天程序。
3 实验内容
3.1 客户端
3.1.1 步骤
(1)客户端建立连接后接收服务器分配的通信套接字。
(2)开启线程处理从服务器发送过来的数据。
(3)不断循环,接收用户输入,发送数据到服务器。
3.1.2 关键代码
(1)接收服务器发送的数据的线程函数
void handleRecvData(SOCKET client) {while(1) {memset(recvBuf,'\0',strlen(recvBuf));recvCode= recv(client,recvBuf,sizeof(recvBuf),0);if(recvCode==-1) {cout<<"接收服务器数据异常!\n";closesocket(client);WSACleanup();break;}if(!flag)cout<<"\n";cout<<recvBuf<<"\n"<<"客户端"<<name<<">";flag=0;}
}
(2)main函数
int main() {// 接收服务器分配的通信套接字err = recv(sockClient, name, sizeof(name), 0); // 开启线程处理从服务器发送过来的数据thread t(handleRecvData, sockClient);t.detach();// 不断循环,接收用户输入,发送数据到服务器while (1) {memset(sendBuf,'\0',strlen(sendBuf));cin>>sendBuf;flag=1;if(!strcmp(sendBuf,"quit")) {send(sockClient,"quit",strlen("quit")+1,0);cout<<"通知服务器客户端已关闭连接!\n";closesocket(sockClient);WSACleanup();break;}err = send(sockClient,sendBuf,strlen(sendBuf)+1,0);if(err == -1){cout<<"客户端发送数据失败!\n";break;} }
}
3.2 服务器
3.2.1 步骤
(1)服务器端初始化,绑定地址并开始监听客户端连接请求。
(2)接受客户端连接请求,为每个客户端分配套接字,并加入客户端列表。
(3)开启线程处理客户端发来的数据,同时遍历客户端列表,将接收到的信息发送给其他客户端。
3.2.2 关键代码
(1)处理客户端主要功能代码
void handleClient(SOCKET client) {while(1) {memset(recvBuf,'\0',strlen(recvBuf));memset(sendBuf,'\0',strlen(sendBuf));recvCode= recv(client,recvBuf,sizeof(recvBuf),0);cout<<"recvCode :"<<recvCode<<"\n";if(recvCode==-1||!strcmp(recvBuf,"quit")) {//删除断开连接的客户端cout<<"客户端"<<client<<"已关闭连接!\n";
clientList.erase(remove(clientList.begin(),clientList.end(),client));closesocket(client);cout<<"删除断开连接的 client 后,clientList 包含 client 的个数:"<<clientList.size()<<"\n";break;} else {cout<<"clientList 包含 client 的个数:"<<clientList.size()<<"\n";}cout<<"接收到客户端"<<client<<"的数据:"<<recvBuf<<"\n";for(int i=0; i<clientList.size(); i++) {sprintf(sendBuf,"群聊信息---客户端%d>%s",client,recvBuf);sendCode = send(clientList[i],sendBuf,strlen(sendBuf)+1,0);cout<<"服务器发送数据:\""<<sendBuf<<"\",给客户端"<<clientList[i]<<",sendCode :"<<sendCode<<"\n";}}
}
(2)main函数
int main() {
// 循环接受客户端连接请求,为每个客户端分配套接字,加入客户端列表并单独处理
while(1) {memset(id,'\0',strlen(id));cout<<"等待新客户端连接\n";SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);if(sockConn==-1) {cout<<"新客户端连接失败\n";closesocket(sockConn);closesocket(sockSrv);WSACleanup();} else {cout<<"新客户端连接成功\n";sprintf(id,"%d",sockConn);sendCode = send(sockConn,id,strlen(id)+1,0);cout<<"为新客户端分配套接字:"<<id<<", sendCode :"<<sendCode<<"\n";clientList.push_back(sockConn);thread t(handleClient,sockConn);t.detach();}}
4 实验结果与分析
(1)启动服务器等待客户端连接。
图1.1 启动服务器
(2)启动3个客户端加入聊天。
图1.2 启动客户端
(3)客户端280发送消息,可以在客户端252和260收到信息。
图1.3 客户端280发送信息
5 代码
5.1 客户端
#include <stdio.h>
#include <bits/stdc++.h>
#include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
#include<vector>
#include<thread>
#include <Winsock2.h>using namespace std;typedef long long LL;
const int maxn = 100;
const int maxRecv = 128;
const int maxSend = maxRecv + 20;char name[6]="\0";
int flag = 0;void handleRecvData(SOCKET client) {char recvBuf[maxRecv];int recvCode;while(1) {memset(recvBuf,'\0',strlen(recvBuf));recvCode= recv(client,recvBuf,sizeof(recvBuf),0);
// cout<<"recvCode :"<<recvCode<<"\n";if(recvCode==-1) {cout<<"接收服务器数据异常!\n";closesocket(client);//shutdown(client,2);WSACleanup();break;}
// cout<<"接收的的数据:"<<recvBuf;if(!flag)cout<<"\n";cout<<recvBuf;cout<<"\n";flag=0;
// if(flag) {
// flag=0;
// }
// if(!flag) {cout<<"客户端"<<name<<">";
// cout<<"\n";
// }
// cout<<"\n请输入要发送数据:\n";}
}int main() {WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested = MAKEWORD( 1, 1 );err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) {return 0;}if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) {WSACleanup();return 0;}SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);if(sockClient==-1) {cout<<"创建客户端失败"<<"\n";closesocket(sockClient);WSACleanup();return 0;}cout<<"创建客户端成功"<<"\n";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);err = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));if(err==-1) {closesocket(sockClient);WSACleanup();cout<<"连接服务器失败"<<"\n";}cout<<"连接服务器成功\n";err = recv(sockClient,name,sizeof(name),0);if(err==-1) {closesocket(sockClient);WSACleanup();cout<<"获取服务器分配的通信套接字失败"<<"\n";}cout<<"服务器分配的通信套接字:"<<name<<"\n";cout<<"-------------------------------------------\n";//开启线程处理从服务器发送过来的数据thread t(handleRecvData,sockClient);t.detach();char sendBuf[maxSend]="\0";cout<<"客户端"<<name<<">";while(1) {memset(sendBuf,'\0',strlen(sendBuf));
// cout<<"请输入要发送数据:\n";
// cout<<"客户端"<<name<<">";cin>>sendBuf;flag=1;if(!strcmp(sendBuf,"quit")) {send(sockClient,"quit",strlen("quit")+1,0);cout<<"通知服务器客户端已关闭连接!\n";closesocket(sockClient);WSACleanup();break;}err = send(sockClient,sendBuf,strlen(sendBuf)+1,0);
// cout<<"sendCode :"<<err<<"\n"; if(err == -1){cout<<"客户端发送数据失败!\n";
// closesocket(sockClient);
// WSACleanup();break;} //while(flag==1);
// cout<<"客户端"<<name<<"发送数据:"<<sendBuf<<"\n";
// char recvBuf[50]="\0";
// cout<<recv(sockClient,recvBuf,50,0)<<"\n";
// cout<<"接收到服务器数据:";
// printf("%s\n",recvBuf);//cout<<closesocket(sockClient)<<"\n";//WSACleanup();//cout<<connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR))<<"\n";//cout<<"服务器连接成功\n";}closesocket(sockClient);WSACleanup();return 0;
}
5.2 服务器
#include <stdio.h>
#include <bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<thread>
#include <Winsock2.h>
#pragma comment(lib,"ws2_32")
using namespace std;typedef long long LL;
const int maxn = 100;
const int maxRecv = 128;
const int maxSend = maxRecv + 20;//SOCKET clientList[maxn];
vector<SOCKET> clientList;void handleClient(SOCKET client) {char recvBuf[maxRecv]="\0";char sendBuf[maxSend]="\0";int recvCode;int sendCode;while(1) {memset(recvBuf,'\0',strlen(recvBuf));memset(sendBuf,'\0',strlen(sendBuf));recvCode= recv(client,recvBuf,sizeof(recvBuf),0);cout<<"recvCode :"<<recvCode<<"\n";if(recvCode==-1||!strcmp(recvBuf,"quit")) {//删除断开连接的客户端cout<<"客户端"<<client<<"已关闭连接!\n";clientList.erase(remove(clientList.begin(),clientList.end(),client));closesocket(client);//shutdown(client,2);cout<<"删除断开连接的 client 后,clientList 包含 client 的个数:"<<clientList.size()<<"\n";break;} else {cout<<"clientList 包含 client 的个数:"<<clientList.size()<<"\n";}cout<<"接收到客户端"<<client<<"的数据:"<<recvBuf<<"\n";
// printf("%s\n",recvBuf);for(int i=0; i<clientList.size(); i++) {
// if(clientList[i]==client) {
// cout<<"跳过 client:"<<client<<"\n";
// continue;
// }sprintf(sendBuf,"群聊信息---客户端%d>%s",client,recvBuf);sendCode = send(clientList[i],sendBuf,strlen(sendBuf)+1,0);
// sendCode = send(clientList[i],recvBuf,strlen(recvBuf)+1,0);cout<<"服务器发送数据:\""<<sendBuf<<"\",给客户端"<<clientList[i]<<",sendCode :"<<sendCode<<"\n";
// cout<<"sendCode :"<<sendCode<<"\n";}}
}
int main() {WORD wVersionRequested;WSADATA wsaData;int err;
// vector<SOCKET> clientList;wVersionRequested = MAKEWORD( 1, 1 );err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) {cout<<"初始化套接字库失败"<<"\n";return 0;}cout<<"初始化套接字库成功"<<"\n";if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) {WSACleanup();cout<<"确定协议失败"<<"\n";return 0;}cout<<"确定协议成功"<<"\n";SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);if(sockSrv==-1) {closesocket(sockSrv);WSACleanup();cout<<"创建服务器失败"<<"\n";return 0;}cout<<"创建服务器成功"<<"\n";cout<<"sockSrv :"<<sockSrv<<"\n";SOCKADDR_IN addrSrv;//htonl函数把主机字节转化成网络字节的函数;u_long htonl(u_long hostlong);addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);addrSrv.sin_family=AF_INET;addrSrv.sin_port=htons(6000);//绑定err = bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));if(err==-1) {closesocket(sockSrv);WSACleanup();cout<<"服务器绑定失败"<<"\n";return 0;}cout<<"服务器绑定成功"<<"\n";err = listen(sockSrv,5);if(err==-1) {closesocket(sockSrv);WSACleanup();cout<<"监听失败"<<"\n";return 0;}cout<<"监听成功"<<"\n";cout<<"-------------------------------------------\n";SOCKADDR_IN addrClient;int len=sizeof(SOCKADDR);char id[6]="\0";int sendCode;while(1) {memset(id,'\0',strlen(id));cout<<"等待新客户端连接\n";SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);if(sockConn==-1) {cout<<"新客户端连接失败\n";closesocket(sockConn);closesocket(sockSrv);WSACleanup();} else {cout<<"新客户端连接成功\n";
// cout<<"sockConn :"<<sockConn<<"\n";sprintf(id,"%d",sockConn);
// cout<<"分配给新客户端的套接字:"<<id<<"\n";sendCode = send(sockConn,id,strlen(id)+1,0);cout<<"为新客户端分配套接字:"<<id<<", sendCode :"<<sendCode<<"\n";clientList.push_back(sockConn);thread t(handleClient,sockConn);t.detach();}
// char sendBuf[50];cout<<"请输入要发送数据:\n";cin>>sendBuf;
// sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
// cout<<send(sockConn,sendBuf,strlen(sendBuf)+1,0)<<"\n";
// cout<<"服务器发送数据:"<<sendBuf<<"\n";
// char recvBuf[50];
// cout<<recv(sockConn,recvBuf,50,0)<<"\n";
// if(!strcmp(recvBuf,"quit")) {
// cout<<"客户端已关闭连接!";
// break;
// } else {
// cout<<"接收到客户端数据:";
// printf("%s\n",recvBuf);
// }
// cout<<closesocket(sockConn)<<"\n";//cout<<"服务器关闭连接\n";}//closesocket(sockConn);WSACleanup();return 0;
}