23,120
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
class Server {
public:
Server(void);
~Server(void);
void server_create(const char* ip = "0.0.0.0", const char* port = "80");
void server_start(void);
void server_stop(void);
private:
static int listen_fd;
};
int Server::listen_fd = 0;
Server::Server(void)
{
}
Server::~Server(void)
{
if (listen_fd > 0)
{
close(listen_fd);
}
}
void Server::server_create(const char* ip, const char* port)
{
int opt = 1;
struct sockaddr_in serv;
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
memset(&serv, 0x00, sizeof(serv));
serv.sin_family = AF_INET;
inet_aton(ip, &serv.sin_addr);
serv.sin_port = htons(atoi(port));
bind(listen_fd, (struct sockaddr *)&serv, sizeof(serv));
listen(listen_fd, 5);
}
void* routine(void* arg)
{
int apt_fd;
struct sockaddr_in client;
socklen_t len = sizeof(client);
int listen_fd = *(int*)arg;
char buf[4096] = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHELLO";
free(arg);
while (1)
{
apt_fd = accept(listen_fd, (struct sockaddr*)&client, &len);
if (apt_fd < 0)
{
perror("accept");
break;
}
recv(apt_fd, buf + 1024, 4096 - 1024, 0);
send(apt_fd, buf, strlen(buf), 0);
close(apt_fd);
}
return NULL;
}
void Server::server_start(void)
{
pthread_t pid;
int *arg = (int*)malloc(sizeof(int));
*arg = listen_fd;
pthread_create(&pid, NULL, routine, arg);
pthread_join(pid, NULL);
}
void Server::server_stop(void)
{
close(listen_fd);
}
void sighandler(int arg)
{
Server srv;
srv.server_stop();
}
void* routine_server(void* arg)
{
Server srv;
srv.server_create();
srv.server_start();
return NULL;
}
int main(void)
{
pthread_t pid;
signal(SIGINT, sighandler);
pthread_create(&pid, NULL, routine_server, NULL);
pthread_join(pid, NULL);
return 0
[/qu
也不行额,我把你代码测试了下,ctrl+c之后,程序卡死,没有输出也没有退出,说明accept没有返回。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
class Server {
public:
Server(void);
~Server(void);
void server_create(const char* ip = "0.0.0.0", const char* port = "80");
void server_start(void);
void server_stop(void);
private:
static int listen_fd;
};
int Server::listen_fd = 0;
Server::Server(void)
{
}
Server::~Server(void)
{
if (listen_fd > 0)
{
close(listen_fd);
}
}
void Server::server_create(const char* ip, const char* port)
{
int opt = 1;
struct sockaddr_in serv;
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
memset(&serv, 0x00, sizeof(serv));
serv.sin_family = AF_INET;
inet_aton(ip, &serv.sin_addr);
serv.sin_port = htons(atoi(port));
bind(listen_fd, (struct sockaddr *)&serv, sizeof(serv));
listen(listen_fd, 5);
}
void* routine(void* arg)
{
int apt_fd;
struct sockaddr_in client;
socklen_t len = sizeof(client);
int listen_fd = *(int*)arg;
char buf[4096] = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHELLO";
free(arg);
while (1)
{
apt_fd = accept(listen_fd, (struct sockaddr*)&client, &len);
if (apt_fd < 0)
{
perror("accept");
break;
}
recv(apt_fd, buf + 1024, 4096 - 1024, 0);
send(apt_fd, buf, strlen(buf), 0);
close(apt_fd);
}
return NULL;
}
void Server::server_start(void)
{
pthread_t pid;
int *arg = (int*)malloc(sizeof(int));
*arg = listen_fd;
pthread_create(&pid, NULL, routine, arg);
pthread_join(pid, NULL);
}
void Server::server_stop(void)
{
close(listen_fd);
}
void sighandler(int arg)
{
Server srv;
srv.server_stop();
}
void* routine_server(void* arg)
{
Server srv;
srv.server_create();
srv.server_start();
return NULL;
}
int main(void)
{
pthread_t pid;
signal(SIGINT, sighandler);
pthread_create(&pid, NULL, routine_server, NULL);
pthread_join(pid, NULL);
return 0