Zócalos asíncronos vs síncronos

I have a question about asynchronous sockets. It's been bothering me for the last few months and thought I should wise up and get the facts from the pros here.

I have an incoming stream from a client which I want to take in the data. This incoming stream requires initial setup via strings before it will commence sending the data.

I have heard about the powerful scalability of asynchronous tcp servers like node.js. My question is whether can I use node.js or any other asynchronous tcp server to handshake with this client and receive the incoming stream.

You can think of the incoming data as an stream of stock data or electricity usage at a given point in time. In essence the data will be streaming in continuously.

Pls let me know what other information I need to share to help me understand asynchronous sockets and how to use it.

preguntado el 03 de mayo de 12 a las 15:05

If the data will be streamed continously, maybe it's better to use UDP? -

It's not possible because the client does not use udp -

1 Respuestas

Yes, it's perfectly normal to have handshaking implemented on top of asynchronous sockets.

Consider the pseudocode:

socket = accept() # or connect()
// now socket is connected
logon_response = socket.read()
assert(logon_response ok)
// now socket is logged in
handshake_response = socket.read()
assert(handshake_response ok)
// now socket is ready to go

while data = socket.read()
{    process data ... }

you can model this as a state machine, which is then easy to use with asynchronous sockets. See how the code below is analagous to that above, but broken into multiple callbacks:

# state Idle -> LoggingOn
    state = LoggingOn

# state LoggingOn -> Handshaking
    logon_response = socket.read()
    assert(logon_response ok)
    state = Handshaking

# state Handshaking -> Ready
    handshake_response = socket.read()
    assert(handshake_response ok)
    state = Ready

and all reads come in here, and get dispatched according to the state machine

    switch(state) {
    case LoggingOn: handle_logon_response(socket)
    case Handshaking: handle_handshake_response(socket)
    case Ready: process(socket.read())

PS. I'm having a major formatting failure here for some reason, hope it's readable

contestado el 04 de mayo de 12 a las 10:05

Thanks! Let me think about yr solution tonight. - Poliquín

Quick question, can this handle more than one concurrent sockets? - Poliquín

Yes, as many as you like: this is commonly used with select, poll or other synchronous event dispatchers. Just associate a separate state machine (and/or callbacks) with each socket. - Inútil

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.