0


使用UDP协议实现—翻译服务器

前言

  1. 上一篇文章中,我们使用UDP协议编码完成了一个简单的服务器,实现数据通信,服务器设计出来后目的不仅仅只是实现数据通信,而是根据客户端发过来的请求,实现一定的需求,今天我们要介绍的是当客户端给服务端发送英文单词,然后服务端获取客户端的请求,将翻译结果返回给客户端,通过这样的方式,实现了一款英文翻译服务器。下面我们就一起具体来看看是如何编码完成。

1.设计思路:

如图所示

第一步:启动服务器,然后服务器加载词库

第二步:客户端向服务器,发送请求

第三步:服务器处理请求查找单词,将查找结果返回给客户端

第四步:客户端获取查询结果

2.词库设计

说明:在这里只是简单模拟实现一个词库,主要是实现业务逻辑

dict.txt:

  1. aunt:姨母
  2. brother:兄弟
  3. cousin:堂兄弟
  4. couple:夫妇
  5. dad:爸爸
  6. daughter:女儿
  7. family:家
  8. father:爸爸
  9. grandchild:孙子
  10. granddaughger:孙女
  11. grandfather:祖父
  12. grandma:外婆
  13. grandpa:外公
  14. granny 老奶奶

3.设计客户端

udpClient.hpp

  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <strings.h>
  5. #include <cerrno>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <functional>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. namespace Client
  15. {
  16. using namespace std;
  17. class udpClient
  18. {
  19. public:
  20. udpClient(const string &serverIp, const uint16_t serverPort)
  21. : _serverIp(serverIp), _serverPort(serverPort), _sockfd(-1) {}
  22. void initClient()
  23. {
  24. _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  25. if (_sockfd == -1)
  26. {
  27. cerr << "socket error:" << errno << strerror(errno) << endl;
  28. exit(2);
  29. }
  30. }
  31. void run()
  32. {
  33. struct sockaddr_in server;
  34. memset(&server, 0, sizeof(server));
  35. server.sin_family = AF_INET;
  36. server.sin_addr.s_addr = inet_addr(_serverIp.c_str());
  37. server.sin_port = htons(_serverPort);
  38. while (1)
  39. {
  40. string message;
  41. cout << "请输入你想要翻译的单词:";
  42. getline(cin,message);
  43. //发送请求
  44. sendto(_sockfd, message.c_str(), message.size(), 0, (const struct sockaddr *)&server, sizeof(server));
  45. char buffer[1024];
  46. struct sockaddr_in temp;
  47. socklen_t len = sizeof(temp);
  48. //接受查询翻译结果
  49. size_t n = recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&temp, &len);
  50. if (n >= 0)
  51. buffer[n] = 0;
  52. cout << "翻译的结果为: " << buffer << endl;
  53. }
  54. }
  55. private:
  56. string _serverIp;
  57. int _sockfd;
  58. uint16_t _serverPort;
  59. };
  60. }

udpClient.cc:启动客户端

  1. #include"udpClient.hpp"
  2. #include<memory>
  3. using namespace Client;
  4. static void Usage(string proc)
  5. {
  6. cout << "\nUsage:\n\t" << proc << " server_ip server_port\n\n";
  7. }
  8. int main(int argc,char* argv[])
  9. {
  10. if(argc != 3)
  11. {
  12. Usage(argv[0]);
  13. exit(1);
  14. }
  15. string serverip = argv[1];
  16. uint16_t serverport = atoi(argv[2]);
  17. unique_ptr<udpClient> uct(new udpClient(serverip,serverport));
  18. uct->initClient();
  19. uct->run();
  20. return 0;
  21. }

4.设计服务端

