- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何使用 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/
我正在使用libwebsockets-2.1.0具有通用 session 和lwsws选项启用。 在 LWS_CALLBACK_SERVER_WRITEABLE 情况下,我有一些代码可以打开文件,并将
供您引用(因为我在下面提到了库函数),可以在此处找到 libwebsockets 文档:https://github.com/warmcat/libwebsockets/blob/master/lib
我正在让 libwebsocket 客户端发送二进制数据。我已将我的二进制数据保存到缓冲区中 [即buf] 并写入套接字。 n = libwebsocket_write(wsi, &bu
当我尝试从客户端向服务器发送数据时,它显示“lwsts[9972]:从skt读取时出错”。我在互联网上搜索过,没有明确的表达。我按照 LibWebSockets 包中的 test_server.c 示
我目前正在构建一个 WebSocket 服务器,我遇到了以下代码来处理碎片消息(来自 libwebsockets ): case LWS_CALLBACK_RECEIVE: { Client
我正在 Linux 上使用 libwebsocket lib 开发一个简单的 websocket 服务器。 我需要为每个客户端连接的已建立事件发送一个初始化数据包。 如果我使用 memcpy( p,
我正在使用 libwebsockets而且我无法编译自己实现的演示代码。 我创建了上下文: struct libwebsocket_context *context; ... context = li
我正在使用 libwebsockets 2.0,但在尝试使用它作为客户端连接到服务器时遇到了一些问题。 根据 libwebsockets 日志,这是当我相当简单的客户端尝试连接到 echo.webso
我试图找到一个示例来解释如何使用 libwebsocket 实现客户端,但我没有得到任何令人信服的代码。有什么链接可以引用吗? 最佳答案 更正了 Ren-Wei Luo 的代码示例以使用 libweb
我正在尝试运行与 LWS 库一起安装的“libwebsockets-test-server”,但它无法运行,因为“lwsts[31616]: libuv 支持未编译”。 我已经检查过是否安装了 lib
我正在尝试为 X-Plane 编写一个小插件,使用 libwebsocket 创建一个简单的 websocket 服务器。我可以从 Google Chrome 连接到 websocket,但是,当我向
我想创建一个 Web 应用程序,只要 C++ 后端有新内容,一组用户就可以异步接收一些数据。因此,理想情况下,当新用户到来时,他将被添加到订阅者列表中,并且每当 C++ 后端有该组的新数据时,它就会将
这是 libwebsocket_client_connect() 的签名。 struct libwebsocket * libwebsocket_client_connect (struct libw
我正在查看 C websocket 库 libwebsockets 客户端示例。 但是我不明白示例的目的是什么。 Here是这个例子,这个例子有两个连接(在代码 wsi_dumb 和 wsi_mirr
如何在 libwebsockets 回调中设置用户自定义指针? 我在 lws_protocol 中添加了一个指针变量。 调用回调函数时,用户指针始终为NULL。 我使用 libwebsockets v
我正在尝试实现一个 websocket 客户端(使用 C 中的 libwebsockets)。我正在使用 cmake 来控制软件编译过程。 我的问题很简单:如何使用 Libwebsockets 启用调
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我正在尝试使用 libwebsockets 创建一个 C++ websocket 客户端,但由于超时,我无法建立连接。我已经剥离了测试的内容,这是我用来建立连接的内容: 协议(protocol) st
我将 warmcat 的 libwebsocket C 库用于小型 websocket 服务器。我已经启动并运行了这些示例,并且可以发送数据以响应从 websocket 接收的数据(例如回显发送的反向
我正在使用 libwebsockets同时运行 websocket 服务器和客户端(实际上在同一个可执行文件中)。有没有办法优雅地关闭连接? 我知道我可以在我的回调函数中返回一个负整数来使连接中止,但
我是一名优秀的程序员,十分优秀!