Android Question b4a CCTV Server Question

Andrew A Gray

Member
Licensed User
I ran your b4a CCTV Server project and it works! This is exactly what I need. However, when I connect with Chrome or Firefox, I am getting "ghost" connections. That is, when I connect with a browser, most times I gets multiple "New connections" for only 1 real connection, and the client count gets off. The video still works, but the video loop is servicing ghost connections. When I disconnect, the client count only decrements by 1, leaving the "ghost" connections. Have you seen this? Any ideas?

P.S. But when I connect to CCTV and watch with ffmpeg (ffplay) the client count remains correct.(?)(?)
P.S.S. With a Linux program I wrote, Chrome/Firefox do not do this. (?)(?)

This occurs on b4a CCTV Server running on either a Samsung Galaxy S4 or a Samsung Galaxy ON5 (in release or debug mode).

Andrew Gray
 
Last edited:

Andrew A Gray

Member
Licensed User
That's what it seems, but I still do not understand.... because in a low level C program that I wrote (for Linux) using sockets, I do not see this behavior with Firefox. Only with the high level program does this happen. So .... you are also seeing these ghost connections on your Android? Any way around them?
 
Last edited:
Upvote 0

Andrew A Gray

Member
Licensed User
So, is B4a's ServerSocket object just the same as the Android SDK ServerSocket object, in a one-to-one use? So this "ghost connection feature" you have no control over? In a other words, is there any reason to try to re-write the CCTV Server program in Java to see if it would have the same "ghost connection" features?

Next, in your Network.zip example, I see you write

B4X:
Private Sub ListenForConnections
   Do While working
     server.Listen
     Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
     If Successful Then
       CloseExistingConnection
       client = NewSocket
       astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
       UpdateState(True)
     End If
   Loop
End Sub
Does this "CloseExistingConnection" call keep the connections limited to just 1 connection? That is, does this keep the "ghost connections" from Firefox out?

My requirements are an "IP Webcam" replacement that can be improved. (IP Webcam does not have the "ghost connection" problem with Firefox). And yes, the "ghost connections" do not happen with ffplay either. But our users already have Firefox and Chrome and it is much better to market to "Browser-Friendly" rather than to "You-Need-A-Special-Client".
 
Last edited:
Upvote 0

Erel

Administrator
Staff member
Licensed User
Please use [code]code here...[/code] tags when posting code.

B4A ServerSocket is based on Android SDK ServerSocket. It is not exactly the same.

So this "ghost connection feature" you have no control over?
It is a real connection.

is there any reason to try to re-write the CCTV Server program in Java to see if it would have the same "ghost connection" features?
You can try to implement it in Java and see whether it happens or not.

Next, in your Network.zip example, I see you write
I'm pretty sure that I can find several Network.zip examples in the forum. I assume that you are talking about an example that maintains a single connection. The CCTV server example is more complicated and it supports multiple connections.

If you only want to support a single connection then you can simplify it. You will still need to find out how to keep the correct one open.
 
Upvote 0

Andrew A Gray

Member
Licensed User
Well, this all seems so strange to me that I decided to use Windump to see what was happening. I have my Android CCTV Server at 192.168.1.156:1237 and the Client at 192.168.1.170. I captured 15 packets from Chrome and 15 packets from ffmpeg.


C:\bin>Windump -i 2 -n -XX -c 15 host 192.168.1.156
Windump: listening on \Device\NPF_{578D99A0-2E4F-4225-8A24-0B08DCA23676}

Chrome<->CCTV first 15 packets
B4X:
13:32:17.651271 IP 192.168.1.170.50780 > 192.168.1.156.1237: S 3662693016:3662693016(0) win 8192 <mss 1460,nop,wscale 2,nop,nop,sackOK>
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0034 62f6 4000 8006 1337 c0a8 01aa c0a8  .4b.@....7......
  0x0020:  019c c65c 04d5 da50 4298 0000 0000 8002  ...\...PB.......
  0x0030:  2000 e264 0000 0204 05b4 0103 0302 0101  ...d............
  0x0040:  0402  ..
