• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Winsock套接字开发网络聊天室(C/S)模式

武飞扬头像
陈丹宇jmu
帮助1

聊天室的基本要求

聊天器采用客户端/服务器(C/S)模式;
1,客户端利用UDP与服务器连接,客户端与客户端之间通过UDP互相通讯;
2,服务器端具有服务器端口设置,维护客户端个人信息,记录客户端状态,分配账号等功能
     客户端具有服务器地址及端口设置,用户注册,用户登陆,添加好友和删除好友,查看好友信       息,给好友发送消息等功能;
3,服务器与客户端间、客户端之间的交互采用控制台方式或GUI窗口方式均可;


聊天器实例的实现情况

1,程序完成了基于客户端/服务器(C/S)模式的设计目标,模拟出类似于QQ聊天室的应用方式,并实现其相关的基本功能。
2,程序完成了基于UDP的设计目标,实现了客户端与服务器,客户端与客户端之间通过UDP互相通讯的设计模式。
3,服务器端实现了具有服务器端口设置,维护客户端个人信息,记录客户端状态等应用功能;客户端实现了具有服务器地址及端口设置,用户注册,用户登陆,给好友发送消息,实现多个用户之间群聊等应用功能。
4,不足之处在于服务器与客户端间、客户端之间采用控制台方式实现了相互交互,未实现基于GUI窗口开发的方式。


运行注意事项(看完再去运行蛤)

    这个基于winsock的UDP网络聊天器是本人的期末大作业的内容,因为自己知道做一个课程设计对于很多同学来说难度都太大了,为了让大家能够在更短的时间内有一份可以完整提交的项目以及答辩顺利,所以我无偿的发布出我的大作业供大家学习使用。

项目基于的环境

  • windows操作系统
  • Visual studio 2022 编译器
  • X86(debug)运行架构

网上的项目繁多,所以需要找到适合自己的去学习使用,比如这门课对于苹果电脑的同学就不是很友好。

项目运行须知

同绝大多数的通信实例一样,网络编程对于网络环境的环境&介质要求很高,所以在运行项目之前,请确保运行项目的主机网络的畅通,关闭windows系统的防火墙,连上自己的手机热点做局域网测试效果为佳。

还有要清楚项目的运行逻辑,基于C/S模型的程序实例,一定要记得要先运行服务器,然后在运行客户端,客户端才可以正常访问服务器,并且要注意服务器和客户端的IP与端口信息,这个对于网络通信是非常重要的,很多bug往往不是代码本身的问题,而是出现在了网络配置的问题上,所以如果你是第一在电脑上运行项目,那么第一次就要做好程序在主机上的网络配置,找到代码里有关于网络配置的相关定义进行修改哦。

最后,在用编辑器调试程序运到报错运行不起来时,要学会复制所报出的问题,在百度上搜索问题的答案,这是基本功。


部分实例演示

服务器的运行界面

学新通

客户端的运行界面

学新通

用户上线提醒

学新通

用户注册提醒

学新通

 私聊功能的实现

学新通

 学新通

群聊功能的实现

学新通

 聊天室的整体设计 

该程序采用经典的c/s架构,即采用客户端/服务器架构。模型的设计包括三个主要部分,即文件存储部分,服务器部分,以及客户端部分。

如图3.1.1所示,其中,服务器的功能为接收发送器的消息请求,并根据消息类型进行不同的响应处理;服务器中通过文件存储用户的用户名和密码。

客户端的功能实现包括两个部分:发送器和接收器。其中发送器所实现的功能包括注册新账号,登录已有账号,发送群聊消息和私聊消息等;接收器主要实现的功能包括接收服务器转发的群聊消息和私聊消息,并将其显示在显示屏上。

学新通

    服务器要处理的消息类型一共有五种,分别是登录请求、注册请求、群聊消息、私聊消息、退出命令。这五种消息类型,设计使用字符串的第一个字符来进行区分,比如’L‘是Login的首字母,用来作为登录请求的标志,’R‘是Rigister的首字母,用来作为注册请求的标志,’G‘是Group的首字母,用来作为群聊消息的标志,’P'是Personal的首字母,用来作为私聊消息的标志,最后字符串"exit"可以作为用户退出的命令。

