C语言实现网络编程课程设计,c语言网络编程实现银行排号系统

网络安全编程综合实训》报告

2023  11 28  

目  录

目录

正文部分....................................................................................................................................................... 3

概述............................................................................................................................................................ 3

系统分析................................................................................................................................................... 3

系统实现................................................................................................................................................... 3

模块实现流程图................................................................................................................................ 4

各个模块实现的代码....................................................................................................................... 9

代码运行结果图.............................................................................................................................. 24

实验小结................................................................................................................................................ 32

正文部分

一.概述

这个银行排号系统是一个网络系统,由三个部分组成:一个服务器、一个取号程序、一个叫号程序和两个业务程序。其中服务器采用指定的TCPI/0复用模型,其它部分可以采用任意模型。这个系统的主要功能是为顾客提供便捷的银行排号服务,让顾客能够更加高效地办理业务,同时也能够提高银行的服务效率和工作效率。

具体来说,顾客可以通过取号程序获取一个唯一的排队号码,而业务程序则可以获取下一个可以办理的业务号码,两个业务程序可以同时运行,实现交叉测试。叫号程序则负责显示当前正在办理的业务号码,让顾客可以及时了解自己的排队情况。

这个银行排号系统的意义非常重要。首先,它可以有效地减少顾客排队的时间和等待的不安感,提高了顾客的满意度。其次,它可以帮助银行实现自动化、智能化的管理,提高了银行的工作效率和服务质量。最后,它也可以为银行提供更加全面、准确的数据分析,帮助银行更好地了解顾客需求和业务情况,进一步优化银行的服务和管理。

二.系统分析

  1. 系统有三个部分组成,分别包括:一个服务器,一个取号程序,一个叫号程序,两个业务程序。
  2. 服务器用指定的模型,其它部分可以采用任意模型
  3. 服务器功能
    1. 取号程序取号,要保证号码的唯一性。
    2. 业务程序取号
    3. 通知叫号程序叫号(即显示当前正在办业务号)
  4. 取号程序功能:通过服务器获取号码,并显示
  5. 业务程序功能:通过服务器获取下一个可以办业务号码,并显示。两个业务程序应同时运行,交叉测试。
  6. 叫号程序:显示当前正在办业务号

三.系统实现

(每个模块功能实现的流程图,各模块实现的代码及运行结果图)

每个模块功能实现的流程图:

以下为各个模块实现流程图:

服务器模块:

业务程序模块:

一直循环等待接收数据

判断接收是否成功

开始

初始化Winsock -> 创建客户端套接字 -> 设置服务器地址和端口 -> 连接到服务器

                              成功               失败

判断接收的数组第50位x

连接失败

菱形: 判断接收的数组第50位x

                              x>0                x<0

在处理的号码为X

等待取号

创建一个新的缓冲区 -> 将"办理业务"复制到缓冲区中 -> 在缓冲区的第50位插入接收到数组第50位的值x -> 休眠20,000毫秒 -> 将缓冲区发送到服务器

关闭客户端套接字 -> 清理Winsock库资源 -> 结束

取号程序模块

判断发送是否成功

开始

初始化Winsock -> 创建客户端套接字 -> 设置服务器地址和端口 -> 连接到服务器

发送取号请求

菱形: 判断发送是否成功

                              成功               失败

判断接收的数组是否接收成功x

发送失败

                              result>0           result<0

接收成功

接收失败

显示当前取得的号码

关闭客户端套接字 -> 清理Winsock库资源 -> 结束

叫号程序模块

一直循环请求显示当前号码

判断接收是否成功

开始

初始化Winsock -> 创建客户端套接字 -> 设置服务器地址和端口 -> 连接到服务器

                              成功               失败

判断接收的数组是否成功

连接失败

菱形: 判断接收的数组是否成功

                              result>0           result<0

连接成功

接收失败

显示当前在处理的号码

关闭客户端套接字 -> 清理Winsock库资源 -> 结束

服务器程序.cpp

  1. // server.cpp : Defines the entry point for the console application.
  2. // ws2_32.lib
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <stdio.h>
  6. #include <winsock2.h>
  7. #include <stdlib.h>
  8. #include <map>
  9. #define MAX_CLIENTS 10   // 最大客户端连接数
  10. #define BUFFER_SIZE 1024 // 缓冲区大小
  11. // 在服务器端定义一个结构体来存储客户端的IP地址和端口号
  12. struct ClientInfo
  13. {
  14.     char ip[16];
  15.     int port;
  16.     int socket; // 添加一个字段用于存储套接字
  17. };
  18. // 在服务器端定义一个数组来存储客户端信息
  19. struct ClientInfo client_info[MAX_CLIENTS];
  20. // std::mutex mtx_number;
  21. //  在服务器端接收到连接请求时,保存客户端的IP地址、端口号和套接字
  22. void saveClientInfo(struct sockaddr_in client_address, int client_socket, int index)
  23. {
  24.     // inet_ntop(AF_INET, &(client_address.sin_addr), client_info[index].ip, INET_ADDRSTRLEN);
  25.     strcpy(client_info[index].ip, inet_ntoa(client_address.sin_addr));
  26.     client_info[index].port = ntohs(client_address.sin_port);
  27.     client_info[index].socket = client_socket;
  28. }
  29. // 在服务器端根据客户端的索引查找对应的套接字
  30. int getClientSocket(int index)
  31. {
  32.     return client_info[index].socket;
  33. }
  34. std::map<int, ClientInfo> client_map;
  35. int main()
  36. {
  37.     WSADATA wsaData;
  38.     SOCKET server_socket, client_sockets[MAX_CLIENTS], max_socket;
  39.     struct sockaddr_in server_addressclient_address;
  40.     fd_set read_fds;
  41.     int activity, i, valread, addrlen, client_socket, sd;
  42.     char buffer[BUFFER_SIZE];
  43.     int current_number = 0;
  44.     int new_number = 0;
  45.     int haoma[1000];
  46.     int number = 0;
  47.     bool flag = true;
  48.     // 初始化Winsock
  49.     if (WSAStartup(MAKEWORD(22), &wsaData) != 0)
  50.     {
  51.         printf("Winsock initialization failed.\n");
  52.         return 1;
  53.     }
  54.     // 创建服务器套接字
  55.     if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  56.     {
  57.         printf("Socket creation failed.\n");
  58.         return 1;
  59.     }
  60.     // 设置服务器地址和端口
  61.     server_address.sin_family = AF_INET;
  62.     server_address.sin_addr.s_addr = INADDR_ANY;
  63.     server_address.sin_port = htons(12345);
  64.     // 绑定服务器套接字到指定地址和端口
  65.     if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == SOCKET_ERROR)
  66.     {
  67.         printf("Socket bind failed.\n");
  68.         return 1;
  69.     }
  70.     // 监听来自客户端的连接请求
  71.     if (listen(server_socket, 5) == SOCKET_ERROR)
  72.     {
  73.         printf("Socket listen failed.\n");
  74.         return 1;
  75.     }
  76.     printf("Server is listening on port 12345.\n");
  77.     // 初始化客户端套接字数组
  78.     for (i = 0; i < MAX_CLIENTS; i++)
  79.     {
  80.         client_sockets[i] = 0;
  81.     }
  82.     addrlen = sizeof(client_address);
  83.     std::map<int, ClientInfo> client_map;
  84.     while (1)
  85.     {
  86.         // 清空可读套接字集合
  87.         FD_ZERO(&read_fds);
  88.         // 将服务器套接字添加到可读套接字集合
  89.         FD_SET(server_socket, &read_fds);
  90.         max_socket = server_socket;
  91.         // 将客户端套接字添加到可读套接字集合
  92.         for (i = 0; i < MAX_CLIENTS; i++)
  93.         {
  94.             sd = client_sockets[i];
  95.             // 如果客户端套接字有效,则添加到可读套接字集合
  96.             if (sd > 0)
  97.             {
  98.                 FD_SET(sd, &read_fds);
  99.             }
  100.             // 更新最大套接字值
  101.             if (sd > max_socket)
  102.             {
  103.                 max_socket = sd;
  104.             }
  105.         }
  106.         // 使用select函数进行I/O复用
  107.         activity = select(max_socket + 1, &read_fds, NULLNULLNULL);
  108.         if (activity == SOCKET_ERROR)
  109.         {
  110.             printf("Select error.\n");
  111.             return 1;
  112.         }
  113.         // 如果服务器套接字有可读数据,表示有新连接请求
  114.         if (FD_ISSET(server_socket, &read_fds))
  115.         {
  116.             if ((client_socket = accept(server_socket, (struct sockaddr *)&client_address, &addrlen)) == INVALID_SOCKET)
  117.             {
  118.                 printf("Accept error.\n");
  119.                 return 1;
  120.             }
  121.             printf("New connection from %s:%d.\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
  122.             // 将新的客户端套接字添加到客户端套接字数组
  123.             for (i = 0; i < MAX_CLIENTS; i++)
  124.             {
  125.                 if (client_sockets[i] == 0)
  126.                 {
  127.                     client_sockets[i] = client_socket;
  128.                     break;
  129.                 }
  130.             }
  131.             for (i = 0; i < MAX_CLIENTS; i++)
  132.             {
  133.                 ClientInfo new_client_info;
  134.                 saveClientInfo(client_address, client_socket, i);
  135.                 new_client_info = client_info[i]; // 获取刚保存的客户端信息
  136.                 client_map[client_socket] = new_client_info;
  137.             }
  138.         }
  139.         // 处理客户端套接字的请求
  140.         for (i = 0; i < MAX_CLIENTS; i++)
  141.         {
  142.             sd = client_sockets[i];
  143.             // 如果客户端套接字有可读数据,表示有请求到达
  144.             if (FD_ISSET(sd, &read_fds))
  145.             {
  146.                 if ((valread = recv(sd, buffer, BUFFER_SIZE, 0)) == SOCKET_ERROR)
  147.                 {
  148.                     printf("Receive error.\n");
  149.                     return 1;
  150.                 }
  151.                 // 处理客户端请求并发送响应
  152.                 if (client_map.find(sd) != client_map.end())
  153.                 {
  154.                     ClientInfo &client = client_map[sd];
  155.                     if (valread > 0)
  156.                     {
  157.                         char recv_buf[100];
  158.                         buffer[valread] = '\0';
  159.                         printf("Received request: %s\n", buffer);
  160.                         // 根据不同的请求进行处理和反馈
  161.                         /*
  162.                             //设置falg2个作用
  163.                             //作用第一次得到取号动作的时候,这时就把号码数组里此时的值发送给业务程序12,他们竞争这次业务。
  164.                             //同时目的是让服务器和业务程序建立连接起来。因为如果不产生联系,服务器是不够凭空直接发送数据给业务程序的
  165.                             //并且我的业务程序始终是监听状态
  166.                             */
  167.                         if (strcmp(buffer, "取号") == 0)
  168.                         {
  169.                             char response[20];
  170.                             sprintf(response, "您的号码是%03d", new_number);
  171.                             send(sd, response, BUFFER_SIZE, 0);
  172.                             haoma[new_number] = new_number;
  173.                             new_number++;
  174.                             // printf("flag:%d\n",flag);
  175.                             if (flag)
  176.                             {
  177.                                 char buf[100] = {0};
  178.                                 memset(buf, 0sizeof(buf));
  179.                                 sprintf(&buf[50], "%d", haoma[number]);
  180.                                 // send(b, buf, 100, 0);
  181.                                 send(client_sockets[0], buf, 1000);
  182.                                 send(client_sockets[1], buf, 1000);
  183.                                 flag = FALSE;
  184.                             }
  185.                             // printf("number:%d\n",number);
  186.                         }
  187.                         /*
  188.                             //业务程序接收到对方,每次发送过来只需要取出头,则只拿头部分就行对方的此时处理的号码的值就是haoma[number]
  189.                         把此时的number值给到current_number,让叫号程序得到最新的处理号码值
  190.                         //判断就是等待取号,同时把此时的falg改为true 为的是在下次取号的时候再由取号程序去发送第一次数据,让服务器和业务程序建立连接
  191.                         //判断就是正在处理xx号码,然后number往后加1
  192.                             */
  193.                         // startnum=new_number;
  194.                         else if (strncmp(buffer, "办理业务"12) == 0)
  195.                         {
  196.                             // printf("new_number:%d\n",new_number);
  197.                             // saveClientInfo(client_address, sd,1);
  198.                             // int m = atoi(&buffer[50]);
  199.                             //  int  b=client_sockets[0];
  200.                             //  new_number = 0;
  201.                             char buf[100] = {0};
  202.                             memset(buf, 0sizeof(buf));
  203.                             sprintf(&buf[50], "%d", haoma[number]);
  204.                             send(client_sockets[0], buf, 1000);
  205.                             current_number = haoma[number];
  206.                             if (haoma[number] < 0)
  207.                             {
  208.                                 printf("等待取号\n");
  209.                                 flag = true;
  210.                             }
  211.                             else
  212.                             {
  213.                                 printf("正在处理%d\n", haoma[number]);
  214.                                 number = number + 1;
  215.                             }
  216.                             // number=number+1;
  217.                             // current_number=m;
  218.                             // send(a, response, BUFFER_SIZE, 0);
  219.                             // printf("number:%d\n",number);
  220.                         }
  221.                         else if (strncmp(buffer, "办理业务1"13) == 0)
  222.                         {
  223.                             // printf("new_number:%d\n",new_number);
  224.                             // saveClientInfo(client_address, sd,1);
  225.                             // int m = atoi(&buffer[50]);
  226.                             // int  c=client_sockets[1];
  227.                             // printf("接收到业务程序发来,可以处理的下一个业务号码m的值为: %d\n", m);
  228.                             char buf[100] = {0};
  229.                             memset(buf, 0sizeof(buf));
  230.                             sprintf(&buf[50], "%d", haoma[number]);
  231.                             send(client_sockets[1], buf, 1000);
  232.                             current_number = haoma[number];
  233.                             if (haoma[number] < 0)
  234.                             {
  235.                                 printf("等待取号\n");
  236.                                 flag = true;
  237.                             }
  238.                             else
  239.                             {
  240.                                 printf("正在处理%d\n", haoma[number]);
  241.                                 number = number + 1;
  242.                             }
  243.                             // number=number+1;
  244.                             // current_number=m;
  245.                             // send(a, response, BUFFER_SIZE, 0);
  246.                             // printf("number:%d\n",number);
  247.                         }
  248.                         else if (strcmp(buffer, "请求当前号码") == 0)
  249.                         {
  250.                             saveClientInfo(client_address, sd, 0);
  251.                             char response[20];
  252.                             int n = 0;
  253.                             if (current_number - 1 < 0)
  254.                             {
  255.                                 sprintf(response, "等待取号\n");
  256.                             }
  257.                             else
  258.                             {
  259.                                 sprintf(response, "当前号码是%03d\n", current_number);
  260.                             }
  261.                             send(sd, response, BUFFER_SIZE, 0);
  262.                             // printf("服务器当前号码为%d\n",current_number);
  263.                         }
  264.                         else if (strcmp(buffer, "退出") == 0)
  265.                         {
  266.                             send(sd, "退出成功"strlen("退出成功"), 0);
  267.                             closesocket(sd);
  268.                             client_sockets[i] = 0;
  269.                             client_map.erase(sd); // map中删除对应的客户端状态信息
  270.                         }
  271.                         else
  272.                         {
  273.                             send(sd, "无法识别的请求"strlen("无法识别的请求"), 0);
  274.                         }
  275.                     }
  276.                 }
  277.             }
  278.         }
  279.     }
  280.     // 关闭服务器套接字
  281.     closesocket(server_socket);
  282.     // 清理Winsock库资源
  283.     WSACleanup();
  284.     return 0;
  285. }

业务程序1.cpp:

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <winsock2.h>
  5. #include <process.h>
  6. // 业务程序
  7. #define BUFFER_SIZE 1024 // 缓冲区大小
  8. int m = 0;
  9. int main()
  10. {
  11.     WSADATA wsaData;
  12.     SOCKET client_socket;
  13.     struct sockaddr_in server_address;
  14.     char buffer[BUFFER_SIZE];
  15.     // 初始化Winsock
  16.     if (WSAStartup(MAKEWORD(22), &wsaData) != 0)
  17.     {
  18.         printf("Winsock initialization failed.\n");
  19.         return 1;
  20.     }
  21.     // 创建客户端套接字
  22.     if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  23.     {
  24.         printf("Socket creation failed.\n");
  25.         return 1;
  26.     }
  27.     // 设置服务器地址和端口
  28.     server_address.sin_family = AF_INET;
  29.     server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
  30.     server_address.sin_port = htons(12345);                  // 服务器端口号
  31.     // 连接到服务器
  32.     if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
  33.     {
  34.         printf("Connection failed.\n");
  35.         return 1;
  36.     }
  37.  /*
  38.   //业务程序只有服务器这里发送数据过来才会执行后面逻辑
  39.   //先取出第50位的值 即发来正在处理的号码,打印出来
  40.   //若小于则表示发送过来的haoma[number]还未赋值,则说明这个号码是未取出,打印等待取号
  41.   */
  42.     while (1)
  43.     { 
  44.   int result=recv(client_socket, buffer, 1000);
  45.   int x = atoi(&buffer[50]);
  46.   if(x<0){
  47.    printf("等待取号\n");
  48.    }
  49.    else{
  50.     printf("在处理的号码为:%d\n",x);}
  51.         if (result< 0)
  52.         {
  53.             printf("Receive response failed.\n");
  54.         }
  55.         else
  56.         {   
  57.             char buf[100] = {0};
  58.             memset(buf, 0sizeof(buf));
  59.             strcpy(buf, "办理业务");
  60.    sprintf(&buf[50], "%d",x);
  61.             Sleep(20000);
  62.             int result = send(client_socket, buf, sizeof(buf), 0);
  63.             if (result < 0)
  64.             {
  65.                 printf("Send request failed.\n");
  66.                 return 1;
  67.             }
  68.         }
  69.     }
  70.     closesocket(client_socket);
  71.     // 清理Winsock库资源
  72.     WSACleanup();
  73.     return 0;
  74. }

业务程序2.cpp:

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <winsock2.h>
  5. // 业务程序
  6. #define BUFFER_SIZE 1024 // 缓冲区大小
  7. int main()
  8. {
  9.     WSADATA wsaData;
  10.     SOCKET client_socket;
  11.     struct sockaddr_in server_address;
  12.     char buffer[BUFFER_SIZE];
  13.     // 初始化Winsock
  14.     if (WSAStartup(MAKEWORD(22), &wsaData) != 0)
  15.     {
  16.         printf("Winsock initialization failed.\n");
  17.         return 1;
  18.     }
  19.     // 创建客户端套接字
  20.     if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  21.     {
  22.         printf("Socket creation failed.\n");
  23.         return 1;
  24.     }
  25.     // 设置服务器地址和端口
  26.     server_address.sin_family = AF_INET;
  27.     server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
  28.     server_address.sin_port = htons(12345);                  // 服务器端口号
  29.     // 连接到服务器
  30.     if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
  31.     {
  32.         printf("Connection failed.\n");
  33.         return 1;
  34.     }
  35.     while (1)
  36.     {  
  37.   int result=recv(client_socket, buffer, 1000);
  38.  // int x = atoi(&buffer[50]);
  39.       //  printf("在处理的号码为:%d\n",x);
  40.         if (result < 0
  41.         {
  42.             printf("Receive response failed.\n");
  43.         }
  44.         else
  45.         {   
  46.    int x = atoi(&buffer[50]);
  47.    if(x<0){
  48.    printf("等待取号\n");
  49.    }
  50.    else{
  51.     printf("在处理的号码为:%d\n",x);}
  52.             char buf[100] = {0};
  53.             memset(buf, 0sizeof(buf));
  54.             strcpy(buf, "办理业务1");
  55.     sprintf(&buf[50], "%d",x);
  56.             Sleep(20000);
  57.             int result = send(client_socket, buf, sizeof(buf), 0);
  58.             if (result < 0)
  59.             {
  60.                 printf("Send request failed.\n");
  61.                 return 1;
  62.             }
  63.         }
  64.     }
  65.     closesocket(client_socket);
  66.     // 清理Winsock库资源
  67.     WSACleanup();
  68.     return 0;
  69. }

叫号程序.cpp

  1. // 网络编程课程设计.cpp : Defines the entry point for the console application.
  2. //server服务器端
  3. //叫号
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <winsock2.h>
  7. #define BUFFER_SIZE 1024  // 缓冲区大小
  8. int main() {
  9.     WSADATA wsaData;
  10.     SOCKET client_socket;
  11.     struct sockaddr_in server_address;
  12.     char buffer[BUFFER_SIZE];
  13.     // 初始化Winsock
  14.     if (WSAStartup(MAKEWORD(22), &wsaData) != 0) {
  15.         printf("Winsock initialization failed.\n");
  16.         return 1;
  17.     }
  18.     // 创建客户端套接字
  19.     if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
  20.         printf("Socket creation failed.\n");
  21.         return 1;
  22.     }
  23.     // 设置服务器地址和端口
  24.     server_address.sin_family = AF_INET;
  25.     server_address.sin_addr.s_addr = inet_addr("127.0.0.1");  // 服务器IP地址
  26.     server_address.sin_port = htons(12345);  // 服务器端口号
  27.     // 连接到服务器
  28.     if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
  29.         printf("Connection failed.\n");
  30.         return 1;
  31.     }
  32.     // 循环获取当前号码
  33.     while (1) {
  34.         // 发送请求获取当前号码
  35.         if (send(client_socket, "请求当前号码"strlen("请求当前号码"), 0) < 0) {
  36.             printf("Send request failed.\n");
  37.             return 1;
  38.         }
  39.         // 接收服务器的响应,并显示当前号码
  40.         if (recv(client_socket, buffer, BUFFER_SIZE, 0) < 0) {
  41.             printf("Receive response failed.\n");
  42.             break;
  43.         }
  44.         else {
  45.             printf("Current number: %s\n", buffer);
  46.         }
  47.   Sleep(6000);
  48.     }
  49.     // 关闭客户端套接字
  50.     closesocket(client_socket);
  51.     // 清理Winsock库资源
  52.     WSACleanup();
  53.     return 0;
  54. }
  55.  

