欢迎来到别爱的CSDN
今天我们主要来实现TCP并发网络编程,有2种方法,分别是一请求一线程的方法,还有一种就是io多路复用epoll的方法。
提示:以下是本篇文章正文内容,下面案例可供参考
主要是多个客户端,一个服务器,多个客户端可以发送请求给服务器,服务端接受并显示出来。
我们下面就用2种方法来分别实现这个功能。
代码如下(示例):
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_LENGTH 1024
#define EPOLL_SIZE 1024
void* client_routine(void* arg) {
int clientfd = *(int*)arg;
while (1) {
char buffer[BUFFER_LENGTH] = { 0 };
int len = recv(clientfd, buffer, BUFFER_LENGTH, 0);
if (len < 0) {
close(clientfd);
break;
}
else if (len == 0) {
close(clientfd);
break;
}
else {
printf("Recv: %s, %d byte(s)\n", buffer, len);
}
}
}
int main(int argc,char* argv[]) {
if (argc < 2) {
printf("Parm Error\n");
return -1;
}
int port = atoi(argv[1]);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0) {
perror("bind");
return 2;
}
if (listen(sockfd, 5) < 0) {
perror("listen");
return 3;
}
while (1) {
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr_in));
socklen_t client_len = sizeof(client_addr);
int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);
pthread_t thread_id;
pthread_create(&thread_id, NULL, client_routine, &clientfd);
}
return 0;
}
代码如下(示例):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_LENGTH 1024
#define EPOLL_SIZE 1024
int main(int argc,char* argv[]) {
if (argc < 2) {
printf("Parm Error\n");
return -1;
}
int port = atoi(argv[1]);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0) {
perror("bind");
return 2;
}
if (listen(sockfd, 5) < 0) {
perror("listen");
return 3;
}
int epfd = epoll_create(1);
struct epoll_event events[EPOLL_SIZE] = { 0 };
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = sockfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
while (1) {
int nready = epoll_wait(epfd, events, EPOLL_SIZE, 5);
if (nready == -1) continue;
for (int i = 0; i < nready; ++i) {
if (events[i].data.fd == sockfd) {//listen
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr_in));
socklen_t client_len = sizeof(client_addr);
int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, clientfd, &ev);
}
else {
int clientfd = events[i].data.fd;
char buffer[BUFFER_LENGTH] = { 0 };
int len = recv(clientfd, buffer, BUFFER_LENGTH, 0);
if (len < 0) {
close(clientfd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
epoll_ctl(epfd, EPOLL_CTL_DEL, clientfd, &ev);
}
else if (len == 0) {
close(clientfd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
epoll_ctl(epfd, EPOLL_CTL_DEL, clientfd, &ev);
}
else {
printf("Recv: %s, %d byte(s)\n", buffer, len);
}
}
}
}
return 0;
}
今天就主要给大家带来这些内容了,希望大家可以尝试自己去解决这些问题,并且成功实现。
C/C++ Linux高级开发课程