环境需求:
- 客户端和服务器均已编译安装f-stack,dpdk接管的网口网络通信OK。安装链接:https://github.com/F-Stack/f-stack
- 客户端绑定本地网口192.168.15.200,向服务器192.168.15.201建立tcp链路
- 服务器192.168.15.201监听tcp的80号端口
- 客户端/服务器代码放在f-stack的example路径下编译执行
客户端:
/*
* 通过 shell 执行
* ./tcp_client --conf /etc/f-stack.conf --proc-type=primary &
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include "ff_config.h"
#include "ff_api.h"
#define MAX_EVENTS 512
/* kevent set */
struct kevent kevSet[2];
/* events */
struct kevent events[MAX_EVENTS];
/* kq */
int kq;
int sockfd;
#ifdef INET6
int sockfd6;
#endif
int loop(void *arg)
{
/* Wait for events to happen */
unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);
unsigned i;
for (i = 0; i < nevents; ++i) {
struct kevent event = events[i];
int clientfd = (int)event.ident;
/* Handle disconnect */
if (event.flags & EV_EOF) {
/* Simply close socket */
ff_close(clientfd);
printf("#### close clientfd=%d\n",clientfd);
}
if (clientfd == sockfd) {
if (event.filter == EVFILT_READ) {
char buf[256];
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
if(readlen > 0){
printf("#### client recv data:%s\n",buf);
}
}
if (event.filter == EVFILT_WRITE) {
char wr_buf[25]="data send to server";
ff_write(clientfd, wr_buf, strlen(wr_buf));
}
}
}
}
int main(int argc, char * argv[])
{
ff_init(argc, argv);
assert((kq = ff_kqueue()) > 0);
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("192.168.56.201");
server_addr.sin_port = htons(80);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(8000);
my_addr.sin_addr.s_addr = inet_addr("192.168.56.200");
int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
ret = ff_connect(sockfd,(struct linux_sockaddr *)&server_addr,sizeof(server_addr));
if (ret < 0 && errno != EPERM) {
printf("ff_connect failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
EV_SET(&kevSet[0], sockfd, EVFILT_READ , EV_ADD, 0, MAX_EVENTS, NULL);
EV_SET(&kevSet[1], sockfd, EVFILT_WRITE, EV_ADD, 0, MAX_EVENTS, NULL);
/* Update kqueue */
ff_kevent(kq, kevSet, 2, NULL, 0, NULL);
ff_run(loop, NULL);
return 0;
}
服务器:
/*
* 通过 shell 执行
* ./tcp_server --conf /etc/f-stack.conf --proc-type=primary &
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include "ff_config.h"
#include "ff_api.h"
#define MAX_EVENTS 512
/* kevent set */
struct kevent kevSet[2];
/* events */
struct kevent events[MAX_EVENTS];
/* kq */
int kq;
int sockfd;
#ifdef INET6
int sockfd6;
#endif
int loop(void *arg)
{
/* Wait for events to happen */
unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);
unsigned i;
for (i = 0; i < nevents; ++i) {
struct kevent event = events[i];
int clientfd = (int)event.ident;
/* Handle disconnect */
if (event.flags & EV_EOF) {
/* Simply close socket */
ff_close(clientfd);
} else if (clientfd == sockfd) {
int available = (int)event.data;
do {
int nclientfd = ff_accept(clientfd, NULL, NULL);
if (nclientfd < 0) {
printf("ff_accept failed:%d, %s\n", errno,
strerror(errno));
break;
}
/* Add to event list */
EV_SET(&kevSet[0], nclientfd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
EV_SET(&kevSet[1], nclientfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
if(ff_kevent(kq, kevSet, 2, NULL, 0, NULL) < 0) {
printf("ff_kevent error:%d, %s\n", errno,
strerror(errno));
return -1;
}
available--;
} while (available);
} else if (event.filter == EVFILT_READ) {
char buf[256];
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
if(readlen > 0){
printf("#### server recv datas:%s,len=%d\n",buf,readlen);
}
} else if (event.filter == EVFILT_WRITE) {
char wr_buf[25]="data send to client";
ff_write(clientfd, wr_buf, strlen(wr_buf));
} else {
printf("unknown event: %8.8X\n", event.flags);
}
}
}
int main(int argc, char * argv[])
{
ff_init(argc, argv);
assert((kq = ff_kqueue()) > 0);
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
ret = ff_listen(sockfd, MAX_EVENTS);
if (ret < 0) {
printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
exit(1);
}
EV_SET(&kevSet[0], sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
/* Update kqueue */
ff_kevent(kq, &kevSet[0], 1, NULL, 0, NULL);
ff_run(loop, NULL);
return 0;
}
|