windows和linux互通收发
- 一、windows的udp客户端代码
- 1、代码剖析
- 2、总体代码
- 二、linux服务器代码
- 三、成果展示
一、windows的udp客户端代码
1、代码剖析
首先我们需要包含头文件以及lib的一个库:
#include <iostream>
#include <WinSock2.h>
#include <string>
#pragma comment(lib,"ws2_32.lib")
然后我们需要启动windows的套接字,并且对winsocket进行初始化:
int main()
{WSAData wsd;//启动Winsock//进行Winsocket的初始化,windows初始化socket网络库,申请2.2的版本if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){cout << "WSAStartup Error =" << WSAGetLastError() << endl;return 0;}else{cout << "WSAStartup Success" << endl;}
}
startup就是启动的接口,里面的参数的意思是:初始化socket网络库,申请2.2的版本。如果startup这个函数的返回值等于0就说明启动成功,否则就启动失败我们就打印一下。
以上是不同的方面,下面是用的linux一套:
创建套接字:
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);if (sock == SOCKET_ERROR){cout << "socket ERROR = " << WSAGetLastError() << endl;return 1;}else{cout << "socket success" << endl;}
客户端收发消息:
struct sockaddr_in server;memset(&server, 0, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(serverport);server.sin_addr.s_addr = inet_addr(serverip.c_str());string line;while (true){cout << "Please Enter# ";getline(cin, line);int n = sendto(sock, line.c_str(), line.size(), 0, (struct sockaddr*)&server, sizeof(server));if (n < 0){cerr << "sendto error" << endl;break;}//接收服务器的数据char buffer[1024];struct sockaddr_in client;int len = sizeof(client);n = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&client, &len);if (n >= 0){buffer[n] = 0;}cout << "[server echo]: " << buffer << endl;}
关闭套接字和网络服务:
closesocket(sock);WSACleanup();
这里会出现这个问题,有两种解决方法:
第一种解决方法是:#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
第二种解决方法是:#pragma warning(disable:4996)这个仅仅是屏蔽了这某一个错误
2、总体代码
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
#include <iostream>
#include <WinSock2.h>
#include <string>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
uint16_t serverport = ; // 这里用随便的端口号
std::string serverip = ; // 这里用自己的云服务器的ip
int main()
{WSAData wsd;// 启动Winsock// 进行Winsocket的初始化,windows初始化socket网络库,申请2.2的版本if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){cout << "WSAStartup Error =" << WSAGetLastError() << endl;return 0;}else{cout << "WSAStartup Success" << endl;}SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);if (sock == SOCKET_ERROR){cout << "socket ERROR = " << WSAGetLastError() << endl;return 1;}else{cout << "socket success" << endl;}struct sockaddr_in server;memset(&server, 0, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(serverport);server.sin_addr.s_addr = inet_addr(serverip.c_str());string line;while (true){cout << "Please Enter# ";getline(cin, line);int n = sendto(sock, line.c_str(), line.size(), 0, (struct sockaddr*)&server, sizeof(server));if (n < 0){cerr << "sendto error" << endl;break;}//接收服务器的数据char buffer[1024];struct sockaddr_in client;int len = sizeof(client);n = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&client, &len);if (n >= 0){buffer[n] = 0;}cout << "[server echo]: " << buffer << endl;}closesocket(sock);WSACleanup();return 0;
}
二、linux服务器代码
main.cc
#include "udp.hpp"
#include "Log.hpp"
#include <memory>
#include <cstdio>
#include <vector>Log log;void Usage(std::string proc)
{std::cout << "\n\rUsages: " << proc << "port[1024+]\n" << std::endl;
}std::string Handler(const std::string& str)
{std::string res = "recv a message# ";res += str;std::cout << res << std::endl;return res;
}bool SafeCheck(const std::string& cmd)
{std::vector<std::string> word_key = {"rm","top","cp","yum","while","kill","unlink""uninstall","top"};for (auto &word : word_key){auto pos = cmd.find(word);if (pos != std::string::npos){return false;}}return true;
}std::string ExcuteCommand(const std::string& cmd)
{std::cout << "get a massage:" << cmd << std::endl;// 做一个保护if (!SafeCheck(cmd)) return "bad man";FILE* fp = popen(cmd.c_str(), "r"); // 管道创建好,子进程创建好,子进程通过管道放到父进程if (nullptr == fp){perror("popen failed");return "error";}std::string result;char buffer[4096];while (true){char* ok = fgets(buffer, sizeof(buffer), fp); // 写到buffer缓冲区中if (ok == nullptr){break;}result += buffer;}pclose(fp);return result;
}// 以后用的是./udpserver + port
int main(int argc, char *argv[])
{if (argc != 2){Usage(argv[0]);exit(0);}uint16_t port = std::stoi(argv[1]);std::unique_ptr<UdpServer> svr(new UdpServer(port)); // new一个对象svr->Init(); // 初始化svr->Run(ExcuteCommand); // 跑起来
}
udp.hpp:
#pragma once #include <iostream>
#include <string>
#include <cstring>
#include <functional>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Log.hpp"using func_t = std::function<std::string(const std::string&)>; // 将返回值为string,参数为const string&的函数包装起来extern Log log;uint16_t defaultport = 8080;
std::string defaultip = "0.0.0.0";enum
{SOCKET_ERR=1,BIND_ERR
};class UdpServer
{
public:// 构造函数UdpServer(const uint16_t &port = defaultport, const std::string &ip = defaultip): _socketfd(0), _port(port), _ip(ip), _isrunning(false){}void Init(){// 1.创建udp套接字socket_socketfd = socket(AF_INET, SOCK_DGRAM, 0);// 创建失败if (_socketfd < 0){log(Fatal, "socket create error,socketfd:%d", _socketfd);exit(SOCKET_ERR);}// 创建成功log(Info, "socket create sucess,socketfd:%d", _socketfd);// 2.绑定端口号bind socketstruct sockaddr_in local; // 网络套接字结构体bzero(&local, sizeof(local)); // 将该套接字结构体对象全部清零local.sin_family = AF_INET; // 类型:ipv4local.sin_port = htons(_port); // 端口号:是在网络中来回发送的,我发过去要让对面知道我发的端口号是什么,所以必须是网络字节序列local.sin_addr.s_addr = inet_addr(_ip.c_str()); // 1.string->unit_32 2.来回通信对方要知道发送的ip,所以ip的unit_32必须是网络序列的int n = bind(_socketfd, (const struct sockaddr *)&local, sizeof(local));if (n < 0){log(Fatal, "bind error, erron:%d, errno string:%s", errno, strerror(errno));exit(BIND_ERR);}log(Info, "bind sucess");}void Run(func_t func) // 对代码进行分层{_isrunning = true;char inbuffer[1024];while (_isrunning){struct sockaddr_in client;socklen_t len = sizeof(client);ssize_t n = recvfrom(_socketfd, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&client, &len);if (n < 0){log(Warning, "recvfrom error");continue;}// 简单的数据处理一下inbuffer[n] = 0;std::string info = inbuffer;std::string echo_string = func(info); // 回调函数,将处理的结果用外部回调函数去处理一下// 回调函数也叫钩子函数,相当于一个钩子等待鱼儿上钩,到这个回调函数info传进参数也就是有鱼上钩的// 时候就进行传参并进行外部处理,我们只需要在main.cc文件中封装一个函数进行处理这个拼接的函数即可sendto(_socketfd, echo_string.c_str(), echo_string.size(), 0, (const struct sockaddr*)&client, len);}}// 析构函数~UdpServer(){if (_socketfd > 0) {close(_socketfd);}}
private:int _socketfd; // 网络文件描述符,表示socket返回的文件描述符uint16_t _port; // 表明服务器进程的端口号std::string _ip; // ip地址,任意地址绑定为0bool _isrunning; // 判断是否运行
};