取号程序.cpp

  1. // 实验九基于事件模型的网络通信.cpp : Defines the entry point for the console application.
  2. //取号
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <winsock2.h>
  6. #include <map>
  7. #define BUFFER_SIZE 1024  // 缓冲区大小
  8. int main() {
  9.     WSADATA wsaData;
  10.     SOCKET client_socket;
  11.     struct sockaddr_in server_address;
  12.     char buffer[BUFFER_SIZE];
  13.     // 初始化Winsock
  14.     if (WSAStartup(MAKEWORD(22), &wsaData) != 0) {
  15.         printf("Winsock initialization failed.\n");
  16.         return 1;
  17.     }
  18.     // 创建客户端套接字
  19.     if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
  20.         printf("Socket creation failed.\n");
  21.         return 1;
  22.     }
  23.     // 设置服务器地址和端口
  24.     server_address.sin_family = AF_INET;
  25.     server_address.sin_addr.s_addr = inet_addr("127.0.0.1");  // 服务器IP地址
  26.     server_address.sin_port = htons(12345);  // 服务器端口号
  27.     // 连接到服务器
  28.     if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
  29.         printf("Connection failed.\n");
  30.         return 1;
  31.     }
  32.     // 循环取号
  33. // while(1){
  34.         // 发送取号请求
  35.         if (send(client_socket, "取号"strlen("取号"), 0) < 0) {
  36.             printf("Send request failed.\n");
  37.             return 1;
  38.         }
  39.         // 接收服务器的响应,并显示取到的号码
  40.         if (recv(client_socket, buffer, BUFFER_SIZE, 0) < 0) {
  41.             printf("Receive response failed.\n");
  42.         }
  43.         else {
  44.             printf("Number received: %s\n", buffer);
  45.         }
  46. //  Sleep(8000);
  47. // }
  48.     // 关闭客户端套接字
  49.     closesocket(client_socket);
  50.     // 清理Winsock库资源
  51.     WSACleanup();
  52.     return 0;
  53. }

