之前不会用nginx 就想过手动做一个端口转发的小程序,可以实现内网穿透,后来有了公网ip服务器之后就没有弄,这几天想弄了,就做了个简单的小程序,大神指点指点哈
话不多说直接上代码和效果
在这里插入代码片
server
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstdlib>
#include <thread>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
class Server {
private:
int _bufSize;
int _maxConnNum;
int _listenFD;
int _serverPort;
sockaddr_in _serverAddr;
int _clientPort;
sockaddr_in _clientAddr;
int _clientConn;
socklen_t _clientAddrLen;
public:
Server() {
this->_bufSize = 1024 * 16;
this->_maxConnNum = 1024;
this->_serverPort = 7005;
this->_listenFD = socket(AF_INET, SOCK_STREAM, 0);
if (this->_listenFD == -1) {
puts("Error: socket\n");
exit(-1);
}
this->_serverAddr.sin_family = AF_INET;
this->_serverAddr.sin_port = htons(this->_serverPort);
this->_serverAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(this->_listenFD, (struct sockaddr *) &this->_serverAddr, sizeof(struct sockaddr_in)) == -1) {
puts("Error: bind\n");
exit(-1);
}
if (listen(this->_listenFD, this->_maxConnNum) == -1) {
puts("Error: listen\n");
exit(-1);
}
puts("server start successfully");