# Problem TCP blocks

**URL:** <https://www.ruby-forum.com/t/problem-tcp-blocks/241920>\
**Category:** GNU Radio\
**Created:** [May 12, 2015, 10:40am UTC](https://www.ruby-forum.com/t/problem-tcp-blocks/241920 "2015-05-12T10:40:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [May 12, 2015, 10:40am UTC](https://www.ruby-forum.com/t/problem-tcp-blocks/241920/1 "2015-05-12T10:40:13Z")

</div>

* * *

Discuss-gnuradio mailing list  
removed\_email\_address@domain.invalid  
[https://lists.gnu.org/mailman/listinfo/discuss-gnuradio](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

---

<div class="post-metadata">

**Author:** ![andreas\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@andreas\_l](https://www.ruby-forum.com/u/andreas_l)\
**Post date:** [May 12, 2015, 1:09pm UTC](https://www.ruby-forum.com/t/problem-tcp-blocks/241920/2 "2015-05-12T13:09:47Z")

</div>

Hi Andy,

oh yes, I don’t like that block 😉 but I’m happy it’s there.

So the problem lies in the way this block is structured, and how GNU  
Radio runs:

tcp\_sink is just a hier block – it’s nothing but a small wrapper around  
the file\_descriptor\_sink[1]. The cool thing about sockets is that you  
can, as soon as you have a connection, get a file descriptor for that  
connection and read and write from/to it, just like a file.  
So tcp\_sink just sets up a listening socket, and as soon as that  
connects, it uses the resulting file descriptor [2] to set up a  
file\_descriptor\_sink.  
That is all nice – but: this means that the constructor of tcp\_sink  
blocks for as long as it takes till someone connects to this socket.  
Which means that the script setting up your flow graph just pauses for  
as long as it waits for the first socket to get its connection set up;  
the second socket is not in listening mode, yet, at this point. So  
you’re doing everything right, it’s just so that your second tcp server  
isn’t even created before the first one connects!

In principle, this is something you’d avoid; waiting for sockets to  
connect is typically the job of a gr::block::start() method rather than  
a gr::hier\_block2’s constructor, for exactly this reason. However,  
tcp\_sink is there for “historical reasons”, and it’s mightily handy in  
many cases.

If your application doesn’t scream for performance:  
You might just want to use gr\_modtool to create a python source, let  
that block’s constructor set up a socket where you call sock.listen(),  
and give that block a start(self) method, which calls sock.accept, and  
stores the returned connected socket in a property of the block; work()  
would then write to that socket. Something like [3]

```
def start(self):
    """
    Block start method, called when user calls tb.run/tb.start
    """
    self.connection, _ = self.listener.accept()

def stop(self):
    self.connection.close()
    self.listener.close()

def work(self, input_items, output_items):
    in0 = input_items[0]
    nbytes = len(in0)*self.itemsize
    nnext = nbytes // self.itemsize * self.itemsize
    last_pos = 0
    sent = 0
    remaining = nbytes
    rawbuffer = in0.data

    while nnext:
        sent =

```

self.connection.send(rawbuffer[last\_pos:last\_pos+nnext]) #um, yeah.  
if not sent: ## we couldn’t send a single byte →   
connection is dead  
self.connection.close()  
return -1 ## We’re done.  
remaining -= sent  
last\_pos += sent  
nnext = remaining // self.itemsize \* self.itemsize # to make  
sure we only send whole items  
return sent // self.itemsize

[1]  
[http://gnuradio.org/doc/doxygen/classgr\_1\_1blocks\_1\_1file\_\_descriptor\_\_sink.html](http://gnuradio.org/doc/doxygen/classgr_1_1blocks_1_1file __descriptor__ sink.html)  
[2]  
[https://github.com/gnuradio/gnuradio/blob/master/grc/grc\_gnuradio/blks2/tcp.py#L70](https://github.com/gnuradio/gnuradio/blob/master/grc/grc_gnuradio/blks2/tcp.py#L70)  
[3]

> <https://github.com/marcusmueller/gr-tcp/blob/master/python/tcp_sink.py>

really, just wrote this in a hurry. didn’t even care to syntax check.  
for illustration purposes only.

---

<div class="post-metadata">

**Author:** ![andreas\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@andreas\_l](https://www.ruby-forum.com/u/andreas_l)\
**Post date:** [May 18, 2015, 3:22pm UTC](https://www.ruby-forum.com/t/problem-tcp-blocks/241920/3 "2015-05-18T15:22:29Z")

</div>

* * *

Discuss-gnuradio mailing list  
removed\_email\_address@domain.invalid  
[https://lists.gnu.org/mailman/listinfo/discuss-gnuradio](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)