运行结果图:

服务器程序:

1)启动服务器

2)连接到业务程序1

3)连接到业务程序2

4)连接到叫号程序,此时还未取号

5)启动取号程序,连接到取号程序

6)显示当前处理到的号码,交给业务程序交叉处理

7)业务程序处理完号码后,继续等待取号此时叫号程序也显示等待取号

8)处理后的各个程序结果

9)此时再次取号

10)业务程序再次交叉处理,同时叫号程序显示在处理的值

业务程序1:

1)启动,此时还未取号

2)业务程序处理过程

业务程序2:

1)启动,此时还未取号

2)业务程序2处理过程

叫号程序:

1)启动,此时还未取号

2)叫号程序显示过程

取号程序:

1)启动

2)一共取了6个号码

四.课程设计小结

银行叫号系统是一个典型的网络编程实验,它包括了一个服务器、一个取号程序、一个叫号程序以及两个业务程序。在这个系统中,服务器起着核心的管理和通知作用,取号程序负责向服务器请求号码并确保唯一性,业务程序负责获取下一个可办理业务的号码并显示给客户,而叫号程序则负责实时显示当前正在办理的业务号码。

在实验中我遇到了很多问题。最开始向老师请教了关于服务器的运行逻辑。发现服务器和客户端需要用类似协议报头的功能实现客户端发送过来的信息能够让服务器接收并分的清楚是哪个客户端发送过来的请求,处理逻辑后返回给相应的客户端。通过与老师的交流,我明确了服务器的核心功能,即确保号码的唯一性、处理取号程序和业务程序的请求,并通知叫号程序。

在之后的代码实现过程中,我还向老师请教了取号程序的运行逻辑。他发现取号程序需要通过服务器获取号码,并且需要保证号码的唯一性。在与老师的讨论中,我了解到取号程序需要与服务器进行通信,以确保号码的唯一性,同时及时获取新的号码并显示给客户。

另外,还向老师请教了关于业务程序的运行逻辑。他发现业务程序需要通过服务器获取下一个可办理业务的号码,并显示给客户。通过和老师的交流,我明白了业务程序需要与服务器进行交互,以获取下一个可办理业务的号码,并且需要与另一个业务程序进行交叉测试,及时改正了自己的代码,将交叉运行的处理逻辑放在了服务器中,而不是业务程序,降低了代码的逻辑复杂度和耦合度

最后,我还向老师请教了叫号程序的运行逻辑。最后通过自己的思考,想到了设置flag来实现判断号码的处理逻辑。

通过多次向老师请教,我逐渐理解了整个系统的运行逻辑,提高了自己的网络编程水平,实现了自己的程序的功能,并且在不断改代码和问代码的过程中,我自己的代码逻辑性和程序的理解能力都得到了非常大的提高。让我为以后工作中的代码设计能力,网络编程应用能力以及编程安全能力都有了非常大的帮助

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值