13:32:17.653326 IP 192.168.1.170.50781 > 192.168.1.156.1237: S 3954253683:3954253683(0) win 8192 <mss 1460,nop,wscale 2,nop,nop,sackOK>
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0034 62f7 4000 8006 1336 c0a8 01aa c0a8  .4b.@....6......
  0x0020:  019c c65d 04d5 ebb1 1f73 0000 0000 8002  ...].....s......
  0x0030:  2000 f427 0000 0204 05b4 0103 0302 0101  ...'............
  0x0040:  0402  ..
13:32:17.654501 IP 192.168.1.156.1237 > 192.168.1.170.50780: S 3502654910:3502654910(0) ack 3662693017 win 65535 <mss 1460,nop,nop,sackOK,nop,wscale 5>
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  0034 0000 4000 4006 b62d c0a8 019c c0a8  .4..@.@..-......
  0x0020:  01aa 04d5 c65c d0c6 45be da50 4299 8012  .....\..E..PB...
  0x0030:  ffff ebcb 0000 0204 05b4 0101 0402 0103  ................
  0x0040:  0305  ..
13:32:17.654576 IP 192.168.1.170.50780 > 192.168.1.156.1237: . ack 1 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 62f8 4000 8006 1341 c0a8 01aa c0a8  .(b.@....A......
  0x0020:  019c c65c 04d5 da50 4299 d0c6 45bf 5010  ...\...PB...E.P.
  0x0030:  4029 ec72 0000  @).r..
13:32:17.657417 IP 192.168.1.156.1237 > 192.168.1.170.50781: S 84401656:84401656(0) ack 3954253684 win 65535 <mss 1460,nop,nop,sackOK,nop,wscale 5>
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  0034 0000 4000 4006 b62d c0a8 019c c0a8  .4..@.@..-......
  0x0020:  01aa 04d5 c65d 0507 ddf8 ebb1 1f74 8012  .....].......t..
  0x0030:  ffff 3114 0000 0204 05b4 0101 0402 0103  ..1.............
  0x0040:  0305  ..
13:32:17.657498 IP 192.168.1.170.50781 > 192.168.1.156.1237: . ack 1 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 62f9 4000 8006 1340 c0a8 01aa c0a8  .(b.@....@......
  0x0020:  019c c65d 04d5 ebb1 1f74 0507 ddf9 5010  ...].....t....P.
  0x0030:  4029 31bb 0000  @)1...
13:32:17.658162 IP 192.168.1.170.50780 > 192.168.1.156.1237: P 1:413(412) ack 1 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  01c4 62fa 4000 8006 11a3 c0a8 01aa c0a8  ..b.@...........
  0x0020:  019c c65c 04d5 da50 4299 d0c6 45bf 5018  ...\...PB...E.P.
  0x0030:  4029 bb98 0000 4745 5420 2f20 4854 5450  @)....GET./.HTTP
  0x0040:  2f31 2e31 0d0a 486f 7374 3a20 3139 322e  /1.1..Host:.192.
  0x0050:  3136 382e 312e 3135 363a 3132 3337 0d0a  168.1.156:1237..
13:32:17.666869 IP 192.168.1.156.1237 > 192.168.1.170.50780: P 1:127(126) ack 1 win 2738
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  00a6 b11c 4000 4006 049f c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65c d0c6 45bf da50 4299 5018  .....\..E..PB.P.
  0x0030:  0ab2 52c8 0000 4854 5450 2f31 2e30 2032  ..R...HTTP/1.0.2
  0x0040:  3030 204f 4b0d 0a43 6163 6865 2d43 6f6e  00.OK..Cache-Con
  0x0050:  7472 6f6c 3a20 6e6f 2d63 6163 6865 0d0a  trol:.no-cache..
13:32:17.666963 IP 192.168.1.156.1237 > 192.168.1.170.50780: . ack 413 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  0028 b11d 4000 4006 051c c0a8 019c c0a8  .(..@.@.........
  0x0020:  01aa 04d5 c65c d0c6 463d da50 4435 5010  .....\..F=.PD5P.
  0x0030:  0ad3 1faf 0000  ......
13:32:17.674348 IP 192.168.1.156.1237 > 192.168.1.170.50781: P 1:127(126) ack 1 win 2738
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  00a6 295a 4000 4006 8c61 c0a8 019c c0a8  ..)Z@.@..a......
  0x0020:  01aa 04d5 c65d 0507 ddf9 ebb1 1f74 5018  .....].......tP.
  0x0030:  0ab2 9810 0000 4854 5450 2f31 2e30 2032  ......HTTP/1.0.2
  0x0040:  3030 204f 4b0d 0a43 6163 6865 2d43 6f6e  00.OK..Cache-Con
  0x0050:  7472 6f6c 3a20 6e6f 2d63 6163 6865 0d0a  trol:.no-cache..
