一.客户端类实现 流程:1.创建网络套接字 2.bind固定端口用于消息的接收 3.使网络套接字支持广播 4.创建两个标准线程用于消息的接收和发送 #include "Log.hpp" #include <cstring> #include <cstdlib> #include <iostream> #include <unistd.h> #include <strings.h> #include <pthread.h> #include <sys/types.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #define CLIENTIP "192.168.0.27" #define CLIENTPORT 8080 Log lg; enum { SOCKET_ERR = 1, BIND_ERR }; namespace black { struct ThreadData { int socket_fd; struct sockaddr_in client_other; };...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by class udpClient
使用UDP协议实现-翻译服务器_终为nullptr的博客-CSDN博客
目录 前言 7.总结 上一篇文章中,我们使用UDP协议编码完成了一个简单的服务器,实现数据通信,服务器设计出来后目的不仅仅只是实现数据通信,而是根据客户端发过来的请求,实现一定的需求,今天我们要介绍的是当客户端给服务端发送英文单词,然后服务端获取客户端的请求,将翻译结果返回给客户端,通过这样的方式,实现了一款英文翻译服务器。下面我们就一起具体来看看是如何编码完成。 如图所示 第一步:启动服务器,然后服务器加载词库 第二步:客户端向服务器,发送请求 第三步:服务器处理请求查找单词,将查找结果返回给客户端 第四步:客户端获取查询结果 说明:在这里只是简单模拟实现一个词库,主要是实现业务逻辑 dict.txt: aunt:姨母 brother:兄弟 cousin:堂兄弟 couple:夫妇 dad:爸爸 daughter:女儿 family:家 father:爸爸 grandchild:孙子 granddaughger:孙女 grandfather:祖父 grandma:外婆 grandpa:外公 granny 老奶奶 udpClient.hpp #pragma once...
简单的UDP网络程序·续写(后端服务器)_清风玉骨的博客-CSDN博客
目录 demo1 signal demo2 popen 效果 udpServer.cc udpServer.hpp udpClient.cc dict.txt demo3 精简 onlineUser.hpp udpClient.cc udpClient.hpp udpServer.cc udpServer.hpp 这个用于对发送过来的数据进行简单的翻译,采取文件读取的方式 先创建一个文件,注意在当前目录下建立一个 dict.txt文件放入一些待翻译的字符组,例如下面 初始化字典 获取数据 unordered_map<string, string> dict; // 一个字典 const std::string dictTxt = "./dict.txt"; static void initDict() { ifstream in(dictTxt, std::ios::binary); // 二进制形式打开一个文件 if (!in.is_open()) { std::cerr << "open file" << dictTxt << "error" << endl;...
【网络】UDP网络服务器简单模拟实现_模拟udp通信_平凡的人1的博客-CSDN博客
【网络】UDP网络服务器简单模拟实现 UDP的封装: 创建makefile文件: udpServer.cc 客户端进行调用的逻辑代码:构建udpServer的对象,然后进行初始化,在进行启动起来;调用逻辑如下: #include "udpServer.hpp" #include <memory> using namespace std; using namespace Server; static void Usage(string proc) { cout<<"\nUsage:\n\t"<<proc<<" local_port\n\n"; } // ./udpServer port int main(int argc,char*argv[]) { if(argc != 2) { Usage(argv[0]); exit(USAGE_ERR); } uint16_t port = atoi(argv[1]); std::unique_ptr<udpServer> usvr(new udpServer(port)); usvr->initServer();...
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with class
Contact class, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack