I write a peer-to-peer messenger. After the two peers have established a connection, they want to know whether the other has sent him a message. If not, it sends a message itself. So I need a function that tells me how many bytes are still to be read on the socket. I looked in the documentation, but didn't find it.
When I was just asking the question I was redirected to Ruby TCPSocket: Find out how much data is available. However, I use an SSL socket and it doesn't seem to have .ready?.
Does anyone know a version of the function for the SSLSocket?
I would be very happy about answers!
Related
I am building a new application that receives data from a number of external devices and needs to make it available to a number of different components. ZeroMQ seems purpose-built for the "data bus" aspect of my architecture.
I recently became aware that zmq STREAM sockets can connect to native TCP sockets and send/received messages. Using zmq throughout has a lot of appeal, but I have one problem that I don't know how to get around.
One of my devices needs to be set up. That is, I connect a socket to it, send it some configuration information, then sit back and wait for it to send me data. The device also has a "reset" capability (useful in some contexts), that requires re-sending the configuration information. Doing this depends upon having visibility to the setup/tear-down stage of the socket interface. I need to know when a new connection is established, so I can send the necessary configuration messages.
It seems that zmq is purposely designed to shield me from that knowledge. Is there a way to do what I want? Or should I just use regular sockets for this interface?
Well, it turns out that reading (the right version of) the fine manual can be instructive.
When a connection is made, a zero-length message will be received by the application. Similarly, when the peer disconnects (or the connection is lost), a zero-length message will be received by the application.
I guess all that remains is to disambiguate between connect and disconnect. Still looking for advice from the community, if others have dealt with this situation before.
Following up on your own answer, I would hesitate to rely on that zero length connect/disconnect message as your whole strategy - that seems needlessly fragile. It's not clear to me from your question which end is persistent and which end needs configuration information, but I expect that one end knows it's resetting and reconnecting, and that end needs configuration information from the peer, so it should ask for it with a message when it needs it, to which the peer responds with the requested information.
If the peer does not yet have the required configuration information before it receives some other message, it could either queue up that work or it could respond back with the need for the config, and then have the rest of the network handle that need appropriately.
You shouldn't need stream/tcp sockets to make that work, it should work with more standard ZMQ socket types, you just need to build the robustness into your application rather than trying to get it for free from TCP/socket actions.
If I've missed your point, and what I'm suggesting won't work for some reason, you will have to give more specific information about your network topology for anyone else to understand what a suitable solution might be.
Recently I attended an interview, he asked this question
I am putting messages in Q. Manager, but client unable to get that messages, what is the problem can you explain it?
(All permission are ok, and put and get are enable state).
There are a 101 possible reasons. That is why MQ provides an MQRC back to the application, and further information in the AMQERR01.LOG. Without either of those you cannot even begin to guess. (P.S. I suspect that would have been a suitable reply in an interview!!)
But, since you ask for us to guess, here's a few more different from those Valerie suggested.
Perhaps the client channel max message length is shorter than the messages on the queue.
The codepage between client and queue manager may be such that data cannot be converted.
Client application get buffer isn't big enough
Hasn't specified accept truncated and the message was bigger than the buffer
AMS is in use and he's not the intended recipient (different from permissions)
This is a very broad question, would need to check error code received by client. Could be programming situation where client is getting based on a specific message or correl ID that does not exist. Could be that channel auth is blocking client. Also, it could be that the putting application did not commit the messages so they are not really available for the get.
Does anyone know if OpenToks sendSignal() method is peer to peer? Or does it get routed through OpenTok's servers? We are looking to send image and video files P2P, but the signaling method seems a bit slower than webRTC's native data channel. I'm wondering if there is something extra happening under the hood.
From the documentation, you should be able to send to a specific end: https://tokbox.com/developer/sdks/js/reference/Session.html#signal. What you need to do is to specify a Connection object.
I would like to know what is the best choice for a connection client-server.
I have to say I'm working with boost, and I'm not in trouble with how to to that, but when, for send, receive and connect. Could someone help-me please?
I want to create an application that sends and receives data parallel, like a chat application. It gets input and also sends some output, but NOT only if it receives data. I want to use UDP as protocol. I'm using ruby 1.9.3.
here's the code that receives data:
#s = UDPSocket.new
#s.bind(localhost, 1234)
Socket.udp_server_loop_on([#s]) do |message, sender|
#do something
end
This code should run independent from the rest of the application, it shouldn't block it.
Should I use a thread? I've never tried a network program and I'm not a professional developer, so please be patient. Perhaps my code/design is just crap, so feel free to tell me how this is done by professionals! ;)
UDP lends itself this sort of non-blocking processing quite naturally since you're receiving individual, atomic messages over your socket and can reply in the same fashion.
Inside that loop, just be sure to process things quickly and send response messages. If you make long blocking calls it will hold up your loop and affect response times.
EventMachine provides a structure for writing asynchronous applications and has its own methods for handling UDP and TCP sockets.
Don't forget to look at solutions which are already implemented. For chat applications, Socket.IO is a great place to start.
You should take a look at Eventmachine gem which handles blocking IO very efficiently.
Among others it also offers TCP and UDP server/client API.