13:32:17.788935 IP 192.168.1.156.1237 > 192.168.1.170.50780: . 127:1587(1460) ack 413 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc b11e 4000 4006 ff66 c0a8 019c c0a8  ....@.@..f......
  0x0020:  01aa 04d5 c65c d0c6 463d da50 4435 5010  .....\..F=.PD5P.
  0x0030:  0ad3 872e 0000 2d2d 6d79 626f 756e 6461  ......--mybounda
  0x0040:  7279 0d0a 436f 6e74 656e 742d 5479 7065  ry..Content-Type
  0x0050:  3a69 6d61 6765 2f6a 7065 670d 0a43 6f6e  :image/jpeg..Con
13:32:17.788996 IP 192.168.1.170.50780 > 192.168.1.156.1237: . ack 1587 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 62fb 4000 8006 133e c0a8 01aa c0a8  .(b.@....>......
  0x0020:  019c c65c 04d5 da50 4435 d0c6 4bf1 5010  ...\...PD5..K.P.
  0x0030:  4029 e4a4 0000  @)....
13:32:17.792839 IP 192.168.1.156.1237 > 192.168.1.170.50780: . 1587:3047(1460) ack 413 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc b11f 4000 4006 ff65 c0a8 019c c0a8  ....@.@..e......
  0x0020:  01aa 04d5 c65c d0c6 4bf1 da50 4435 5010  .....\..K..PD5P.
  0x0030:  0ad3 b900 0000 0e94 a290 c78a 5cf1 4d06  ............\.M.
  0x0040:  814c 4385 3853 075a 78a4 c05a 3345 1486  .LC.8S.Zx..Z3E..
  0x0050:  2d14 0a29 8800 a701 4829 e298 0a3a 5396  -..)....H)...:S.
13:32:17.792932 IP 192.168.1.156.1237 > 192.168.1.170.50780: . 3047:4507(1460) ack 413 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc b120 4000 4006 ff64 c0a8 019c c0a8  ....@.@..d......
  0x0020:  01aa 04d5 c65c d0c6 51a5 da50 4435 5010  .....\..Q..PD5P.
  0x0030:  0ad3 e571 0000 45ad 0715 5255 f4a6 32a9  ...q..E...RU..2.
  0x0040:  14d3 5230 c546 6802 334d 239a 7914 87a6  ..R0.Fh.3M#.y...
  0x0050:  6908 6118 a4a5 34d3 45c6 2534 d3e9 b4c0  i.a...4.E.%4....
13:32:17.792966 IP 192.168.1.170.50780 > 192.168.1.156.1237: . ack 4507 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 62fc 4000 8006 133d c0a8 01aa c0a8  .(b.@....=......
  0x0020:  019c c65c 04d5 da50 4435 d0c6 5759 5010  ...\...PD5..WYP.
  0x0030:  4029 d93c 0000  @).<..
15 packets captured
151 packets received by filter
0 packets dropped by kernel


C:\bin>Windump -i 2 -n -XX -c 15 host 192.168.1.156
Windump: listening on \Device\NPF_{578D99A0-2E4F-4225-8A24-0B08DCA23676}

ffmpeg<->CCTV first 15 packets
B4X:
13:31:25.077991 IP 192.168.1.170.50778 > 192.168.1.156.1237: S 110404506:110404506(0) win 8192 <mss1460,nop,wscale 2,nop,nop,sackOK>
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0034 6284 4000 8006 13a9 c0a8 01aa c0a8  .4b.@...........
  0x0020:  019c c65a 04d5 0694 a39a 0000 0000 8002  ...Z............
  0x0030:  2000 5521 0000 0204 05b4 0103 0302 0101  ..U!............
  0x0040:  0402  ..
