tcp_server.h
#include <unistd.h> #include <iostream> #include <sys/socket.h> #include <arpa/inet.h> #define MAXSIZE 1024 class tcp_server { private: int socket_fd,accept_fd; sockaddr_in myserver; sockaddr_in remote_addr; public: tcp_server(int listen_port); int recv_msg(); };
tcp_server.cpp
#include "tcp_server.h" tcp_server::tcp_server(int listen_port) { if(( socket_fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ){ throw "socket() failed"; } memset(&myserver,0,sizeof(myserver)); myserver.sin_family = AF_INET; myserver.sin_addr.s_addr = htonl(INADDR_ANY); myserver.sin_port = htons(listen_port); if( bind(socket_fd,(sockaddr*) &myserver,sizeof(myserver)) < 0 ) { throw "bind() failed"; } if( listen(socket_fd,10) < 0 ) { throw "listen() failed"; } } int tcp_server::recv_msg() { while( 1 ) { socklen_t sin_size = sizeof(struct sockaddr_in); if(( accept_fd = accept(socket_fd,(struct sockaddr*) &remote_addr,&sin_size)) == -1 ) { throw "Accept error!"; continue; } printf("Received a connection from %s\n",(char*) inet_ntoa(remote_addr.sin_addr)); if( !fork() ) { char buffer[MAXSIZE]; memset(buffer,0,MAXSIZE); if( ( read(accept_fd,buffer,MAXSIZE)) < 0 ) { throw("Read() error!"); } else { printf("Received message: %s\n",buffer); break; } exit(0); } close(accept_fd); } return 0; }
main.cpp
#include <iostream> #include <tcp_server.h> int main(int argc,char* argv[]) { tcp_server ts(atoi(argv[1])); ts.recv_msg(); return 0; }
编译
g++ -c tcp_server.cpp -o tcp_server.o g++ -g tcp_server.o main.cpp -I. -o main
运行
./main 9527
客户端测试
telnet server_ip 9527
服务器端响应
client(IP:192.168.99.1) connected.