linux编译运行build.sh,linux下libwebsockets编译及实例

最近想自己搭建一个webscoket协议的服务器,打算用libwebsockts这个库。

下载代码编译。

编写一个shell脚本

#!/bin/sh

# wget http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz

# tar xvzf libwebsockets-1.4-chrome43-firefox-36.tar.gz

#cd libwebsockets-1.4-chrome43-firefox-36

# build

git clone https://github.com/warmcat/libwebsockets.git

cd libwebsockets

documentRoot=`pwd`

mkdir build

cmake ..

make

执行shell脚本就下载编译成功了。

运行一个例子

其实在库里面有一些实例的.

cd build

#!/bin/sh

documentRoot=`pwd`

libdir=libwebsockets

cd $libdir/build/bin

./libwebsockets-test-server --resource_path=$documentRoot/test-server

这样就运行自带的服务器。运行结果.

0818b9ca8b590ca3270a3433284dd417.png

浏览器输入127.0.0.1:7681结果:

0818b9ca8b590ca3270a3433284dd417.png

这样就是完整的结果了。

搭建自己的websocket server

#include

#include

#include

#include

#include

#include

#define KGRN "\033[0;32;32m"

#define KCYN "\033[0;36m"

#define KRED "\033[0;32;31m"

#define KYEL "\033[1;33m"

#define KMAG "\033[0;35m"

#define KBLU "\033[0;32;34m"

#define KCYN_L "\033[1;36m"

#define RESET "\033[0m"

static int destroy_flag = 0;

static void INT_HANDLER(int signo) {

destroy_flag = 0;

}

/* *

* websocket_write_back: write the string data to the destination wsi.

*/

int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)

{

if (str == NULL || wsi_in == NULL)

return -1;

int n;

int len;

unsigned char *out = NULL;

if (str_size_in < 1)

len = strlen(str);

else

len = str_size_in;

out = (unsigned char *)malloc(sizeof(unsigned char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));

//* setup the buffer*/

memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, str, len );

//* write out*/

n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);

printf(KBLU"[websocket_write_back] %s\n"RESET, str);

//* free the buffer*/

free(out);

return n;

}

static int ws_service_callback(

struct lws *wsi,

enum lws_callback_reasons reason, void *user,

void *in, size_t len)

{

switch (reason) {

case LWS_CALLBACK_ESTABLISHED:

printf(KYEL"[Main Service] Connection established\n"RESET);

break;

//* If receive a data from client*/

case LWS_CALLBACK_RECEIVE:

printf(KCYN_L"[Main Service] Server recvived:%s\n"RESET,(char *)in);

//* echo back to client*/

websocket_write_back(wsi ,(char *)in, -1);

break;

case LWS_CALLBACK_CLOSED:

printf(KYEL"[Main Service] Client close.\n"RESET);

break;

default:

break;

}

return 0;

}

struct per_session_data {

int fd;

};

int main(void) {

// server url will usd port 5000

int port = 5000;

const char *interface = NULL;

struct lws_context_creation_info info;

struct lws_protocols protocol;

struct lws_context *context;

// Not using ssl

const char *cert_path = NULL;

const char *key_path = NULL;

// no special options

int opts = 0;

//* register the signal SIGINT handler */

struct sigaction act;

act.sa_handler = INT_HANDLER;

act.sa_flags = 0;

sigemptyset(&act.sa_mask);

sigaction( SIGINT, &act, 0);

//* setup websocket protocol */

protocol.name = "my-echo-protocol";

protocol.callback = ws_service_callback;

protocol.per_session_data_size=sizeof(struct per_session_data);

protocol.rx_buffer_size = 0;

//* setup websocket context info*/

memset(&info, 0, sizeof info);

info.port = port;

info.iface = interface;

info.protocols = &protocol;

info.extensions = lws_get_internal_extensions();

info.ssl_cert_filepath = cert_path;

info.ssl_private_key_filepath = key_path;

info.gid = -1;

info.uid = -1;

info.options = opts;

//* create libwebsocket context. */

context = lws_create_context(&info);

if (context == NULL) {

printf(KRED"[Main] Websocket context create error.\n"RESET);

return -1;

}

printf(KGRN"[Main] Websocket context create success.\n"RESET);

//* websocket service */

while ( !destroy_flag ) {

lws_service(context, 50);

}

usleep(10);

lws_context_destroy(context);

return 0;

}

执行脚本:

#!/bin/sh

libdir=libwebsockets

g++ -g -o ws_servertest_ws_server.cpp -I$libdir/lib -I$libdir/build -L$libdir/build/lib -lwebsockets

编写自己的websocket client

#include

#include

#include

#include

#include

#include

#include

#define KGRN "\033[0;32;32m"

#define KCYN "\033[0;36m"

#define KRED "\033[0;32;31m"

#define KYEL "\033[1;33m"

#define KBLU "\033[0;32;34m"

#define KCYN_L "\033[1;36m"

#define KBRN "\033[0;33m"

#define RESET "\033[0m"

static int destroy_flag = 0;

static int connection_flag = 0;

static int writeable_flag = 0;

static void INT_HANDLER(int signo) {

destroy_flag = 0;

}

struct session_data {

int fd;

};

struct pthread_routine_tool {

struct lws_context *context;

struct lws *wsi;

};

static int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)

{

if (str == NULL || wsi_in == NULL)

return -1;

int n;

int len;

unsigned char *out = NULL;

if (str_size_in < 1)

len = strlen(str);

else

len = str_size_in;

out = (unsigned char *)malloc(sizeof(unsigned char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));

//* setup the buffer*/

memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, str, len );

//* write out*/

n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);

printf(KBLU"[websocket_write_back] %s\n"RESET, str);

//* free the buffer*/

free(out);

return n;

}

static int ws_service_callback(

struct lws *wsi,

enum lws_callback_reasons reason, void *user,

void *in, size_t len)

{

switch (reason) {

case LWS_CALLBACK_CLIENT_ESTABLISHED:

printf(KYEL"[Main Service] Connect with server success.\n"RESET);

connection_flag = 1;

break;

case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:

printf(KRED"[Main Service] Connect with server error.\n"RESET);

destroy_flag = 1;

connection_flag = 0;

break;

case LWS_CALLBACK_CLOSED:

printf(KYEL"[Main Service] LWS_CALLBACK_CLOSED\n"RESET);

destroy_flag = 1;

connection_flag = 0;

break;

case LWS_CALLBACK_CLIENT_RECEIVE:

printf(KCYN_L"[Main Service] Client recvived:%s\n"RESET, (char *)in);

if (writeable_flag)

destroy_flag = 1;

break;

case LWS_CALLBACK_CLIENT_WRITEABLE :

printf(KYEL"[Main Service] On writeable is called. send byebye message\n"RESET);

websocket_write_back(wsi, "Byebye! See you later", -1);

writeable_flag = 1;

break;

default:

break;

}

return 0;

}

static void *pthread_routine(void *tool_in)

{

struct pthread_routine_tool *tool = (struct pthread_routine_tool*)tool_in;

printf(KBRN"[pthread_routine] Good day. This is pthread_routine.\n"RESET);

//* waiting for connection with server done.*/

while(!connection_flag)

usleep(1000*20);

//*Send greeting to server*/

printf(KBRN"[pthread_routine] Server is ready. send a greeting message to server.\n"RESET);

websocket_write_back(tool->wsi, "Good day", -1);

printf(KBRN"[pthread_routine] sleep 2 seconds then call onWritable\n"RESET);

sleep(1);

printf(KBRN"------------------------------------------------------\n"RESET);

sleep(1);

//printf(KBRN"[pthread_routine] sleep 2 seconds then call onWritable\n"RESET);

//*involked wriable*/

printf(KBRN"[pthread_routine] call on writable.\n"RESET);

lws_callback_on_writable(tool->wsi);

}

int main(void)

{

//* register the signal SIGINT handler */

struct sigaction act;

act.sa_handler = INT_HANDLER;

act.sa_flags = 0;

sigemptyset(&act.sa_mask);

sigaction( SIGINT, &act, 0);

struct lws_context *context = NULL;

struct lws_context_creation_info info;

struct lws *wsi = NULL;

struct lws_protocols protocol;

memset(&info, 0, sizeof info);

info.port = CONTEXT_PORT_NO_LISTEN;

info.iface = NULL;

info.protocols = &protocol;

info.ssl_cert_filepath = NULL;

info.ssl_private_key_filepath = NULL;

info.extensions = lws_get_internal_extensions();

info.gid = -1;

info.uid = -1;

info.options = 0;

protocol.name = "my-echo-protocol";

protocol.callback = &ws_service_callback;

protocol.per_session_data_size = sizeof(struct session_data);

protocol.rx_buffer_size = 0;

protocol.id = 0;

protocol.user = NULL;

context = lws_create_context(&info);

printf(KRED"[Main] context created.\n"RESET);

if (context == NULL) {

printf(KRED"[Main] context is NULL.\n"RESET);

return -1;

}

wsi = lws_client_connect(context, "localhost", 5000, 0, "/", "localhost:5000", NULL,

protocol.name, -1);

if (wsi == NULL) {

printf(KRED"[Main] wsi create error.\n"RESET);

return -1;

}

printf(KGRN"[Main] wsi create success.\n"RESET);

struct pthread_routine_tool tool;

tool.wsi = wsi;

tool.context = context;

pthread_t pid;

pthread_create(&pid, NULL, pthread_routine, &tool);

pthread_detach(pid);

while(!destroy_flag)

{

lws_service(context, 50);

}

lws_context_destroy(context);

return 0;

}编写脚本:

