💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 迭代服务器 ### server代码 ***** server.cpp ``` #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> #include <thread> #include <memory> #include <vector> #include <list> #include <mutex> class client { public: client(int fd, char* ip, unsigned short int port):socket_(fd), ip_(ip), port_(port) { memset(buf, 0, sizeof(buf)); read(); } void read() { while(true) { memset(buf, 0, sizeof(buf)); if (::read(socket_,buf,sizeof(buf)- 1) <= 0) break; printf("Message from client [%s:%d]: %s\n",ip_,port_,buf); write(socket_,buf,strlen(buf)); } } private: int socket_; char buf[100]; char* ip_; unsigned short int port_; }; class peerMgr { public: int acceptClient() { struct sockaddr_in client_addr; memset(&client_addr, 0, sizeof(client_addr)); socklen_t clnt_addr_size = sizeof(client_addr); int cli_sock; char logbuf[100] = "you had connected"; char buf[100] = {0}; while(true) { puts("Server accept wait\n"); if ((cli_sock = accept(serv_socket_, (struct sockaddr*) &client_addr, &clnt_addr_size)) <= 0) { printf("cli_sock : %d,error %d\n", strerror(cli_sock),errno); continue; } printf("Have one client accpet ip:%s:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); write(cli_sock,logbuf,strlen(logbuf)); std::shared_ptr<client> clients = std::make_shared<client>(cli_sock, inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); client_.push_back(clients); } } void start() { //创建套接字 serv_socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //将套接字和IP、端口绑定 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 serv_addr.sin_family = AF_INET; //使用IPv4地址 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 serv_addr.sin_port = htons(1234); //端口 bind(serv_socket_, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(serv_socket_, 20); thread_accept_.reset(new std::thread(std::bind(&peerMgr::acceptClient,this))); thread_accept_->join(); } private: std::list<std::shared_ptr<client>> client_; int serv_socket_; std::mutex mutex_; std::shared_ptr<std::thread> thread_accept_; }; int main() { std::shared_ptr<peerMgr> ac = std::make_shared<peerMgr>(); ac->start(); return 0; } ``` ### 客户端代码 ***** client.cpp ``` #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> int main(){ //创建套接字 int sock = socket(AF_INET, SOCK_STREAM, 0); //向服务器(特定的IP和端口)发起请求 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 serv_addr.sin_family = AF_INET; //使用IPv4地址 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 serv_addr.sin_port = htons(1234); //端口 connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); //读取服务器传回的数据 char buffer[40] = {0}; read(sock, buffer, sizeof(buffer)-1); printf("Message form server: %s\n", buffer); while(true) { memset(buffer, 0, sizeof(buffer)); puts("Input your contents"); scanf("%s",buffer); write(sock, buffer,strlen(buffer)); memset(buffer, 0, sizeof(buffer)); read(sock, buffer, sizeof(buffer)-1); printf("Message form server: %s\n", buffer); } //关闭套接字 close(sock); return 0; } ```