学新通

 服务器要处理的五种消息类型对应着聊天器所要实现的五种功能,即登录功能,注册功能,群聊功能,私聊功能,关闭程序。下文将针对聊天器所要实现的功能给出设计的方案与实现。

 聊天室服务器完整代码

  1.  
    #include<WinSock2.h>
  2.  
    #include<iostream>
  3.  
    #include<fstream>
  4.  
    #include<vector>
  5.  
    #include<string>
  6.  
    #include<cstdlib>
  7.  
    #pragma comment(lib,"ws2_32.lib")
  8.  
    using namespace std;
  9.  
     
  10.  
    #define DEFAULT_PORT 5055
  11.  
    #define BUFFER_LENGTH 1024
  12.  
    class user
  13.  
    {
  14.  
    public:
  15.  
    user(string username, string ip, int sender_port, int receiver_port)
  16.  
    {
  17.  
    this->username = username;
  18.  
    this->ip = ip;
  19.  
    this->sender_port = sender_port;
  20.  
    this->receiver_port = receiver_port;
  21.  
     
  22.  
    //设置接收器的地址
  23.  
    receiver.sin_family = AF_INET;
  24.  
    receiver.sin_port = htons(receiver_port);
  25.  
    char *addr = new char[ip.length() 1];
  26.  
    strcpy(addr, ip.c_str());
  27.  
    receiver.sin_addr.s_addr = inet_addr(addr);
  28.  
    }
  29.  
    string username; //用户名
  30.  
    string ip; //客户端ip地址
  31.  
    int sender_port; //发送器端口
  32.  
    int receiver_port; //接收器端口
  33.  
    struct sockaddr_in receiver; //存储接收器的地址
  34.  
    };
  35.  
    class server
  36.  
    {
  37.  
    public:
  38.  
    bool Startup(); //检测是否满足服务器运行的环境
  39.  
    bool SetServerSocket(); //设置服务器用来监听信息的socket套接字
  40.  
    bool Checktxt(); //检测存储文件是否存在,若不存在,创建一个
  41.  
    void work(); //服务器运行的主函数
  42.  
    void SendMessage(string message, struct sockaddr_in x); //发送信息的函数
  43.  
    void Sendonlinelist(); //向客户端发送好友在线列表
  44.  
    bool TestUsernameAndPassword(string username, string password, int &flag); //测试用户名和密码是否正确
  45.  
    bool TestDuplicateLogin(string username); //测试是否重复登录
  46.  
    bool TestDuplicateRigister(string username); //测试是否重复注册
  47.  
    string Getusername(string ip, int port); //根据ip和端口号获得用户名
  48.  
    int Getuserindex(string username); //根据用户名获得用户在在线用户表的索引号
  49.  
    void extractLoginuserinfor(string userinfor, string &username, string &password, string &receiverport); //提取登录请求中的用户名密码和显示器端口号
  50.  
    void extractRegisteruserinfor(string userinfor, string&username, string&password); //提取注册请求中的用户名和密码
  51.  
    void extactPersonalMessageReceivername(string &message, string &receivername); //提取私聊消息中的接收者的姓名
  52.  
     
  53.  
    private:
  54.  
    WSADATA wsaData;
  55.  
    SOCKET sSocket; //用来接收消息的套接字
  56.  
    struct sockaddr_in ser; //服务器地址
  57.  
    struct sockaddr_in cli; //客户地址
  58.  
    int cli_length = sizeof(cli); //客户地址长度
  59.  
    char recv_buf[BUFFER_LENGTH]; //接收数据的缓冲区
  60.  
    vector<user> usertable; //在线用户表
  61.  
    string sendmessage, printmessage; //存储服务器转发、打印用的字符串
  62.  
    int iSend, iRecv; //存储服务器发送和接收的字符串的长度
  63.  
    };
  64.  
     
  65.  
    bool server::Startup()
  66.  
    {
  67.  
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  68.  
    {
  69.  
    cout << "Failed to load Winsock." << endl;
  70.  
    return false;
  71.  
    }
  72.  
    return true;
  73.  
    }
  74.  
    bool server::SetServerSocket()
  75.  
    {
  76.  
    //产生服务器端套接口
  77.  
    sSocket = socket(AF_INET, SOCK_DGRAM, 0);
  78.  
    if (sSocket == INVALID_SOCKET)
  79.  
    {
  80.  
    cout << "socket()Failed:" << WSAGetLastError() << endl;
  81.  
    return false;
  82.  
    }
  83.  
    //建立服务器端地址
  84.  
    ser.sin_family = AF_INET;
  85.  
    ser.sin_port = htons(DEFAULT_PORT); //htons()函数把一个双字节主机字节顺序的数转换为网络字节顺序的数
  86.  
    ser.sin_addr.s_addr = htonl(INADDR_ANY); //htonl()函数把一个主机字节顺序的数转换为网络字节顺序的数
  87.  
    if (bind(sSocket, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
  88.  
    {
  89.  
    cout << "bind()Failed:" << WSAGetLastError() << endl;
  90.  
    return false;
  91.  
    }
  92.  
    return true;
  93.  
    }
  94.  
    void server::SendMessage(string message, struct sockaddr_in x)
  95.  
    {
  96.  
    char *send_buf = new char[message.length() 1];
  97.  
    strcpy(send_buf, message.c_str());
  98.  
    SOCKET rSocket = socket(AF_INET, SOCK_DGRAM, 0);
  99.  
    if (rSocket == INVALID_SOCKET)
  100.  
    {
  101.  
    cout << "socket()Failed:" << WSAGetLastError() << endl;
  102.  
    return;
  103.  
    }
  104.  
    iSend = sendto(rSocket, send_buf, message.length() 1, 0, (SOCKADDR*)&(x), sizeof(x));
  105.  
    if (iSend == SOCKET_ERROR)
  106.  
    {
  107.  
    cout << "sendto failed:" << WSAGetLastError() << endl;
  108.  
    closesocket(rSocket);
  109.  
    return;
  110.  
    }
  111.  
    closesocket(rSocket);
  112.  
    }
  113.  
    void server::Sendonlinelist()
  114.  
    {
  115.  
    string onlinelist;
  116.  
    for (int i = 0; i < usertable.size(); i )
  117.  
    onlinelist = onlinelist usertable[i].username "#";
  118.  
    onlinelist = onlinelist "$"; //结束标志
  119.  
    SendMessage(onlinelist, cli);
  120.  
    }
  121.  
    bool server::TestUsernameAndPassword(string username, string password, int &flag)
  122.  
    {
  123.  
    if (!Checktxt())
  124.  
    {
  125.  
    cout << "无法找到存储文件." << endl << endl;
  126.  
    flag = 0; //未找到用户名的标志
  127.  
    return false;
  128.  
    }
  129.  
    fstream in("C:\\userform\\userform.txt");
  130.  
    string line;
  131.  
    string username_txt, password_txt;
  132.  
    while (getline(in, line))
  133.  
    {
  134.  
    for (int i = 0; i < line.size(); i )
  135.  
    {
  136.  
    if (line[i] == '#')
  137.  
    {
  138.  
    username_txt = line.substr(0, i);
  139.  
    password_txt = line.substr(i 1);
  140.  
    break;
  141.  
    }
  142.  
    }
  143.  
    if (username_txt == username) //该用户名存在
  144.  
    {
  145.  
    if (password == password_txt) //且密码正确
  146.  
    {
  147.  
    in.close();
  148.  
    return true; //返回验证成功
  149.  
    }
  150.  
    cout << "用户" << username << "登录密码错误" << endl << endl; //返回密码错误的信息
  151.  
    flag = 1; //密码错误的标志
  152.  
    return false;
  153.  
    }
  154.  
    }
  155.  
    in.close();
  156.  
    cout << "未注册过的用户:" << username << endl << endl;
  157.  
    flag = 0; //未找到用户名的标志
  158.  
    return false;
  159.  
    }
  160.  
    bool server::TestDuplicateLogin(string username)
  161.  
    {
  162.  
    int i;
  163.  
    for (i = 0; i < usertable.size(); i )
  164.  
    if (usertable[i].username == username) break;
  165.  
    if (i == usertable.size()) //该用户还没有登录过
  166.  
    return false;
  167.  
    else
  168.  
    {
  169.  
    cout << "用户" << username << "重复登录" << endl;
  170.  
    return true;
  171.  
    }
  172.  
    }
  173.  
    bool server::TestDuplicateRigister(string username)
  174.  
    {
  175.  
    if (!Checktxt())
  176.  
    {
  177.  
    cout << "无法找到存储文件." << endl << endl;
  178.  
    return true;
  179.  
    }
  180.  
    fstream in("C:\\userform\\userform.txt");
  181.  
    string line;
  182.  
    while (getline(in, line))
  183.  
    {
  184.  
    string username_txt;
  185.  
    for (int i = 0; i < line.size(); i )
  186.  
    {
  187.  
    if (line[i] == '#')
  188.  
    {
  189.  
    username_txt = line.substr(0, i); //提取用户名
  190.  
    if (username_txt == username) //对比,相等则表明已经注册过
  191.  
    {
  192.  
    in.close();
  193.  
    cout << "用户名" << username << "重复注册" << endl << endl;
  194.  
    return true;
  195.  
    }
  196.  
    break; //否则继续对比下一个用户名
  197.  
    }
  198.  
    }
  199.  
    }
  200.  
    in.close();
  201.  
    return false; //代码执行到这说明该用户名还没有注册过
  202.  
    }
  203.  
    string server::Getusername(string ip, int port)
  204.  
    {
  205.  
    for (int i = 0; i < usertable.size(); i )
  206.  
    if (usertable[i].ip == ip&&usertable[i].sender_port == port)
  207.  
    return usertable[i].username;
  208.  
    cout << "非法的用户连接上服务器" << endl;
  209.  
    cout << "ip地址为:" << ip << endl << "端口号为:" << port << endl;
  210.  
    return "";
  211.  
    }
  212.  
    int server::Getuserindex(string username)
  213.  
    {
  214.  
    int i = 0;
  215.  
    for (i = 0; i < usertable.size(); i )
  216.  
    if (usertable[i].username == username) break;
  217.  
    return i;
  218.  
    }
  219.  
    void server::extractLoginuserinfor(string userinfor, string &username, string &password, string &receiverport)
  220.  
    {
  221.  
    int i;
  222.  
    for (i = 0; i < userinfor.length(); i ) //提取用户名
  223.  
    {
  224.  
    if (userinfor[i] == '#')
  225.  
    {
  226.  
    username = userinfor.substr(0, i);
  227.  
    break;
  228.  
    }
  229.  
    }
  230.  
    for (int j = i 1; j < userinfor.length(); j ) //提取密码和显示器端口号
  231.  
    {
  232.  
    if (userinfor[j] == '#')
  233.  
    {
  234.  
    password = userinfor.substr(i 1, j - i - 1);
  235.  
    receiverport = userinfor.substr(j 1);
  236.  
    break;
  237.  
    }
  238.  
    }
  239.  
    }
  240.  
    void server::extractRegisteruserinfor(string userinfor, string&username, string&password)
  241.  
    {
  242.  
    for (int i = 0; i < userinfor.size(); i )
  243.  
    {
  244.  
    if (userinfor[i] == '#')
  245.  
    {
  246.  
    username = userinfor.substr(0, i);
  247.  
    password = userinfor.substr(i 1);
  248.  
    break;
  249.  
    }
  250.  
    }
  251.  
    }
  252.  
    void server::extactPersonalMessageReceivername(string &message, string &receivername)
  253.  
    {
  254.  
    for (int i = 0; i < message.size(); i )
  255.  
    {
  256.  
    if (message[i] == '#')
  257.  
    {
  258.  
    receivername = message.substr(0, i);
  259.  
    message = message.substr(i 1);
  260.  
    break;
  261.  
    }
  262.  
    }
  263.  
    }
  264.  
    bool server::Checktxt()
  265.  
    {
  266.  
    FILE *fp = fopen("C:\\userform\\userform.txt", "r");
  267.  
    if (fp == NULL)
  268.  
    {
  269.  
    system("md C:\\userform");
  270.  
    ofstream out("C:\\userform\\userform.txt");
  271.  
    if (!out)
  272.  
    return false;
  273.  
    out.close();
  274.  
    return true;
  275.  
    }
  276.  
    return true;
  277.  
    }
  278.  
    void server::work()
  279.  
    {
  280.  
    cout << "-----------------" << endl;
  281.  
    cout << "Server running" << endl;
  282.  
    cout << "-----------------" << endl;
  283.  
    while (true) //进入一个无限循环,进行数据接收和发送
  284.  
    {
  285.  
    memset(recv_buf, 0, sizeof(recv_buf)); //初始化接收缓冲区
  286.  
    iRecv = recvfrom(sSocket, recv_buf, BUFFER_LENGTH, 0, (struct sockaddr*)&cli, &cli_length);
  287.  
    if (iRecv == SOCKET_ERROR)
  288.  
    {
  289.  
    cout << "recvfrom()Failed:" << WSAGetLastError() << endl;
  290.  
    continue;
  291.  
    }
  292.  
     
  293.  
    //获取发送方的地址(ip和端口)
  294.  
    char *x = inet_ntoa(cli.sin_addr); string address(x); //获取客户端ip
  295.  
    int userport = ntohs(cli.sin_port); //获取客户端端口
  296.  
     
  297.  
    string infortype = string(recv_buf); //根据infortype[0]来判断消息的类型
  298.  
    if (infortype[0] == 'L') //登录请求
  299.  
    {
  300.  
    string userinfor = infortype.substr(1); //除去消息类型
  301.  
    string username, password, receiver_port;
  302.  
    extractLoginuserinfor(userinfor, username, password, receiver_port); //提取用户名和密码
  303.  
    //向不合法用户发送登录失败的回应
  304.  
    int flag = 0;
  305.  
    if (!TestUsernameAndPassword(username, password, flag))
  306.  
    {
  307.  
    if (flag == 0)
  308.  
    SendMessage("0", cli);
  309.  
    if (flag == 1)
  310.  
    SendMessage("1", cli);
  311.  
    continue;
  312.  
    }
  313.  
    //查询该用户是否重复登录
  314.  
    if (TestDuplicateLogin(username))
  315.  
    {
  316.  
    SendMessage("2", cli);
  317.  
    continue;
  318.  
    }
  319.  
    //将合法的未登录的用户加入列表
  320.  
    int receiver_port_int = atoi(receiver_port.c_str());
  321.  
    user newuser(username, address, userport, receiver_port_int);
  322.  
    usertable.push_back(newuser);
  323.  
     
  324.  
    printmessage = "(上线消息)" newuser.username "已上线"; //设置要打印的消息
  325.  
    sendmessage = printmessage; //设置要转发的消息
  326.  
    SendMessage("Y", cli); //向客户端发送登录成功的回应
  327.  
    }
  328.  
    else if (infortype[0] == 'R') //注册信息
  329.  
    {
  330.  
    string userinfor = infortype.substr(1); //除去消息类型
  331.  
    string username, password;
  332.  
    extractRegisteruserinfor(userinfor, username, password); //提取用户名和密码
  333.  
     
  334.  
    //检测用户名是否已经注册过
  335.  
    if (TestDuplicateRigister(username))
  336.  
    {
  337.  
    SendMessage("N", cli);
  338.  
    continue;
  339.  
    }
  340.  
    //向文件写入新注册的用户名和密码
  341.  
    if (!Checktxt())
  342.  
    {
  343.  
    SendMessage("N", cli);
  344.  
    continue;
  345.  
    }
  346.  
    fstream out("C:\\userform\\userform.txt", ios::app);
  347.  
    out << userinfor << endl;
  348.  
    out.close();
  349.  
    //发送注册成功的回应
  350.  
    SendMessage("Y", cli);
  351.  
    cout << "注册成功" << endl << "新用户名为:" << username << endl << endl;
  352.  
    continue;
  353.  
    }
  354.  
    else if (infortype[0] == 'G') //群聊消息
  355.  
    {
  356.  
    string message = infortype.substr(1);
  357.  
    string sendername = Getusername(address, userport); //获取发送者姓名
  358.  
    if (sendername == "") continue;
  359.  
    printmessage = "(群消息)" sendername ":" message; //设置要打印的消息
  360.  
    sendmessage = printmessage;
  361.  
    //sendmessage = "G#" sendername ":" message; //设置要转发的消息
  362.  
    }
  363.  
    else if (infortype[0] == 'P') //私聊消息
  364.  
    {
  365.  
    if (infortype[1] == 'L') //获取在线好友列表的请求
  366.  
    {
  367.  
    Sendonlinelist();
  368.  
    continue;
  369.  
    }
  370.  
    if (infortype[1] == 'M') //私聊消息
  371.  
    {
  372.  
    string message = infortype.substr(2);
  373.  
    string sendername = Getusername(address, userport); //提取发送者姓名
  374.  
    if (sendername == "") continue;
  375.  
    //提取接收者姓名
  376.  
    string receivername;
  377.  
    extactPersonalMessageReceivername(message, receivername);
  378.  
    //检查接收者是否离线
  379.  
    int i = Getuserindex(receivername);
  380.  
    if (i == usertable.size()) //接收者已经离线
  381.  
    {
  382.  
    SendMessage("N", cli);
  383.  
    continue;
  384.  
    }
  385.  
    SendMessage("Y", cli); //向发送方发送成功的响应
  386.  
    printmessage = "(私消息)" sendername "->" receivername ":" message; //设置要打印的消息
  387.  
    cout << printmessage << endl;
  388.  
    cout << "用户ip:" << address << endl;
  389.  
    cout << "用户端口:" << userport << endl;
  390.  
    cout << "当前在线人数:" << usertable.size() << endl << endl;
  391.  
    sendmessage = printmessage; //设置要发送的消息
  392.  
    SendMessage(sendmessage, usertable[i].receiver);
  393.  
    if (sendername != receivername)
  394.  
    {
  395.  
    int j = Getuserindex(sendername);
  396.  
    SendMessage(sendmessage, usertable[j].receiver);
  397.  
    }
  398.  
    continue;
  399.  
    }
  400.  
    }
  401.  
    else if (infortype == "exit")
  402.  
    {
  403.  
    string sendername = Getusername(address, userport);
  404.  
    if (sendername == "") continue;
  405.  
    int i = Getuserindex(sendername);
  406.  
    if (i >= usertable.size() || i < 0) continue;
  407.  
    SendMessage("exit", usertable[i].receiver); //向该用户显示器发送退出命令
  408.  
    usertable.erase(usertable.begin() i);
  409.  
    printmessage = "(下线消息)" sendername "已下线"; //设置要打印的消息
  410.  
    sendmessage = printmessage; //设置要转发的消息
  411.  
     
  412.  
    }
  413.  
    //在服务器上打印消息
  414.  
    cout << printmessage << endl;
  415.  
    cout << "用户ip:" << address << endl;
  416.  
    cout << "用户端口:" << userport << endl;
  417.  
    cout << "当前在线人数:" << usertable.size() << endl << endl;
  418.  
    //向客户端发送消息
  419.  
    for (int i = 0; i < usertable.size(); i )
  420.  
    SendMessage(sendmessage, usertable[i].receiver);
  421.  
    }
  422.  
     
  423.  
     
  424.  
    }
  425.  
     
  426.  
    int main()
  427.  
    {
  428.  
    server x;
  429.  
    if (x.Startup() == false)
  430.  
    return 0;
  431.  
    if (x.SetServerSocket() == false)
  432.  
    return 0;
  433.  
    x.work();
  434.  
    return 0;
  435.  
    }

客户端发送器的完整代码

  1.  
    #include<Winsock2.h>
  2.  
    #include<iostream>
  3.  
    #include<string>
  4.  
    #include<ctime>
  5.  
    #include<tchar.h>
  6.  
    #include<Windows.h>
  7.  
    #include<fstream>
  8.  
    #include<vector>
  9.  
    #include <sstream>
  10.  
    using namespace std;
  11.  
    #pragma comment(lib,"ws2_32.lib")
  12.  
     
  13.  
    #define DEFAULT_PORT 5055
  14.  
    #define DATA_BUFFER 1024
  15.  
     
  16.  
    class client
  17.  
    {
  18.  
    public:
  19.  
    bool Startup();
  20.  
    void SetServerAddress();
  21.  
    int GeneratePort(); //随机生成显示器端口号
  22.  
    bool Getonlinelist(); //获得在线的用户名
  23.  
    void work(); //发送器的主函数
  24.  
    private:
  25.  
    WSADATA wsaData;
  26.  
    SOCKET sClient; //发送信息和接收信息时使用的套接字
  27.  
    struct sockaddr_in ser; //保存服务器的地址
  28.  
    int ser_length = sizeof(ser);
  29.  
    struct sockaddr_in communication;
  30.  
    int communication_length = sizeof(communication);
  31.  
    char recv_buf[DATA_BUFFER]; //接收信息的缓冲区
  32.  
    int receiver_port; //显示器的端口号
  33.  
    vector<string> onlinelist; //保存在线用户的用户名
  34.  
    int iSend, iRecv;
  35.  
    };
  36.  
     
  37.  
    bool client::Startup()
  38.  
    {
  39.  
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  40.  
    {
  41.  
    cout << "Failed to load Winsock." << endl;
  42.  
    return false;
  43.  
    }
  44.  
    sClient = socket(AF_INET, SOCK_DGRAM, 0);
  45.  
    if (sClient == INVALID_SOCKET)
  46.  
    {
  47.  
    cout << "socket()Failed:" << WSAGetLastError() << endl;
  48.  
    return false;
  49.  
    }
  50.  
    return true;
  51.  
    }
  52.  
    void client::SetServerAddress()
  53.  
    {
  54.  
    cout << "请输入ip地址:";
  55.  
    string iptemp;
  56.  
    cin >> iptemp;
  57.  
    char *ip = new char[iptemp.length() 1];
  58.  
    strcpy(ip, iptemp.c_str());
  59.  
    //建立服务器端地址
  60.  
    ser.sin_family = AF_INET;
  61.  
    ser.sin_port = htons(DEFAULT_PORT);
  62.  
    ser.sin_addr.s_addr = inet_addr(ip);
  63.  
    }
  64.  
    int client::GeneratePort()
  65.  
    {
  66.  
    srand((unsigned)time(NULL));
  67.  
    int x = 1024 rand() % (5000 - 1024);
  68.  
    return x;
  69.  
    }
  70.  
    bool client::Getonlinelist() //向服务器请求获取好友在线列表
  71.  
    {
  72.  
    if (onlinelist.size() > 0)
  73.  
    onlinelist.clear();
  74.  
    char getonlinelist[3] = "PL";
  75.  
    iSend = sendto(sClient, getonlinelist, 3, 0, (struct sockaddr*)&ser, ser_length);
  76.  
    if (iSend == SOCKET_ERROR)
  77.  
    {
  78.  
    cout << "sendto()Failed:" << WSAGetLastError() << endl;
  79.  
    return false;
  80.  
    }
  81.  
    memset(recv_buf, 0, sizeof(recv_buf));
  82.  
    iRecv = recvfrom(sClient, recv_buf, sizeof(recv_buf), 0, (struct sockaddr*)&communication, &communication_length); ///
  83.  
    if (iRecv == SOCKET_ERROR)
  84.  
    {
  85.  
    cout << "recvfrom() Failed" << WSAGetLastError() << endl;
  86.  
    return false;
  87.  
    }
  88.  
    string list(recv_buf);
  89.  
    string friendname;
  90.  
    for (int i = 0; i < list.length(); i )
  91.  
    {
  92.  
    if (list[i] == '$') break;
  93.  
    else if (list[i] == '#')
  94.  
    {
  95.  
    onlinelist.push_back(friendname);
  96.  
    friendname = "";
  97.  
    }
  98.  
    else
  99.  
    friendname = friendname list[i];
  100.  
    }
  101.  
    cout << "----------------------------" << endl;
  102.  
    cout << "在线好友列表" << endl;
  103.  
    for (int i = 0; i < onlinelist.size(); i )
  104.  
    cout << i << ": " << onlinelist[i] << endl;
  105.  
    cout << "----------------------------" << endl;
  106.  
    return true;
  107.  
    }
  108.  
     
  109.  
    void client::work()
  110.  
    {
  111.  
    while (true)
  112.  
    {
  113.  
    memset(recv_buf, 0, sizeof(recv_buf));
  114.  
    system("cls");
  115.  
    cout << "****************************************" << endl;
  116.  
    cout << "* *" << endl;
  117.  
    cout << "* 1.登录 2.注册 3.退出 *" << endl;
  118.  
    cout << "* *" << endl;
  119.  
    cout << "****************************************" << endl;
  120.  
     
  121.  
    string choice;
  122.  
    getline(cin, choice);
  123.  
    if (choice == "1")
  124.  
    {
  125.  
    system("cls");
  126.  
    cout << "请输入用户名:";
  127.  
    string username;
  128.  
    getline(cin, username);
  129.  
    cout << "请输入密码:";
  130.  
    string password;
  131.  
    getline(cin, password);
  132.  
    //产生显示器端口
  133.  
    receiver_port = GeneratePort();
  134.  
    //将端口号写入文件供显示器程序读取
  135.  
    ofstream out("port.txt");
  136.  
    out << receiver_port << "\n" << username;
  137.  
    out.close();
  138.  
     
  139.  
    string init_infortemp = "L" username "#" password "#" to_string(receiver_port);
  140.  
    char *init_infor = new char[init_infortemp.length() 1];
  141.  
    strcpy(init_infor, init_infortemp.c_str());
  142.  
    //向服务器验证用户信息
  143.  
    iSend = sendto(sClient, init_infor, init_infortemp.length() 1, 0, (struct sockaddr*)&ser, ser_length);
  144.  
    //接收服务器回应的消息
  145.  
    iRecv = recvfrom(sClient, recv_buf, sizeof(recv_buf), 0, (SOCKADDR*)&communication, &communication_length);
  146.  
    if (iRecv == SOCKET_ERROR)
  147.  
    {
  148.  
    cout << "recvfrom() Failed:" << GetLastError() << endl;
  149.  
    cout << "未收到服务器的响应,登录失败,请输入Y返回首页:";
  150.  
    string ret;
  151.  
    while (getline(cin, ret))
  152.  
    {
  153.  
    if (ret == "Y")break;
  154.  
    cout << "未收到服务器的响应,登录失败,请输入Y返回首页:";
  155.  
    }
  156.  
    continue;
  157.  
    }
  158.  
    if (recv_buf[0] == 'Y') //登录成功
  159.  
    {
  160.  
    system("cls");
  161.  
    ShellExecute(NULL, _T("open"), _T("receiver.exe"), NULL, NULL, SW_SHOW); //运行显示器程序
  162.  
    }
  163.  
    else if (recv_buf[0] == '0')
  164.  
    {
  165.  
    cout << "未注册用户名,登录失败,请输入Y返回首页:";
  166.  
    string ret;
  167.  
    while (getline(cin, ret))
  168.  
    {
  169.  
    if (ret == "Y")break;
  170.  
    cout << "未注册用户名,登录失败,请输入Y返回首页:";
  171.  
    }
  172.  
    continue;
  173.  
    }
  174.  
    else if (recv_buf[0] == '1')
  175.  
    {
  176.  
    cout << "密码错误,登录失败,请输入Y返回首页:" << endl;
  177.  
    string ret;
  178.  
    while (getline(cin, ret))
  179.  
    {
  180.  
    if (ret == "Y")break;
  181.  
    cout << "密码错误,登录失败,请输入Y返回首页:";
  182.  
    }
  183.  
    continue;
  184.  
    }
  185.  
    else if (recv_buf[0] == '2')
  186.  
    {
  187.  
    cout << "重复登录,登录失败,请输入Y返回首页:" << endl;
  188.  
    string ret;
  189.  
    while (getline(cin, ret))
  190.  
    {
  191.  
    if (ret == "Y")break;
  192.  
    cout << "重复登录,登录失败,请输入Y返回首页:";
  193.  
    }
  194.  
    continue;
  195.  
    }
  196.  
     
  197.  
    //选择聊天方式
  198.  
    while (true)
  199.  
    {
  200.  
    system("cls");
  201.  
    cout << "---------------------------------------------------" << endl;
  202.  
    cout << " 用户名:" << username << endl << endl;;
  203.  
    cout << " 1.私聊 2.群聊 3.退出登录 " << endl << endl;
  204.  
    cout << "---------------------------------------------------" << endl;
  205.  
    string mode;
  206.  
    getline(cin, mode);
  207.  
    if (mode == "1") //私聊
  208.  
    {
  209.  
    system("cls");
  210.  
    cout << "私聊模式中,输入return返回上一级" << endl << endl;
  211.  
    if (!Getonlinelist()) continue; //获取好友在线列表失败
  212.  
    cout << "请选择私聊对象的序号" << endl;
  213.  
    string choose;
  214.  
    getline(cin, choose);
  215.  
    if (choose == "return") continue;
  216.  
    int i = 0;
  217.  
    for (i = 0; i < choose.size(); i )
  218.  
    if (choose[i] > '9' || choose[i] < '0')break;
  219.  
    if (i < choose.size()) continue;
  220.  
    stringstream stream(choose);
  221.  
    int index = 0;
  222.  
    stream >> index;
  223.  
    if (index<0 || index>=onlinelist.size()) continue;
  224.  
    while (true) //向该用户循环发送消息,直到输入return退出
  225.  
    {
  226.  
    system("cls");
  227.  
    cout << "正在和" << onlinelist[index] << "私聊中" << ",输入return返回上一级" << endl << endl;
  228.  
    string message;
  229.  
    getline(cin, message);
  230.  
    if (message == "return")
  231.  
    {
  232.  
    system("cls");
  233.  
    break;
  234.  
    }
  235.  
    message = "PM" onlinelist[index] "#" message;
  236.  
    char *buf = new char[message.length() 1];
  237.  
    strcpy(buf, message.c_str());
  238.  
    iSend = sendto(sClient, buf, message.length() 1, 0, (struct sockaddr*)&ser, ser_length);
  239.  
    if (iSend == SOCKET_ERROR)
  240.  
    {
  241.  
    cout << "sendto()Failed:" << WSAGetLastError() << endl;
  242.  
    break;
  243.  
    }
  244.  
    delete[]buf;
  245.  
    iRecv = recvfrom(sClient, recv_buf, sizeof(recv_buf), 0, (SOCKADDR*)&communication, &communication_length);
  246.  
    if (recv_buf[0] == 'Y') continue;
  247.  
    else
  248.  
    {
  249.  
    cout << onlinelist[index] << "已下线" << "输入Y返回主菜单";
  250.  
    string ret;
  251.  
    while (getline(cin, ret))
  252.  
    {
  253.  
    if (ret == "Y") break;
  254.  
    cout << onlinelist[index] << "已下线" << "输入Y返回主菜单";
  255.  
    }
  256.  
    break;
  257.  
    }
  258.  
    }
  259.  
    }
  260.  
    else if (mode == "2") //群聊
  261.  
    {
  262.  
    system("cls");
  263.  
    while (true)
  264.  
    {
  265.  
    system("cls");
  266.  
    cout << "群聊模式,输入return返回上一级" << endl << endl;
  267.  
    string message;
  268.  
    getline(cin, message);
  269.  
    if (message == "return")
  270.  
    {
  271.  
    system("cls");
  272.  
    break;
  273.  
    }
  274.  
     
  275.  
    message = "G" message;
  276.  
    char *buf = new char[message.length() 1];
  277.  
    strcpy(buf, message.c_str());
  278.  
    iSend = sendto(sClient, buf, message.length() 1, 0, (struct sockaddr*)&ser, ser_length);
  279.  
    delete[]buf;
  280.  
    if (iSend == SOCKET_ERROR)
  281.  
    {
  282.  
    cout << "sendto()Failed:" << WSAGetLastError() << endl;
  283.  
    break;
  284.  
    }
  285.  
    }
  286.  
    continue;
  287.  
    }
  288.  
    else if (mode == "3") //退出登录
  289.  
    {
  290.  
    char buf[] = "exit";
  291.  
    iSend = sendto(sClient, buf, sizeof(buf), 0, (struct sockaddr*)&ser, ser_length);
  292.  
    break;
  293.  
    }
  294.  
    else
  295.  
    continue;
  296.  
    }
  297.  
    }
  298.  
    else if (choice == "2")
  299.  
    {
  300.  
    system("cls");
  301.  
    cout << "请设置用户名:";
  302.  
    string username;
  303.  
    getline(cin, username);
  304.  
    cout << "请设置登录密码:";
  305.  
    string password;
  306.  
    getline(cin, password);
  307.  
    string init_infortemp = "R" username "#" password;
  308.  
    char *init_infor = new char[init_infortemp.length() 1];
  309.  
    strcpy(init_infor, init_infortemp.c_str());
  310.  
    //向服务器发送注册用户信息
  311.  
    iSend = sendto(sClient, init_infor, init_infortemp.length() 1, 0, (struct sockaddr*)&ser, ser_length);
  312.  
    //接收服务器回应的消息
  313.  
    iRecv = recvfrom(sClient, recv_buf, sizeof(recv_buf), 0, (struct sockaddr*)&communication, &communication_length);
  314.  
    if (recv_buf[0] == 'Y')
  315.  
    {
  316.  
    cout << "注册成功" << endl;
  317.  
    continue;
  318.  
    }
  319.  
    else
  320.  
    {
  321.  
    cout << "用户名已存在,注册失败,请输入Y返回首页:" << endl;
  322.  
    string ret;
  323.  
    while (getline(cin, ret))
  324.  
    {
  325.  
    if (ret == "Y")break;
  326.  
    cout << "用户名已存在,注册失败,请输入Y返回首页:";
  327.  
    }
  328.  
    continue;
  329.  
    }
  330.  
    }
  331.  
    else if (choice == "3")
  332.  
    {
  333.  
    closesocket(sClient);
  334.  
    WSACleanup;
  335.  
    return;
  336.  
    }
  337.  
    else
  338.  
    continue;
  339.  
    }
  340.  
    }
  341.  
     
  342.  
    int main()
  343.  
    {
  344.  
    client x;
  345.  
    if (x.Startup() == false)
  346.  
    return 0;
  347.  
    x.SetServerAddress();
  348.  
    x.work();
  349.  
    }

客户端接收器的完整代码

  1.  
    #include<WinSock2.h>
  2.  
    #include<iostream>
  3.  
    #include<fstream>
  4.  
    #include<string>
  5.  
    #include<ctime>
  6.  
    #pragma comment(lib,"ws2_32.lib")
  7.  
    using namespace std;
  8.  
    #define DEFAULT_SPORT 5055
  9.  
    #define DEFAULT_CPORT 5056
  10.  
    #define BUFFER_LENGTH 1024
  11.  
     
  12.  
    void main()
  13.  
    {
  14.  
    WSADATA wsaData;
  15.  
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  16.  
    {
  17.  
    cout << "Failed to load Winsock." << endl;
  18.  
    return;
  19.  
    }
  20.  
    //建立显示器端地址
  21.  
    struct sockaddr_in receiver;
  22.  
    //读取分配好的端口
  23.  
    ifstream in("port.txt");
  24.  
    string receiver_port;
  25.  
    string username;
  26.  
    getline(in, receiver_port);
  27.  
    getline(in, username);
  28.  
    in.close();
  29.  
    remove("port.txt");
  30.  
    int receiver_port_int = atoi(receiver_port.c_str());
  31.  
     
  32.  
    receiver.sin_family = AF_INET;
  33.  
    receiver.sin_port = htons(receiver_port_int); //htons()函数把一个双字节主机字节顺序的数转换为网络字节顺序的数
  34.  
    receiver.sin_addr.s_addr = htonl(INADDR_ANY); //htonl()函数把一个主机字节顺序的数转换为网络字节顺序的数
  35.  
    SOCKET rSocket = socket(AF_INET, SOCK_DGRAM, 0);
  36.  
    if (rSocket == INVALID_SOCKET)
  37.  
    {
  38.  
    cout << "socket()Failed:" << WSAGetLastError() << endl;
  39.  
    return;
  40.  
    }
  41.  
    if (bind(rSocket, (LPSOCKADDR)&receiver, sizeof(receiver)) == SOCKET_ERROR)
  42.  
    {
  43.  
    cout << "bind()Failed:" << WSAGetLastError() << endl;
  44.  
    return;
  45.  
    }
  46.  
     
  47.  
    char recv_buf[BUFFER_LENGTH]; //接收数据的缓冲区
  48.  
    memset(recv_buf, 0, sizeof(recv_buf)); //初始化接收缓冲区
  49.  
     
  50.  
    struct sockaddr_in ser; //客户端地址
  51.  
    int ser_length = sizeof(ser); //客户端地址长度
  52.  
     
  53.  
     
  54.  
    cout << "----------------------------------------" << endl << endl;
  55.  
    cout << " 显示器---" << username << endl << endl << endl;
  56.  
    cout << "----------------------------------------" << endl << endl;
  57.  
     
  58.  
    while (true) //进入一个无限循环,进行数据接收和发送
  59.  
    {
  60.  
    int iRecv = recvfrom(rSocket, recv_buf, BUFFER_LENGTH, 0, (SOCKADDR*)&ser, &ser_length);
  61.  
    string transmessage(recv_buf);
  62.  
    if (iRecv == SOCKET_ERROR)
  63.  
    {
  64.  
    cout << "recvfrom()Failed:" << WSAGetLastError() << endl;
  65.  
    break;
  66.  
    }
  67.  
    else if (transmessage == "exit") break;
  68.  
    else
  69.  
    cout << transmessage << endl;
  70.  
    }
  71.  
    closesocket(rSocket);
  72.  
    WSACleanup();
  73.  
    }


 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfceeg
系列文章
更多 icon
同类精品
更多 icon
继续加载