#!/bin/sh

libdir=libwebsockets

g++ -g -o ws_client test_ws_client.cpp -I$libdir/lib -I$libdir/build -L$libdir/build/lib -lwebsockets -lpthread

运行结果

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

红线部分是交互的结果.这样就是完整的一套逻辑了.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/257978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Tomcat如何配置环境变量

1&#xff0c; JDK&#xff1a;版本为jdk-7-windows-i586.exe 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2&#xff0c;tomcat&#xff1a;版本为apache-tomcat-7.0.33-windows-x86.zip 下载地址&#xff1a;http://tomcat.apache.org/ 2…

反编译查看源码dex2jar

为什么80%的码农都做不了架构师&#xff1f;>>> 上次说到了用apktool反编译&#xff0c;这次我们来用dex2jar 把apk解压得到文件夹 文件夹打开看到这些文件 其中这个classes.dex就是这次需要用到的字节码文件 把这个字节码文件托到dex2jar目录里 命令行编辑 得到下…

代码混淆之后定位线上bug

代码混淆的目的 代码混淆的目的是防止竞争对手通过反编译来阅读项目代码。 Android中通过ProGuard来做代码混淆&#xff08;当然也还有其他的产品可以做代码混淆&#xff09;。 bug日志反混淆 资料&#xff1a;错误log、mapping.txt 异常log&#xff1a; mapping.txt&#xff…

本地通知

本地通知&#xff0c;local notification&#xff0c;用于基于时间行为的通知&#xff0c;比如有关日历或者todo列表的小应用。另外&#xff0c;应用如果在后台执行&#xff0c;iOS允许它在受限的时间内运行&#xff0c;它也会发现本地通知有用。比如&#xff0c;一个应用&…

把windows装到linux下,如何将WSL(Windows Subsystem for Linux 2)安装到Windows 10?

原标题&#xff1a;如何将WSL(Windows Subsystem for Linux 2)安装到Windows 10&#xff1f;Windows 10凭借大受欢迎的WSL(Windows Subsystem for Linux)进入Linux领域。由于最近推出了WSL的最新版WSL2&#xff0c;用户现在可以利用实际的Linux内核从Windows执行Linux任务。现在…

vba执行linux命令,从VBA中的shell命令捕获输出值?

慕盖茨4494581根据Andrew Lessard的回答&#xff0c;这是一个运行命令并将输出作为字符串返回的函数 -Public Function ShellRun(sCmd As String) As StringRun a shell command, returning the output as a stringDim oShell As ObjectSet oShell CreateObject("WScript…

C#= 栈模仿堆的操作

//原理&#xff0c;利用两个栈&#xff0c;互相作用&#xff0c;来模仿堆的效果&#xff0c;先进先出。。 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Threading.Tasks;5 6 namespace TwoStacksQueue7 {8 public class Progra…

linux中内部命令有哪些,linux内部命令有哪些

linux中常见的内部命令有&#xff1a;1.exit命令&#xff0c;退出当前的shell&#xff1b;2.history命令&#xff0c;显示历史执行过的命令&#xff1b;3.cd命令&#xff0c;切换当前工作目录&#xff1b;4.source命令&#xff0c;重新执行刚修改的初始化文件&#xff1b;5.ech…

POJ 2778

题意&#xff1a;很Uva项链题目类似。 区别&#xff1a; 1、字符串很多&#xff0c;用map hash超时&#xff0c;用Trie查找。 2、DFS判断连通&#xff0c;和并查集判连通&#xff0c;被我写错的地方时&#xff0c;查森林的时候&#xff0c;还是要Find_Set。 1 #include <ios…

linux挂载VMFS硬盘,ESX4.1挂载NFS共享存储(VMkernel)

