Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3759797
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2019-10-18 14:31:48

https://www.cnblogs.com/smallleiit/p/11646622.html
构建json串
cJSON * root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString(Name));
cJSON_AddItemToObject(root, "Ip", cJSON_CreateString(Ip));
text = cJSON_Print(root);


首先shell命令:
[root@controller /]# curl -X POST -H "Content-Type:application/json" -d '{"dpid": 60447901079364,"count": 5,"packets": [{"port": 2,"vlan ": 24,"mac": "6c:92:bf:e0:36:e7","ip": "1.1.1.1"},]}'
[root@controller /]#

int url_request(char *url, char *data, int size)
{
    int ret = -1;
    CURLcode res;
    CURL *curl_h = NULL;
    long retcode = 0;
    struct mem_curl_data msg = { NULL, 0};
    struct curl_slist *list = NULL;

    if(url == NULL || data == NULL){
        return -1;
    }
    curl_global_init(CURL_GLOBAL_ALL);
        
    /* init the curl session */
    curl_h = curl_easy_init();
    
    if(strncmp(url, "https", 5) == 0){
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYHOST, 0);
    }
    
    /* specify URL to get */
    curl_easy_setopt(curl_h, CURLOPT_URL, url);
    /* set curl head */
    list = curl_slist_append(list, "Content-Type: application/json");
    list = curl_slist_append(list, "Accept: */*");
    curl_easy_setopt(curl_h, CURLOPT_HTTPHEADER, list);
    /* send all data to this function  */
    curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, agent_curl_read_data);
    /* we pass our 'chunk' struct to the callback function */
    curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, (void *)&msg);
    /* get it! */
    res = curl_easy_perform(curl_h);
    curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &retcode);
    
    /* free cache */
    curl_slist_free_all(list);
    
    /* check for errors */
    if(res == CURLE_OK && retcode == 200) {
        ret = 0;
    } else {
        ret = -1;
    }
    
    if(msg.size <= size && msg.memory != NULL) {
        memset(data, 0x00, size);
        memcpy(data, msg.memory, msg.size);
        free(msg.memory);
    }
    
    curl_easy_cleanup(curl_h);
    
    curl_global_cleanup();
    
    return ret;
}

int url_reply(char *url, char *data, int size)
{
    struct curl_slist* headers = NULL;

    curl_global_init(CURL_GLOBAL_ALL);
    CURL *easy_handle = curl_easy_init();

/*
    if(strncmp(url, "https", 5) == 0){
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYHOST, 0);
    }
*/

    curl_easy_setopt(easy_handle, CURLOPT_URL, url);
    // 单个域post
    curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, data);

    headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
    curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(easy_handle, CURLOPT_POST, 1);

    curl_easy_perform(easy_handle);

    curl_easy_cleanup(easy_handle);
    curl_global_cleanup();

    return 0;
}

https: //blog.csdn.net/zhangxiao93/article/details/53575881


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 BUFFER_SIZE 512
#define BUFFER_SIZE 512

int Socket(int ,int,int);
void Bind(int ,const struct sockaddr*sa,socklen_t salen);
void Listen(int ,int);
int Accept(int,struct sockaddr*,socklen_t*);
void handleAccept(int);
void handleHttp(int);
int getRequest(int);


int main(int argc,char **argv)
{
    const int port = 1024;//listen port
    int listenfd=Socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in serverAddr;
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_addr.s_addr=INADDR_ANY;
    serverAddr.sin_port=htons(port);
    Bind(listenfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
    Listen(listenfd,5);

    while(1)
    {
        handleAccept(listenfd);
    }

}

int Socket(int family , int type,int protocol)
{
    int     n;
    if ( (n = socket(family, type, protocol)) < 0)
    {
        printf("socket error\r\n");
        return -1;
    }
    return(n);

}
void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
    if (bind(fd, sa, salen) < 0)
    {
        printf("bind error\r\n");
        exit(-1);
    }
}
void
Listen(int fd, int backlog)
{
    char    *ptr;

        /*4can override 2nd argument with environment variable */
    if ( (ptr = getenv("LISTENQ")) != NULL)
        backlog = atoi(ptr);

    if (listen(fd, backlog) < 0)
    {
        printf("listen error\r\n");
        return ;
    }
}
int
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
    int     n;

again:
    if ( (n = accept(fd, sa, salenptr)) < 0) {
#ifdef  EPROTO
        if (errno == EPROTO || errno == ECONNABORTED)
#else
        if (errno == ECONNABORTED)
#endif
            goto again;
        else
        {
            printf("accept error\r\n");
            return -1;
        }
    }
    return(n);
}

void handleAccept(int listenfd)
{
    struct sockaddr_in clientAddr;
    socklen_t clientLen=sizeof(clientAddr);
    int connfd=Accept(listenfd,(struct sockaddr *)&clientAddr,&clientLen);
    getRequest(connfd);
    close(connfd);
}

int getRequest(int socket)
{
    int msgLen=0;
    char buffer[BUFFER_SIZE];
    memset (buffer,'\0', BUFFER_SIZE);
    if ((msgLen = recv(socket, buffer, BUFFER_SIZE, 0)) == -1)
    {
        printf("Error handling incoming request");
        return -1;
    }
        printf("string======[%s---------------------------]",buffer);
    return 0;
}


阅读(8951) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~