13:31:25.086617 IP 192.168.1.156.1237 > 192.168.1.170.50778: S 1000419909:1000419909(0) ack 110404507 win 65535 <mss 1460,nop,nop,sackOK,nop,wscale 5>
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  0034 0000 4000 4006 b62d c0a8 019c c0a8  .4..@.@..-......
  0x0020:  01aa 04d5 c65a 3ba1 3245 0694 a39b 8012  .....Z;.2E......
  0x0030:  ffff 0727 0000 0204 05b4 0101 0402 0103  ...'............
  0x0040:  0305  ..
13:31:25.086670 IP 192.168.1.170.50778 > 192.168.1.156.1237: . ack 1 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 6285 4000 8006 13b4 c0a8 01aa c0a8  .(b.@...........
  0x0020:  019c c65a 04d5 0694 a39b 3ba1 3246 5010  ...Z......;.2FP.
  0x0030:  4029 07ce 0000  @)....
13:31:25.086792 IP 192.168.1.170.50778 > 192.168.1.156.1237: P 1:139(138) ack 1 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  00b2 6286 4000 8006 1329 c0a8 01aa c0a8  ..b.@....)......
  0x0020:  019c c65a 04d5 0694 a39b 3ba1 3246 5018  ...Z......;.2FP.
  0x0030:  4029 91d5 0000 4745 5420 2f20 4854 5450  @)....GET./.HTTP
  0x0040:  2f31 2e31 0d0a 5573 6572 2d41 6765 6e74  /1.1..User-Agent
  0x0050:  3a20 4c61 7666 2f35 372e 3234 2e31 3031  :.Lavf/57.24.101
