gpt4 book ai didi

c++ - 如何使用 libwebsockets C 库发送 JSON 数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:38 25 4
gpt4 key购买 nike

如何使用 libwebsockets C 库发送 JSON 数据?

我已经安装并尝试了示例代码,但我看不到任何关于如何将数据发送到 localhost:someport 的示例。任何帮助将不胜感激。

这里是 libwebsockets 的网站:http://libwebsockets.org/trac/libwebsockets

这是 test-client.c 文件:http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/tree/test-server

任何帮助将不胜感激。

最佳答案

我想这是你的例子 http://martinsikora.com/libwebsockets-simple-websocket-server

作者打开localhost服务器并通过 Safari 连接到它。

如果您想与 C 连接,这里是 C 中的 HTTP 客户端示例。

[编辑][客户端](死链接 https://www.cs.utah.edu/~swalton/listings/sockets/programs/part1/chap4/http-client.c 通过 web.archive.org 恢复)

/* http-client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

/*****************************************************************************/
/*** http-client.c ***/
/*** ***/
/*** This program shows what the HTTP server sends to the client. First, ***/
/*** it opens a TCP socket to the server. Then, it sends the request ***/
/*** "GET <resource> HTTP/1.0\n\n" (the second newline is needed for the ***/
/*** "message-end" message. Lastly it prints out the reply. ***/
/*****************************************************************************/

#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>

#define MAXBUF 1024
void PANIC(char *msg);
#define PANIC(msg) {perror(msg); abort();}

int main(int Count, char *Strings[])
{ int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[MAXBUF];

/*---Make sure we have the right number of parameters---*/
if ( Count != 3 )
PANIC(stderr, "usage: testport <IP-addr> <send-msg>\n");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");

/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(80); /*default HTTP Server port */
if ( inet_addr(Strings[1], &dest.sin_addr.s_addr) == 0 )
PANIC(Strings[1]);

/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
PANIC("Connect");

sprintf(buffer, "GET %s HTTP/1.0\n\n", Strings[2]);
send(sockfd, buffer, strlen(buffer), 0);

/*---While there's data, read and print it---*/
do
{
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if ( bytes_read > 0 )
printf("%s", buffer);
}
while ( bytes_read > 0 );

/*---Clean up---*/
close(sockfd);
return 0;
}

关于c++ - 如何使用 libwebsockets C 库发送 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com