要使用vmotion,iscsi,nfs功能&#xff0c;必须启用VMkernel端口&#xff0c;ESX 4.1默认不启用&#xff0c;ESXi 5.x默认启用。在 vCenter Server“SZVCENTER01”上调用对象“datastoreSystem-44”的“HostDatastoreSystem.CreateNasDatastore” 失败。挂载NFS存储的ESX控制台命…

2017年8个最流行的Web编程趋势

互联网一直在不断的发展&#xff0c;这意味着开发人员必须及时了解当前的所有变化。人们在新闻、社交、购物到银行等各大方面都与互联网有着千丝万缕的联系。因此&#xff0c;为了满足全球数百万网络用户的需求&#xff0c;Web开发需求正在上升。Web编程趋势是在W开发的过程中不…

gRPC-rs:从 C 到 Rust

介绍 在上篇文章中&#xff0c;我们讲到 TiKV 为了支持 [gRPC]&#xff0c;我们造了个轮子 [gRPC-rs]&#xff0c;这篇文章简要地介绍一下这个库。首先我们来聊聊什么是 gRPC。gRPC 是 Google 推出的基于 [HTTP2] 的开源 RPC 框架&#xff0c;希望通过它使得各种微服务之间拥有…

linux系统编程练手项目,精选 22 个 C++ 项目,编程小白练手首选!

C/C 做为元老级的编程语言&#xff0c;任时光更迭依旧屹立不倒&#xff0c;哪怕现在煊赫一时的AI&#xff0c;其底层也是用其编写。linux那么做为新手该如何快速上手 C 呢&#xff1f;固然是敲代码啊&#xff01;一切不写代码的学编程都是瞎搞。下面为你们精选了 22 个 C 项目&…

linux怎么同时查看两个文件,MultiTail - 在单个Linux终端中同时监视多个文件

无论是服务器管理员还是程序员&#xff0c;我们需要参考多个日志文件来有效地排除故障任务。 为了实现这一点&#xff0c;我们必须打开&#xff0c;拖尾或更少的不同shell中的每个日志文件。 但是&#xff0c;我们可以使用传统的tail命令状尾-f在/ var / log / messages文件或尾…

今日BBC

1、随身英语 Dry January 新年戒酒一个月 link 2、地道英语 Hot potato 棘手的问题“烫手山芋” link 3、今日新闻 Brussels attacks: Belgian police arrest six suspects link The arrests were made in the Schaerbeek district. There is no word yet on the identitie…

实验吧 貌似有点难 伪造ip

解题链接&#xff1a; http://ctf5.shiyanbar.com/phpaudit/ 解答&#xff1a; 点击View the source code —>代码显示IP为1.1.1.1即可得到KEY—>使用modify header伪造IP—>拿到flag 相关&#xff1a; modify header我也是第一次用&#xff0c;下面附上相关说明&…

用C语言用指针怎么算通用定积分,C语言:利用指针编写程序,用梯形法计算给定的定积分实例...

题目要求利用指针编写程序&#xff0c;用梯形法计算下列公式中的定积分&#xff1a;参考代码首先说明一下指针的用处&#xff1a;因为所传递的参数均为数字&#xff0c;并不需要使用指针提高效率&#xff0c;故这里使用指针指向函数。请注意calc()函数中的这一语句&#xff1a;…

你该把前端外包出来了

2019独角兽企业重金招聘Python工程师标准>>> 移动热潮慢慢褪去&#xff0c;大的几个app已经霸占了所有的人桌面&#xff0c;而微信却变得越来越重要。微信里面&#xff0c;提倡H5的应用&#xff0c;H5应用开发成本低、上线快、易调整、跨平台等诸多优势&#xff0c;…

R 统计学工具部署和使用

由于公司内部对于市场数据分析的需求&#xff0c;要求引入R统计工具&#xff0c;并集成到报表工具中。对于R的介绍&#xff0c;大家请百度一下&#xff0c;当然&#xff0c;最好能去看官方的说明 https://www.r-project.org/ 下面简单介绍一下R工具的安装和数据分析工具Spotfir…

Android下创建一个输入法

输入法是一种可以让用户输入文字的控件。Android提供了一套可扩展的输入法框架&#xff0c;使得应用程序可以让用户选择各种类型的输入法&#xff0c;比如基于触屏的键盘输入或者基于语音。当安装了特定输入法之后&#xff0c;用户即可在系统设置中选择个输入法&#xff0c;并在…