13:31:25.092051 IP 192.168.1.156.1237 > 192.168.1.170.50778: . ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  0028 f9d7 4000 4006 bc61 c0a8 019c c0a8  .(..@.@..a......
  0x0020:  01aa 04d5 c65a 3ba1 3246 0694 a425 5010  .....Z;.2F...%P.
  0x0030:  0ad3 3c9a 0000  ..<...
13:31:25.187506 IP 192.168.1.156.1237 > 192.168.1.170.50778: P 1:127(126) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  00a6 f9d8 4000 4006 bbe2 c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 3246 0694 a425 5018  .....Z;.2F...%P.
  0x0030:  0ad3 6d78 0000 4854 5450 2f31 2e30 2032  ..mx..HTTP/1.0.2
  0x0040:  3030 204f 4b0d 0a43 6163 6865 2d43 6f6e  00.OK..Cache-Con
  0x0050:  7472 6f6c 3a20 6e6f 2d63 6163 6865 0d0a  trol:.no-cache..
13:31:25.381489 IP 192.168.1.156.1237 > 192.168.1.170.50778: . 127:1587(1460) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc f9d9 4000 4006 b6ab c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 32c4 0694 a425 5010  .....Z;.2....%P.
  0x0030:  0ad3 eb85 0000 2d2d 6d79 626f 756e 6461  ......--mybounda
  0x0040:  7279 0d0a 436f 6e74 656e 742d 5479 7065  ry..Content-Type
  0x0050:  3a69 6d61 6765 2f6a 7065 670d 0a43 6f6e  :image/jpeg..Con
13:31:25.381539 IP 192.168.1.170.50778 > 192.168.1.156.1237: . ack 1587 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 6287 4000 8006 13b2 c0a8 01aa c0a8  .(b.@...........
  0x0020:  019c c65a 04d5 0694 a425 3ba1 3878 5010  ...Z.....%;.8xP.
  0x0030:  4029 0112 0000  @)....
13:31:25.387108 IP 192.168.1.156.1237 > 192.168.1.170.50778: . 1587:3047(1460) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc f9da 4000 4006 b6aa c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 3878 0694 a425 5010  .....Z;.8x...%P.
  0x0030:  0ad3 eb9c 0000 07a8 a70a 68a7 8a40 28a5  ..........h..@(.
  0x0040:  a4a2 8014 53c1 a60a 78a6 02e6 8eb4 502a  ....S...x.....P*
  0x0050:  405a 3145 14c0 00e6 9c05 20a7 8a60 28a7  @Z1E.........`(.
13:31:25.392654 IP 192.168.1.156.1237 > 192.168.1.170.50778: . 3047:4507(1460) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc f9db 4000 4006 b6a9 c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 3e2c 0694 a425 5010  .....Z;.>,...%P.
  0x0030:  0ad3 55c3 0000 0c53 0f5a a403 08cd 3718  ..U....S.Z....7.
  0x0040:  e94f 2298 78a0 0434 d239 a5a6 9a2e 210f  .O".x..4.9....!.
  0x0050:  5a4a 5c52 5340 275a 4a3a 504d 3012 8a33  ZJ\RS@'ZJ:PM0..3
13:31:25.392680 IP 192.168.1.170.50778 > 192.168.1.156.1237: . ack 4507 win 16425
  0x0000:  54f2 014c b4de 0027 10e2 5db4 0800 4500  T..L...'..]...E.
  0x0010:  0028 6288 4000 8006 13b1 c0a8 01aa c0a8  .(b.@...........
  0x0020:  019c c65a 04d5 0694 a425 3ba1 43e0 5010  ...Z.....%;.C.P.
  0x0030:  4029 f5a9 0000  @)....
13:31:25.392761 IP 192.168.1.156.1237 > 192.168.1.170.50778: . 4507:5967(1460) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc f9dc 4000 4006 b6a8 c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 43e0 0694 a425 5010  .....Z;.C....%P.
  0x0030:  0ad3 dc4b 0000 9d9a 6114 c425 2506 92a9  ...K....a..%%...
  0x0040:  085a 5148 29c2 9885 14e5 a68a 7ad0 31e2  .ZQH).......z.1.
  0x0050:  9e29 8b4f 150c 6851 4b40 a314 0c43 4da7  .).O..hQK@...CM.
13:31:25.392787 IP 192.168.1.156.1237 > 192.168.1.170.50778: . 5967:7427(1460) ack 139 win 2771
  0x0000:  0027 10e2 5db4 54f2 014c b4de 0800 4500  .'..].T..L....E.
  0x0010:  05dc f9dd 4000 4006 b6a7 c0a8 019c c0a8  ....@.@.........
  0x0020:  01aa 04d5 c65a 3ba1 4994 0694 a425 5010  .....Z;.I....%P.
  0x0030:  0ad3 c412 0000 a434 ca43 108e 29bd 29d4  .......4.C..).).
  0x0040:  da68 043c d274 a5e9 4868 b083 b526 28a2  .h.<.t..Hh...&(.
  0x0050:  9808 451d a973 4119 a2c0 3681 4525 1601  ..E..sA...6.E%..
15 packets captured
105 packets received by filter
0 packets dropped by kernel

Continued...
 
Last edited:
Upvote 0

Andrew A Gray

Member
Licensed User
Well, Erel, you are correct. Chrome sends initial requests from ports 50780 and 50781, and CCTV Server responds to both of them, giving 2 clients. What The ...? What is the purpose of this and how does Linux get around this? What is going on behind the scenes here? Is there some unwritten protocol involved here that I never heard about?

I wrote a very similar program in Linux and I never saw anything like this, so Linux must handle these requests as just one request, correct? Do you have more info about this?
 
Last edited:
Upvote 0

Andrew A Gray

Member
Licensed User
Well, I was bothered because the video loop was processing the "extra connections" (I'm no longer calling them "ghost connections" ), and the CCTV Server program was crashing trying to delete random clients. So I decided to go back to allowing just a single browser connection. This still did not work because sometimes the wrong connection was closed. What finally seemed to work was to store a time and ignore connections following a Socket Disconnect Event and ignore connections just following a NewSocket Event.


So store a "LastTime" here:
B4X:
Private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)

   If (Successful And ( DateTime.Now > (LastTime + 2000)  ) )  Then
     'CloseExistingConnection'
     ClientIn = NewSocket
     MyClient.Initialize( NewSocket, Me )
   Else
     Log( "Skipping New Connection" )
     NewSocket.Close
   End If

   server.Listen
   LastTime = DateTime.Now
End Sub




And store a "LastTime" here:
B4X:
Public Sub ClientDisconnected (client1 As Client)
   CloseExistingConnection
   LastTime = DateTime.Now
End Sub

This seems to throw away the unwanted NewSockets and let the browser work. I have not tried this with the multiclient version yet.
And I am still very curious as to why these Browsers have this behavior.

Thanks for your help!
 
Last edited:
Upvote 0

Similar Threads

Top