Do I need 2 sockets for this setup or one will do?











up vote
0
down vote

favorite












I am implementing a small client that connects to a server for online rooms creation.



A client has an API for creating, joining rooms, etc on the server side. All these API invocations against the server wait for a response from the server, that is received in json format.



Now, on the other side, given one of these clients, I want to receive notifications of when another user joined the same room as me, asynchronously.



My guess is that this is not possible to do with a single socket and I will need at least 2 of them:




  • one socket for API invocations (since this already uses read/write on the socket) for request and response

  • one socket to receive notifications


My questions are:




  1. is there a way to do this with a single socket in a reasonable way?
    or

  2. it is mandatory to use at least 2 sockets in this setup?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am implementing a small client that connects to a server for online rooms creation.



    A client has an API for creating, joining rooms, etc on the server side. All these API invocations against the server wait for a response from the server, that is received in json format.



    Now, on the other side, given one of these clients, I want to receive notifications of when another user joined the same room as me, asynchronously.



    My guess is that this is not possible to do with a single socket and I will need at least 2 of them:




    • one socket for API invocations (since this already uses read/write on the socket) for request and response

    • one socket to receive notifications


    My questions are:




    1. is there a way to do this with a single socket in a reasonable way?
      or

    2. it is mandatory to use at least 2 sockets in this setup?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am implementing a small client that connects to a server for online rooms creation.



      A client has an API for creating, joining rooms, etc on the server side. All these API invocations against the server wait for a response from the server, that is received in json format.



      Now, on the other side, given one of these clients, I want to receive notifications of when another user joined the same room as me, asynchronously.



      My guess is that this is not possible to do with a single socket and I will need at least 2 of them:




      • one socket for API invocations (since this already uses read/write on the socket) for request and response

      • one socket to receive notifications


      My questions are:




      1. is there a way to do this with a single socket in a reasonable way?
        or

      2. it is mandatory to use at least 2 sockets in this setup?










      share|improve this question













      I am implementing a small client that connects to a server for online rooms creation.



      A client has an API for creating, joining rooms, etc on the server side. All these API invocations against the server wait for a response from the server, that is received in json format.



      Now, on the other side, given one of these clients, I want to receive notifications of when another user joined the same room as me, asynchronously.



      My guess is that this is not possible to do with a single socket and I will need at least 2 of them:




      • one socket for API invocations (since this already uses read/write on the socket) for request and response

      • one socket to receive notifications


      My questions are:




      1. is there a way to do this with a single socket in a reasonable way?
        or

      2. it is mandatory to use at least 2 sockets in this setup?







      sockets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 19:25









      Germán Diago

      4,97312744




      4,97312744
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Having the client allocate just one socket (and connect a single TCP connection) is sufficient, provided you have a well-defined messaging protocol between your client and server.



          In particular, I suspect that you perceive a need for a second TCP socket connection primarily because you're unsure about how a single-socket solution would be able to handle multiple different kinds of operations (i.e. RPC calls and asynchronous notifications) properly, without one type of operations interfering with the functionality of the other.



          In a purely message-passing design, handling everything via a single socket is straightforward -- when your client wants to send a message to the server, it calls your SendMessage() function (or whatever you call it), and your code queues up the bytes to be sent across the socket; conversely, when the server sends a message to your client, your I/O code receives the bytes, and if all of the bytes of the message have been received, it parses them and calls your appropriate callback function (MessageReceived() or whatever) to react to them appropriately. Since your client never blocks anywhere (other than perhaps in some WaitForNextEvent()-type-call such as select() or poll()), integrating multiple simultaneous tasks is not a problem.



          In general, I recommend against RPC-style semantics (i.e. where your client code calls a function-call that hides a network operation and the function-call doesn't return until a reply has been received back from the server) because it leaves your client at the mercy of the server and the network's connectivity -- in particular it will cause your client thread to freeze up (blocked in an RPC-call) whenever there is a temporary network outage, which is not a very good user experience.



          That said, if you must use RPC-style semantics, asynchronous-notifications are still possible with just one socket; you just have to include code inside your RPC-functions that gives them the ability to call an appropriate callback-function (i.e. SomeAsynchronousEventOccured() or whatever you like to call it) when they receive an asynchronous-event-occurred message from the socket. Note that that means that the asynchronous-callback function can get called before your RPC-function returns its network-provided result, which might be a bit surprising to some programmers, so be sure to document that it is a possibility. (At least it still gets called inside the same thread, so race conditions won't be an issue, though re-entrancy problems might be)






          share|improve this answer























          • Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
            – Germán Diago
            Nov 11 at 19:57






          • 1




            Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
            – Jeremy Friesner
            Nov 11 at 20:03










          • Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
            – Germán Diago
            Nov 11 at 20:03










          • Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
            – Jeremy Friesner
            Nov 11 at 20:04










          • Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
            – Germán Diago
            Nov 11 at 20:05


















          up vote
          1
          down vote













          You need only one connection between the server and each client. That connection can transmit messages in both directions (API calls and notifications).



          Most socket APIs work like that: You open a port on the server by creating a listening socket. Each client that connects becomes another socket. That socket represents the connection to that client. Likely, you need to maintain a list of all connected clients. There needs to be a single socket bound to an open port.






          share|improve this answer





















          • I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
            – Germán Diago
            Nov 11 at 19:35












          • @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
            – usr
            Nov 11 at 20:05










          • I am not new actually. Just not an expert at the task :)
            – Germán Diago
            Nov 11 at 20:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252363%2fdo-i-need-2-sockets-for-this-setup-or-one-will-do%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Having the client allocate just one socket (and connect a single TCP connection) is sufficient, provided you have a well-defined messaging protocol between your client and server.



          In particular, I suspect that you perceive a need for a second TCP socket connection primarily because you're unsure about how a single-socket solution would be able to handle multiple different kinds of operations (i.e. RPC calls and asynchronous notifications) properly, without one type of operations interfering with the functionality of the other.



          In a purely message-passing design, handling everything via a single socket is straightforward -- when your client wants to send a message to the server, it calls your SendMessage() function (or whatever you call it), and your code queues up the bytes to be sent across the socket; conversely, when the server sends a message to your client, your I/O code receives the bytes, and if all of the bytes of the message have been received, it parses them and calls your appropriate callback function (MessageReceived() or whatever) to react to them appropriately. Since your client never blocks anywhere (other than perhaps in some WaitForNextEvent()-type-call such as select() or poll()), integrating multiple simultaneous tasks is not a problem.



          In general, I recommend against RPC-style semantics (i.e. where your client code calls a function-call that hides a network operation and the function-call doesn't return until a reply has been received back from the server) because it leaves your client at the mercy of the server and the network's connectivity -- in particular it will cause your client thread to freeze up (blocked in an RPC-call) whenever there is a temporary network outage, which is not a very good user experience.



          That said, if you must use RPC-style semantics, asynchronous-notifications are still possible with just one socket; you just have to include code inside your RPC-functions that gives them the ability to call an appropriate callback-function (i.e. SomeAsynchronousEventOccured() or whatever you like to call it) when they receive an asynchronous-event-occurred message from the socket. Note that that means that the asynchronous-callback function can get called before your RPC-function returns its network-provided result, which might be a bit surprising to some programmers, so be sure to document that it is a possibility. (At least it still gets called inside the same thread, so race conditions won't be an issue, though re-entrancy problems might be)






          share|improve this answer























          • Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
            – Germán Diago
            Nov 11 at 19:57






          • 1




            Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
            – Jeremy Friesner
            Nov 11 at 20:03










          • Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
            – Germán Diago
            Nov 11 at 20:03










          • Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
            – Jeremy Friesner
            Nov 11 at 20:04










          • Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
            – Germán Diago
            Nov 11 at 20:05















          up vote
          2
          down vote



          accepted










          Having the client allocate just one socket (and connect a single TCP connection) is sufficient, provided you have a well-defined messaging protocol between your client and server.



          In particular, I suspect that you perceive a need for a second TCP socket connection primarily because you're unsure about how a single-socket solution would be able to handle multiple different kinds of operations (i.e. RPC calls and asynchronous notifications) properly, without one type of operations interfering with the functionality of the other.



          In a purely message-passing design, handling everything via a single socket is straightforward -- when your client wants to send a message to the server, it calls your SendMessage() function (or whatever you call it), and your code queues up the bytes to be sent across the socket; conversely, when the server sends a message to your client, your I/O code receives the bytes, and if all of the bytes of the message have been received, it parses them and calls your appropriate callback function (MessageReceived() or whatever) to react to them appropriately. Since your client never blocks anywhere (other than perhaps in some WaitForNextEvent()-type-call such as select() or poll()), integrating multiple simultaneous tasks is not a problem.



          In general, I recommend against RPC-style semantics (i.e. where your client code calls a function-call that hides a network operation and the function-call doesn't return until a reply has been received back from the server) because it leaves your client at the mercy of the server and the network's connectivity -- in particular it will cause your client thread to freeze up (blocked in an RPC-call) whenever there is a temporary network outage, which is not a very good user experience.



          That said, if you must use RPC-style semantics, asynchronous-notifications are still possible with just one socket; you just have to include code inside your RPC-functions that gives them the ability to call an appropriate callback-function (i.e. SomeAsynchronousEventOccured() or whatever you like to call it) when they receive an asynchronous-event-occurred message from the socket. Note that that means that the asynchronous-callback function can get called before your RPC-function returns its network-provided result, which might be a bit surprising to some programmers, so be sure to document that it is a possibility. (At least it still gets called inside the same thread, so race conditions won't be an issue, though re-entrancy problems might be)






          share|improve this answer























          • Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
            – Germán Diago
            Nov 11 at 19:57






          • 1




            Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
            – Jeremy Friesner
            Nov 11 at 20:03










          • Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
            – Germán Diago
            Nov 11 at 20:03










          • Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
            – Jeremy Friesner
            Nov 11 at 20:04










          • Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
            – Germán Diago
            Nov 11 at 20:05













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Having the client allocate just one socket (and connect a single TCP connection) is sufficient, provided you have a well-defined messaging protocol between your client and server.



          In particular, I suspect that you perceive a need for a second TCP socket connection primarily because you're unsure about how a single-socket solution would be able to handle multiple different kinds of operations (i.e. RPC calls and asynchronous notifications) properly, without one type of operations interfering with the functionality of the other.



          In a purely message-passing design, handling everything via a single socket is straightforward -- when your client wants to send a message to the server, it calls your SendMessage() function (or whatever you call it), and your code queues up the bytes to be sent across the socket; conversely, when the server sends a message to your client, your I/O code receives the bytes, and if all of the bytes of the message have been received, it parses them and calls your appropriate callback function (MessageReceived() or whatever) to react to them appropriately. Since your client never blocks anywhere (other than perhaps in some WaitForNextEvent()-type-call such as select() or poll()), integrating multiple simultaneous tasks is not a problem.



          In general, I recommend against RPC-style semantics (i.e. where your client code calls a function-call that hides a network operation and the function-call doesn't return until a reply has been received back from the server) because it leaves your client at the mercy of the server and the network's connectivity -- in particular it will cause your client thread to freeze up (blocked in an RPC-call) whenever there is a temporary network outage, which is not a very good user experience.



          That said, if you must use RPC-style semantics, asynchronous-notifications are still possible with just one socket; you just have to include code inside your RPC-functions that gives them the ability to call an appropriate callback-function (i.e. SomeAsynchronousEventOccured() or whatever you like to call it) when they receive an asynchronous-event-occurred message from the socket. Note that that means that the asynchronous-callback function can get called before your RPC-function returns its network-provided result, which might be a bit surprising to some programmers, so be sure to document that it is a possibility. (At least it still gets called inside the same thread, so race conditions won't be an issue, though re-entrancy problems might be)






          share|improve this answer














          Having the client allocate just one socket (and connect a single TCP connection) is sufficient, provided you have a well-defined messaging protocol between your client and server.



          In particular, I suspect that you perceive a need for a second TCP socket connection primarily because you're unsure about how a single-socket solution would be able to handle multiple different kinds of operations (i.e. RPC calls and asynchronous notifications) properly, without one type of operations interfering with the functionality of the other.



          In a purely message-passing design, handling everything via a single socket is straightforward -- when your client wants to send a message to the server, it calls your SendMessage() function (or whatever you call it), and your code queues up the bytes to be sent across the socket; conversely, when the server sends a message to your client, your I/O code receives the bytes, and if all of the bytes of the message have been received, it parses them and calls your appropriate callback function (MessageReceived() or whatever) to react to them appropriately. Since your client never blocks anywhere (other than perhaps in some WaitForNextEvent()-type-call such as select() or poll()), integrating multiple simultaneous tasks is not a problem.



          In general, I recommend against RPC-style semantics (i.e. where your client code calls a function-call that hides a network operation and the function-call doesn't return until a reply has been received back from the server) because it leaves your client at the mercy of the server and the network's connectivity -- in particular it will cause your client thread to freeze up (blocked in an RPC-call) whenever there is a temporary network outage, which is not a very good user experience.



          That said, if you must use RPC-style semantics, asynchronous-notifications are still possible with just one socket; you just have to include code inside your RPC-functions that gives them the ability to call an appropriate callback-function (i.e. SomeAsynchronousEventOccured() or whatever you like to call it) when they receive an asynchronous-event-occurred message from the socket. Note that that means that the asynchronous-callback function can get called before your RPC-function returns its network-provided result, which might be a bit surprising to some programmers, so be sure to document that it is a possibility. (At least it still gets called inside the same thread, so race conditions won't be an issue, though re-entrancy problems might be)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 19:58

























          answered Nov 11 at 19:54









          Jeremy Friesner

          38k1078159




          38k1078159












          • Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
            – Germán Diago
            Nov 11 at 19:57






          • 1




            Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
            – Jeremy Friesner
            Nov 11 at 20:03










          • Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
            – Germán Diago
            Nov 11 at 20:03










          • Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
            – Jeremy Friesner
            Nov 11 at 20:04










          • Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
            – Germán Diago
            Nov 11 at 20:05


















          • Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
            – Germán Diago
            Nov 11 at 19:57






          • 1




            Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
            – Jeremy Friesner
            Nov 11 at 20:03










          • Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
            – Germán Diago
            Nov 11 at 20:03










          • Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
            – Jeremy Friesner
            Nov 11 at 20:04










          • Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
            – Germán Diago
            Nov 11 at 20:05
















          Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
          – Germán Diago
          Nov 11 at 19:57




          Actually my client is synchronous (I did not want to complicate things much). So I have more or less something like main thread calling SendMessage() and another thread trying to receive the notification in the same socket. But it can happen that it happens during a reply. I want to keep it simple but I am not sure I will be able to do it :)
          – Germán Diago
          Nov 11 at 19:57




          1




          1




          Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
          – Jeremy Friesner
          Nov 11 at 20:03




          Beware of the wallpaper-bubble-effect, where you try to avoid complexity in one place, only to have the complexity pop up elsewhere instead :) (in particular, whenever I've tried to go with a synchronous/blocking/multithreaded design, I ended up with responsiveness-problems whenever the network or server didn't respond promptly, and difficulty getting the client program to quit cleanly and reliably--due to the fact that the client was "stuck" in a blocking function somewhere. In the end I found that a single-threaded/non-blocking design was more reliable, despite its initial complexity)
          – Jeremy Friesner
          Nov 11 at 20:03












          Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
          – Germán Diago
          Nov 11 at 20:03




          Basically I am sending json, receiving json and will get json from async notifications. I receive in the response (data_length, json_data) and in notifications also. maybe should add another field to distinguish both kinds?
          – Germán Diago
          Nov 11 at 20:03












          Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
          – Jeremy Friesner
          Nov 11 at 20:04




          Having a field that clearly and unambiguously describes what the message's purpose is, is always a good idea :)
          – Jeremy Friesner
          Nov 11 at 20:04












          Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
          – Germán Diago
          Nov 11 at 20:05




          Jeremy, I do agree with your assessment, but my strategy at this point is to have a prototype quick and dirty to enable finishing a prototype of a bigger product overall. After that I agree that for a robust solution maybe it is . better an async design directly.
          – Germán Diago
          Nov 11 at 20:05












          up vote
          1
          down vote













          You need only one connection between the server and each client. That connection can transmit messages in both directions (API calls and notifications).



          Most socket APIs work like that: You open a port on the server by creating a listening socket. Each client that connects becomes another socket. That socket represents the connection to that client. Likely, you need to maintain a list of all connected clients. There needs to be a single socket bound to an open port.






          share|improve this answer





















          • I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
            – Germán Diago
            Nov 11 at 19:35












          • @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
            – usr
            Nov 11 at 20:05










          • I am not new actually. Just not an expert at the task :)
            – Germán Diago
            Nov 11 at 20:06















          up vote
          1
          down vote













          You need only one connection between the server and each client. That connection can transmit messages in both directions (API calls and notifications).



          Most socket APIs work like that: You open a port on the server by creating a listening socket. Each client that connects becomes another socket. That socket represents the connection to that client. Likely, you need to maintain a list of all connected clients. There needs to be a single socket bound to an open port.






          share|improve this answer





















          • I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
            – Germán Diago
            Nov 11 at 19:35












          • @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
            – usr
            Nov 11 at 20:05










          • I am not new actually. Just not an expert at the task :)
            – Germán Diago
            Nov 11 at 20:06













          up vote
          1
          down vote










          up vote
          1
          down vote









          You need only one connection between the server and each client. That connection can transmit messages in both directions (API calls and notifications).



          Most socket APIs work like that: You open a port on the server by creating a listening socket. Each client that connects becomes another socket. That socket represents the connection to that client. Likely, you need to maintain a list of all connected clients. There needs to be a single socket bound to an open port.






          share|improve this answer












          You need only one connection between the server and each client. That connection can transmit messages in both directions (API calls and notifications).



          Most socket APIs work like that: You open a port on the server by creating a listening socket. Each client that connects becomes another socket. That socket represents the connection to that client. Likely, you need to maintain a list of all connected clients. There needs to be a single socket bound to an open port.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 19:30









          usr

          143k26185297




          143k26185297












          • I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
            – Germán Diago
            Nov 11 at 19:35












          • @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
            – usr
            Nov 11 at 20:05










          • I am not new actually. Just not an expert at the task :)
            – Germán Diago
            Nov 11 at 20:06


















          • I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
            – Germán Diago
            Nov 11 at 19:35












          • @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
            – usr
            Nov 11 at 20:05










          • I am not new actually. Just not an expert at the task :)
            – Germán Diago
            Nov 11 at 20:06
















          I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
          – Germán Diago
          Nov 11 at 19:35






          I am not sure I explained the question well. I am talking about the client. The server already gets one open connection per client. But the client needs to execute api calls against the server. The server replies to those calls. But, additionally, on the client side, I need to receive asynchronous notifications (for example if another client connected from another place and joined my room)
          – Germán Diago
          Nov 11 at 19:35














          @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
          – usr
          Nov 11 at 20:05




          @GermánDiago yes, I seem to have misunderstood. But again, you need just one connection. Define a binary protocol to be able to send request/reply pairs. That protocol would be symmetric. The server can call the client and the client can call the server. Better yet, use one of the ready-made protocols and libraries. Writing socket code is extremely hard if you are new to it.
          – usr
          Nov 11 at 20:05












          I am not new actually. Just not an expert at the task :)
          – Germán Diago
          Nov 11 at 20:06




          I am not new actually. Just not an expert at the task :)
          – Germán Diago
          Nov 11 at 20:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252363%2fdo-i-need-2-sockets-for-this-setup-or-one-will-do%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine