1*1c60b9acSAndroid Build Coastguard Worker# lws minimal ws server raw adopt tcp 2*1c60b9acSAndroid Build Coastguard Worker 3*1c60b9acSAndroid Build Coastguard WorkerThis example is only meaningful if you are integrating lws in another 4*1c60b9acSAndroid Build Coastguard Workerapp which generates its own connected sockets. In some cases you may 5*1c60b9acSAndroid Build Coastguard Workerwant lws to "adopt" the socket. 6*1c60b9acSAndroid Build Coastguard Worker 7*1c60b9acSAndroid Build Coastguard Worker(If you simply want a connected client raw socket using lws alone, you 8*1c60b9acSAndroid Build Coastguard Workercan just use lws_client_connect_via_info() with info.method = "RAW". 9*1c60b9acSAndroid Build Coastguard Workerhttp-client/minimal-http-client shows how to do that, just set 10*1c60b9acSAndroid Build Coastguard Workerinfo.method to "RAW".) 11*1c60b9acSAndroid Build Coastguard Worker 12*1c60b9acSAndroid Build Coastguard WorkerThis example demonstrates how to adopt a foreign, connected socket into lws 13*1c60b9acSAndroid Build Coastguard Workeras a raw wsi, bound to a specific lws protocol. 14*1c60b9acSAndroid Build Coastguard Worker 15*1c60b9acSAndroid Build Coastguard WorkerThe example connects a socket itself to libwebsockets.org:80, and then 16*1c60b9acSAndroid Build Coastguard Workerhas lws adopt it as a raw wsi. The lws protocol writes "GET / HTTP/1.1" 17*1c60b9acSAndroid Build Coastguard Workerto the socket and hexdumps what was sent back. 18*1c60b9acSAndroid Build Coastguard Worker 19*1c60b9acSAndroid Build Coastguard WorkerThe socket won't close until the server side times it out, since it's 20*1c60b9acSAndroid Build Coastguard Workera raw socket that doesn't understand it's looking at http. 21*1c60b9acSAndroid Build Coastguard Worker 22*1c60b9acSAndroid Build Coastguard Worker## build 23*1c60b9acSAndroid Build Coastguard Worker 24*1c60b9acSAndroid Build Coastguard Worker``` 25*1c60b9acSAndroid Build Coastguard Worker $ cmake . && make 26*1c60b9acSAndroid Build Coastguard Worker``` 27*1c60b9acSAndroid Build Coastguard Worker 28*1c60b9acSAndroid Build Coastguard Worker## usage 29*1c60b9acSAndroid Build Coastguard Worker 30*1c60b9acSAndroid Build Coastguard Worker``` 31*1c60b9acSAndroid Build Coastguard Worker $ ./lws-minimal-raw-adopt-tcp 32*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:1960] USER: LWS minimal raw adopt tcp 33*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:1961] NOTICE: Creating Vhost 'default' port 7681, 1 protocols, IPv6 off 34*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:2079] USER: Starting connect... 35*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:4963] USER: Connected... 36*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:4963] USER: LWS_CALLBACK_RAW_ADOPT 37*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7842] USER: LWS_CALLBACK_RAW_RX (186) 38*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7842] NOTICE: 39*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7842] NOTICE: 0000: 48 54 54 50 2F 31 2E 31 20 33 30 31 20 52 65 64 HTTP/1.1 301 Red 40*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7842] NOTICE: 0010: 69 72 65 63 74 0D 0A 73 65 72 76 65 72 3A 20 6C irect..server: l 41*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7842] NOTICE: 0020: 77 73 77 73 0D 0A 53 74 72 69 63 74 2D 54 72 61 wsws..Strict-Tra 42*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0030: 6E 73 70 6F 72 74 2D 53 65 63 75 72 69 74 79 3A nsport-Security: 43*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0040: 20 6D 61 78 2D 61 67 65 3D 31 35 37 36 38 30 30 max-age=1576800 44*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0050: 30 20 3B 20 69 6E 63 6C 75 64 65 53 75 62 44 6F 0 ; includeSubDo 45*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0060: 6D 61 69 6E 73 0D 0A 6C 6F 63 61 74 69 6F 6E 3A mains..location: 46*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0070: 20 68 74 74 70 73 3A 2F 2F 6C 69 62 77 65 62 73 https://libwebs 47*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0080: 6F 63 6B 65 74 73 2E 6F 72 67 0D 0A 63 6F 6E 74 ockets.org..cont 48*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 0090: 65 6E 74 2D 74 79 70 65 3A 20 74 65 78 74 2F 68 ent-type: text/h 49*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 00A0: 74 6D 6C 0D 0A 63 6F 6E 74 65 6E 74 2D 6C 65 6E tml..content-len 50*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 00B0: 67 74 68 3A 20 30 0D 0A 0D 0A gth: 0.... 51*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:03:57:7843] NOTICE: 52*1c60b9acSAndroid Build Coastguard Worker[2018/03/23 09:04:03:3627] USER: LWS_CALLBACK_RAW_CLOSE 53*1c60b9acSAndroid Build Coastguard Worker 54*1c60b9acSAndroid Build Coastguard Worker``` 55*1c60b9acSAndroid Build Coastguard Worker 56*1c60b9acSAndroid Build Coastguard WorkerNote the example does everything itself, after 5s idle the remote server closes the connection 57*1c60b9acSAndroid Build Coastguard Workerafter which the example continues until you ^C it. 58