Signal broadcasting using TCP - tcp

Is there any method to broadcast a signal or message in TCP? I know that for broadcasting UDP is used.
For example multiple clients connected with server and server sends a broadcast signal to all of connected clients at the same time. Is it possible using TCP?

No, TCP is connection oriented and designed for single end to end communications.

Related

NodeMCU broadcast to all clients

I want to broadcast a request to all client that are connected to my esp8266 12f access-point
I used this to create a connection per client, means if there're 3 clients it will create 3 connections.
for mac,ip in pairs(wifi.ap.getclient()) do
srv= net.createConnection(net.TCP, 0)
srv:on("receive", function(client, b_response) srv:close() collectgarbage() end)
srv:on("connection", function(client, b_request) client:send(request) end)
srv:connect(80, ip)
end
I tried the broadcast ip srv:connect(80, "255.255.255.255") but nothing was sent
The Problem :-
What I used that every srv will overwrite the previous srv so I can not get a response if it was delayed, even so I can name every srv with a different name like srv_1, srv_2, srv_3 but this take too much memory.
What I want
Create only one connection ?
Your code is using TCP, which is inherently a single connection, point-to-point protocol. There's no such thing as a "broadcast" TCP connection. TCP simply does not work using broadcast. That's like trying to use a car as a boat.
If you're sending a small amount of information, you might try UDP instead. The drawbacks are that UDP is unreliable - you can't be certain your message was received - and you'd need a lot more code to receive a response, if you want one, and you'd need to build a reliability mechanism (retransmit if no answer received, detect retransmissions in case the answer was dropped) if you care about that.
I'd recommend that you check out the MQTT protocol - it's designed to make it easy to communicate with multiple clients. It's lightweight and MQTT clients run well on NodeMCU and Arduino processors. There's an MQTT client built into NodeMCU's LUA implementation.
The downside is you'd need an MQTT broker that all of your NodeMCU's will connect to. The broker is usually run on a more capable processor (a Raspberry Pi is a good choice) or externally on the Internet (Adafruit offers a broker at https://io.adafruit.com/), although there are some implementations that run on an ESP8266.

Is there a way to send/receive Tcp messages without sustaining a connection and without reopening a connection each time?

I want to implement a basic message queue server. The server will be able to receive messages (byte arrays) and store them, and send messages to requesting clients.
I want to decide which protocol to base my custom protocol over.
Tcp seems natural for this kind of thing. But it seems that with Tcp, each time a client wants to send a message to the MQ server it has to establish a connection. It can't just 'send away' the message. Or, I can sustain a message between the client and MQ server, but I'm not sure that's a good idea.
So is there a way in Tcp to simply 'send a message' without establishing a connection each time? If not, what protocol should I use for this?
So is there a way in Tcp to simply 'send a message' without establishing a connection each time?
No.
If not, what protocol should I use for this?
UDP is a connectionless protocol, but it also lacks guaranteed sequencing and retransmission. You would have to build an ACK-based or NACK-based protocol over it.
What's your objection to TCP connections?

Is ZMQ TCP socket different from conventional TCP?

I am new to ZMQ. I find ZMQ socket implementation much simpler than winsock. But my doubt is that "can a client created using ZMQ TCP socket talk to conventional TCP server?" in other words Can my ZMQ client communicate with a TELNET server implemented using winsock?
No it cannot. ZeroMQ is a messaging library and is not just a pure socket. It uses its own protocol called ZMTP and both endpoints are required to understand it.
Yes you can. It's called a ZMQ_STREAM socket. Documentation is here: zmq_socket.txt
A socket of type ZMQ_STREAM is used to send and receive TCP data from a non-ØMQ peer, when using the tcp:// transport. A ZMQ_STREAM socket can act as client and/or server, sending and/or receiving TCP data asynchronously.
When receiving TCP data, a ZMQ_STREAM socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers.
When sending TCP data, a ZMQ_STREAM socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.

Can multiple sockets be associated with same port for UDP?

I think multiple sockets can be associated with same TCP port.
But can the same thing work for UDP?
The only way to associate multiple sockets with a port in TCP is by listening and then accepting.
The purpose in that case is to give every incoming client a unique socket so as to keep their byte streams separate.
You don't need that in the case of UDP because there are no byte streams. You can write an entire UDP server using a single UDP socket. You just read, despatch to a handler for that client, the handler writes the response back via the same socket.
Yes, it is also possible to have multiple sockets using a single UDP port.

how to forward udp packets over tcp (udptunnel)

how to use "udptunnel" so that i will be able to send and receive udp packets over tcp
probably with an example
You start udptunnel on two PCs in different networks and connect them as decribed here: http://www.cs.columbia.edu/~lennox/udptunnel/ with an example: http://wiki.leipzig.freifunk.net/Udptunnel

Resources