udpServer.hpp

  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <strings.h>
  5. #include <cerrno>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <functional>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. namespace Server
  15. {
  16. using namespace std;
  17. const static string defaultIP = "0.0.0.0";
  18. enum {USAGE_ERR = 1, SOCKET_ERR, BIND_ERR,OPEN_ERR};
  19. typedef function<void(int,string,uint16_t,string)> func_t;
  20. class udpServer
  21. {
  22. public:
  23. udpServer(const func_t& cb,uint16_t port, const string &ip = defaultIP)
  24. :_callback(cb),_port(port),_ip(ip),_sockfd(-1)
  25. {}
  26. void initServer()
  27. {
  28. _sockfd = socket(AF_INET,SOCK_DGRAM,0);
  29. if(_sockfd == -1)
  30. {
  31. cerr<<"socket error:" << errno << strerror(errno) << endl;
  32. exit(SOCKET_ERR);
  33. }
  34. struct sockaddr_in local;
  35. bzero(&local,sizeof(local));
  36. local.sin_family = AF_INET;
  37. local.sin_port = htons(_port);
  38. local.sin_addr.s_addr = htonl(INADDR_ANY);
  39. int n = bind(_sockfd,(struct sockaddr*)&local,sizeof(local));
  40. if(n == -1)
  41. {
  42. cerr<<"bind error:" << errno << strerror(errno) << endl;
  43. exit(BIND_ERR);
  44. }
  45. }
  46. void startServer()
  47. {
  48. char buffer[1024];
  49. for(;;)
  50. {
  51. struct sockaddr_in peer;
  52. socklen_t len = sizeof(peer);
  53. ssize_t s = recvfrom(_sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&peer,&len);
  54. if(s)
  55. {
  56. buffer[s] = { 0 };
  57. string clientIp = inet_ntoa(peer.sin_addr);
  58. uint16_t clientPort = ntohs(peer.sin_port);
  59. string message = buffer;
  60. cout << clientIp << "[" << clientPort << "]" << message << endl;
  61. //服务器只负责接受数据,处理方法采用回调的方式交给上层处理
  62. _callback(_sockfd,clientIp,clientPort,message);
  63. }
  64. }
  65. }
  66. ~udpServer()
  67. {}
  68. private:
  69. uint16_t _port;
  70. string _ip;
  71. int _sockfd;
  72. func_t _callback;
  73. };
  74. }

udpServer.cc:启动服务端

  1. #include "udpServer.hpp"
  2. #include <memory>
  3. #include <unordered_map>
  4. #include <fstream>
  5. using namespace Server;
  6. static void Usage(string proc)
  7. {
  8. cout << "\nUsage:\n\t" << proc << " local_port\n\n";
  9. }
  10. const string DictTxt = "./dict.txt";
  11. unordered_map<string,string> dict;
  12. static bool cutString(string& str,string& s1,string& s2,const string& sep)
  13. {
  14. auto pos = str.find(sep);
  15. if(pos == string::npos)
  16. return false;
  17. s1 = str.substr(0,pos);
  18. s2 = str.substr(pos + sep.size());
  19. return true;
  20. }
  21. static void initDict()
  22. {
  23. ifstream in(DictTxt,ios::binary);
  24. if(!in.is_open())
  25. {
  26. cerr << "open fail:" << DictTxt << "error" << endl;
  27. exit(OPEN_ERR);
  28. }
  29. string line;
  30. string key,value;
  31. while(getline(in,line))
  32. {
  33. if(cutString(line,key,value,":"))
  34. {
  35. dict.insert(make_pair(key,value));
  36. }
  37. }
  38. in.close();
  39. cout << "load dict success" << endl;
  40. }
  41. //翻译:
  42. void TranslationWord(int sockfd,string clientIp,uint16_t clientPort,string message)
  43. {
  44. string response_message;
  45. auto iter = dict.find(message);
  46. if(iter == dict.end())
  47. response_message = "unknown";
  48. else
  49. response_message = iter->second;
  50. struct sockaddr_in client;
  51. bzero(&client, sizeof(client));
  52. client.sin_family = AF_INET;
  53. client.sin_port = htons(clientPort);
  54. client.sin_addr.s_addr = inet_addr(clientIp.c_str());
  55. sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr*)&client, sizeof(client));
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. if (argc != 2)
  60. {
  61. Usage(argv[0]);
  62. exit(USAGE_ERR);
  63. }
  64. //加载词库
  65. initDict();
  66. uint16_t port = atoi(argv[1]);
  67. unique_ptr<udpServer> usvr(new udpServer(TranslationWord,port));
  68. usvr->initServer();
  69. usvr->startServer();
  70. return 0;
  71. }

5.编译客户端和服务端

makefile:

  1. .PHONY:all
  2. all:udpServer udpClient
  3. udpServer:udpServer.cc
  4. g++ -o $@ $^ -std=c++11
  5. udpClient:udpClient.cc
  6. g++ -o $@ $^ -std=c++11
  7. .PHONY:clean
  8. clean:
  9. rm -f udpServer udpClient

6.测试结果

如图所示:服务端能够准确处理客户端的请求,将翻译查询结果返回给客户端

7.总结

  1. 以上就是使用UDP协议实现的一款翻译服务器,细心的小伙伴也已经发现了,在上面的代码中服务器的任务只是接受请求,然后将请求的数据回调处理,让上层处理业务逻辑,这样的实现方式实现了服务器与业务逻辑代码之间的解耦,如果以后想实现一款别的需求的服务器,只需要更改上层的业务逻辑就可以了。
标签: udp 网络协议 网络

本文转载自: https://blog.csdn.net/qq_65307907/article/details/132164347
版权归原作者 终为nullptr 所有, 如有侵权,请联系我们删除。

“使用UDP协议实现—翻译服务器”的评论:

还没有评论