pull/17/head
parent
5a249ad51d
commit
ea52d2645b
@ -1,96 +0,0 @@
|
||||
#include <winsock2.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
#define PORT 8888 // 监听的端口号
|
||||
#define KEY "secret_password" // 定义认证密钥
|
||||
|
||||
int main() {
|
||||
WSADATA wsaData;
|
||||
SOCKET sock, newSock;
|
||||
SOCKADDR_IN serverAddr, clientAddr;
|
||||
int cAddrLen = sizeof(clientAddr);
|
||||
char recvbuf[1024];
|
||||
char sendbuf[1024];
|
||||
|
||||
// 初始化Winsock
|
||||
WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||
|
||||
// 创建套接字
|
||||
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
std::cerr << "Could not create socket: " << WSAGetLastError() << std::endl;
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 设置服务器地址
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddr.sin_port = htons(PORT);
|
||||
|
||||
// 绑定套接字到地址
|
||||
if (bind(sock, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
|
||||
std::cerr << "Bind failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 开始监听
|
||||
if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
|
||||
std::cerr << "Listen failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Server listening on port " << PORT << "..." << std::endl;
|
||||
|
||||
// 接受连接
|
||||
newSock = accept(sock, (SOCKADDR*)&clientAddr, &cAddrLen);
|
||||
if (newSock == INVALID_SOCKET) {
|
||||
std::cerr << "Accept failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Connected with client." << std::endl;
|
||||
|
||||
// 发送密钥
|
||||
strcpy(sendbuf, KEY);
|
||||
if (send(newSock, sendbuf, strlen(sendbuf), 0) == SOCKET_ERROR) {
|
||||
std::cerr << "Send failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(newSock);
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 接收客户端响应的密钥
|
||||
int iResult = recv(newSock, recvbuf, sizeof(recvbuf), 0);
|
||||
if (iResult > 0) {
|
||||
recvbuf[iResult] = '\0';
|
||||
if (strcmp(recvbuf, KEY) == 0) {
|
||||
std::cout << "Authentication successful." << std::endl;
|
||||
// 认证成功后,可以接收客户端的消息或进行其他操作
|
||||
// ...
|
||||
} else {
|
||||
std::cerr << "Authentication failed." << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Receive failed with error: " << WSAGetLastError() << std::endl;
|
||||
}
|
||||
|
||||
// 关闭套接字
|
||||
closesocket(newSock);
|
||||
closesocket(sock);
|
||||
|
||||
// 清理Winsock
|
||||
WSACleanup();
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
// main.cpp
|
||||
#include <Poco/Net/HTTPServer.h>
|
||||
#include <Poco/Net/HTTPRequestHandler.h>
|
||||
#include <Poco/Net/HTTPRequestHandlerFactory.h>
|
||||
#include <Poco/Net/HTTPResponse.h>
|
||||
#include <Poco/Net/HTTPServerParams.h>
|
||||
#include <Poco/Net/ServerSocket.h>
|
||||
#include <Poco/JSON/JSON.h>
|
||||
#include <Poco/JSON/Parser.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Poco::Net;
|
||||
using namespace Poco::JSON;
|
||||
|
||||
class DataRequestHandler : public HTTPRequestHandler {
|
||||
public:
|
||||
void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp) override {
|
||||
// 设置响应的HTTP头
|
||||
resp.setContentType("application/json");
|
||||
|
||||
// 创建 JSON 对象
|
||||
Object::Ptr pObj = new Object();
|
||||
pObj->set("key", "value"); // 示例数据
|
||||
|
||||
// 将 JSON 对象转换为字符串
|
||||
String result;
|
||||
Poco::JSON::Stringifier::stringify(*pObj, result);
|
||||
|
||||
// 发送响应
|
||||
resp.send() << result;
|
||||
}
|
||||
};
|
||||
|
||||
class DataRequestHandlerFactory : public HTTPRequestHandlerFactory {
|
||||
public:
|
||||
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &req) override {
|
||||
// 检查请求的路径是否为 "/api/data"
|
||||
if (req.getURI() == "/api/data") {
|
||||
return new DataRequestHandler();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ServerSocket svs(8080);
|
||||
HTTPServerParams* pParams = new HTTPServerParams;
|
||||
pParams->setMaxQueued(100);
|
||||
pParams->setMaxThreads(16);
|
||||
HTTPServer srv(new DataRequestHandlerFactory(), svs, pParams);
|
||||
|
||||
std::cout << "Server started on port 8080" << std::endl;
|
||||
srv.start();
|
||||
|
||||
std::cin.get(); // 等待用户输入以结束服务器
|
||||
|
||||
srv.stop();
|
||||
return 0;
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define PORT 8080
|
||||
#define MAX_CLIENTS 5
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
// 声明 handle_client 函数
|
||||
void handle_client(int client_socket);
|
||||
|
||||
int main() {
|
||||
int server_fd, new_socket;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
char buffer[BUFFER_SIZE];
|
||||
int opt = 1;
|
||||
|
||||
// 创建套接字
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd < 0) {
|
||||
perror("socket creation failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// 设置选项,允许重用地址
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
|
||||
perror("setsockopt failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(PORT);
|
||||
|
||||
// 绑定
|
||||
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
perror("bind failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// 监听
|
||||
if (listen(server_fd, MAX_CLIENTS) < 0) {
|
||||
perror("listen failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "Server listening on port " << PORT << std::endl;
|
||||
|
||||
while (true) {
|
||||
client_len = sizeof(client_addr);
|
||||
new_socket = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
|
||||
if (new_socket < 0) {
|
||||
perror("accept failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "New client connected from " << inet_ntoa(client_addr.sin_addr) << " port " << ntohs(client_addr.sin_port) << std::endl;
|
||||
|
||||
// 创建并启动线程处理客户端
|
||||
std::thread(handle_client, new_socket).detach();
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 定义 handle_client 函数
|
||||
void handle_client(int client_socket) {
|
||||
while (true) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
memset(buffer, 0, BUFFER_SIZE); // 清空缓冲区
|
||||
int nbytes = read(client_socket, buffer, BUFFER_SIZE);
|
||||
if (nbytes <= 0) {
|
||||
std::cout << "Client disconnected." << std::endl;
|
||||
close(client_socket);
|
||||
break;
|
||||
}
|
||||
std::cout << "Received message from client: " << buffer << std::endl;
|
||||
// 这里可以添加更多的处理逻辑
|
||||
}
|
||||
}
|
Loading…
Reference in new issue