Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3105613
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-06-15 17:34:03

#include
#include
#include
#include
#include
#include

#include
#include
#include

#include
#include
#include
#include
#include


int sockfd;
int tcp_connect_server(const char* server_ip, int port);


void cmd_msg_cb(int fd, short events, void* arg);
void socket_read_cb(int fd, short events, void *arg);
void send_data(void);
void event_handle(void);
struct timeval lasttime;
static int event_is_persistent=1;

static void timeout_cb(int sock, short events, void *arg) {
        
    
//    

        if(event_is_persistent) {
                
                struct timeval tv;
                
                tv.tv_sec = 1;
                event_add( *((struct event**)arg), &tv);
        }  
        send_data();
 
}

int init_tcp(char *add,int port)
{
    //两个参数依次是服务器端的IP地址、端口号
     sockfd = tcp_connect_server(add, port);
    if( sockfd == -1)
    {
        perror("tcp_connect error ");
        return -1;
    }

    printf("connect to server successful\n");
    return 0;

}
void event_handle(void)
{
    struct event_base* base = event_base_new();

    struct event *ev_sockfd = event_new(base, sockfd,
                                        EV_READ | EV_PERSIST,
                                        socket_read_cb, NULL);
    event_add(ev_sockfd, NULL);

    //监听终端输入事件
    struct event* ev_cmd = event_new(base, STDIN_FILENO,
                                      EV_READ | EV_PERSIST, cmd_msg_cb,
                                      (void*)&sockfd);


    event_add(ev_cmd, NULL);
    
    struct event *timer_ev=NULL;
    struct timeval tv;
    timer_ev=evtimer_new(base,timeout_cb,(void *)&timer_ev);
    memset(&tv,0,sizeof(tv));
    tv.tv_sec=1;
    evtimer_add(timer_ev,&tv);

    event_base_dispatch(base);

}
void timer_handler (int signum)
{
    
  //  event_handle();
    printf("trigger...\n");
}
int main(int argc, char** argv)
{
    struct sigaction sa;
    struct itimerval timer;

    /* Install timer_handler as the signal handler for SIGVTALRM. */
    memset(&sa, 0, sizeof (sa));
    sa.sa_handler = &timer_handler;
    sigaction (SIGVTALRM, &sa, NULL);

    /* Configure the timer to expire after 100 msec... */
    timer.it_value.tv_sec = 5;
    timer.it_value.tv_usec = 0;
    /* ... and every 100 msec after that. */
    timer.it_interval.tv_sec = 5;
    timer.it_interval.tv_usec = 0;
    /* Start a virtual timer. It counts down whenever this process is
    executing. */

    setitimer (ITIMER_VIRTUAL, &timer, NULL);
  //  init_tcp("192.168.1.250",9501);
      init_tcp("127.0.0.1",9876);
        
  //  init_tcp("192.168.18.132",3000);
    event_handle();
/*
    struct event_base* base = event_base_new();

    struct event *ev_sockfd = event_new(base, sockfd,
                                        EV_READ | EV_PERSIST,
                                        socket_read_cb, NULL);
    event_add(ev_sockfd, NULL);

    //监听终端输入事件
    struct event* ev_cmd = event_new(base, STDIN_FILENO,
                                      EV_READ | EV_PERSIST, cmd_msg_cb,
                                      (void*)&sockfd);


    event_add(ev_cmd, NULL);
    
    struct event *timer_ev=NULL;
    struct timeval tv;
    timer_ev=evtimer_new(base,timeout_cb,(void *)&timer_ev);
    memset(&tv,0,sizeof(tv));
    tv.tv_sec=1;
    evtimer_add(timer_ev,&tv);

    event_base_dispatch(base);
*/
       
            //
        
    printf("finished \n");
    return 0;
}
void send_data(void)
{
   char msg[1024]={"1,1001,1.2,3.4,-3.2,11,0,0,0,0,2018-06-14 16:40:34\r\n"};
   write(sockfd, msg, strlen(msg));

}





void cmd_msg_cb(int fd, short events, void* arg)
{
    char msg[1024];

    int ret = read(fd, msg, sizeof(msg));
    if( ret <= 0 )
    {
        perror("read fail ");
        exit(1);
    }

     sockfd = *((int*)arg);

    //把终端的消息发送给服务器端
    //为了简单起见,不考虑写一半数据的情况
    write(sockfd, msg, ret);
}


void socket_read_cb(int fd, short events, void *arg)
{
    char msg[1024];

    //为了简单起见,不考虑读一半数据的情况
    int len = read(fd, msg, sizeof(msg)-1);
    if( len <= 0 )
    {
       // perror("read fail ");
        exit(1);
    }
    msg[len] = '\0';
        if(0==strcmp(msg,"OK"))
    {
        close(sockfd);
        sleep(5);
    init_tcp("192.168.1.250",9501);
        event_handle();
          printf("recv %s from server\n", msg);
    }
     
    // printf("recv %s from server\n", msg);
    

    
}



typedef struct sockaddr SA;
int tcp_connect_server(const char* server_ip, int port)
{
    int  status, save_errno;
    struct sockaddr_in server_addr;

    memset(&server_addr, 0, sizeof(server_addr) );

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    status = inet_aton(server_ip, &server_addr.sin_addr);

    if( status == 0 ) //the server_ip is not valid value
    {
        errno = EINVAL;
        return -1;
    }

    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if( sockfd == -1 )
        return sockfd;


    status = connect(sockfd, (SA*)&server_addr, sizeof(server_addr) );

    if( status == -1 )
    {
        save_errno = errno;
        close(sockfd);
        errno = save_errno; //the close may be error
        return -1;
    }

    evutil_make_socket_nonblocking(sockfd);

    return sockfd;
}

阅读(1718) | 评论(0) | 转发(0) |
0

上一篇:QT tableview例子

下一篇:libevent client

给主人留下些什么吧!~~