Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Server receives only Chars. Need full String

    Dear Community
    I'm pretty new to Javascript and have been working on a little chit-chat Client-Server Software.

    I'm currently working on a Server that should accept 3 types of Strings, these Strings look like this:
    "StillAlive"
    "AddConnection"
    "Shutdown"

    My problem is that the Server only accepts ONE Char, it takes this char as the "default" string (which activates the AddConnection method) and immediately closes the connection to the client.
    This leads to an error shown at the Client because he has been interrupted pretty suddenly.

    The Server (Receive) Method looks like this:

    Code:
    function RunServer()
    {
      // Die Funktion "sock" die der net.createServer() Methode mitgegeben wird stellt den handler dar
    // Jede Verbindung erzeugt ein einzigartiges SOCK Objekt
    net.createServer(function(sock) {
       
        // Neue Verbindung. Hiermit wurde ein SOCK Objekt automatisch erstellt
        console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
       
        // Jede Socket-Instanz bekommt einen EventHandler "data"
        sock.on('data', function(data) {
            
           console.log('DATA ' + sock.remoteAddress + ': ' + data); //Empfange String
           if (data == 'Shutdown') //UNSECURE
           {
           ClearTemp();
           console.log('Exiting');
                         process.exit(code=0);
           }
           if (data == 'StillAlive')
           {
               sock.write('YES');
               sock.end();
           }
          DoStuff();
            sock.end(); //Close Socket
          console.log('Socket Closed');
            }
            
        });
        
        // Ein Eventhandler für das Schließen am Socket
        sock.on('close', function(data) {
            console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
        });
        
    }).listen(PORT, HOST);
    
    console.log('Server listening on ' + HOST +':'+ PORT);  
    
    };
    I hope someone can explain me why it accepts only one char and how i can resolve this problem.

    Thank you very much in advance!

  • #2
    New Coder
    Join Date
    Aug 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your problem my be in the database, the default value of AddConnection may be set as the default value, example (1) and the other's may be (0),

    try making the code more complex, like setting the default value and making it get the default value and if default value = blar blar then do funktion else repeat then die and show error or something

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NoobCoder101 View Post
    your problem my be in the database, the default value of AddConnection may be set as the default value, example (1) and the other's may be (0),

    try making the code more complex, like setting the default value and making it get the default value and if default value = blar blar then do funktion else repeat then die and show error or something
    The Problem is not "the triggering of the AddConnection Event", my Problem is that the Server-Method only accepts 1 Char, then triggers a methos.
    I want it to receive a Whole string, not Char by Char

  • #4
    New Coder
    Join Date
    Aug 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    have you tried Visual Basic for client to server program's ? good info on how to slove these problem's

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,102
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    I don't understand how that code works in any system.
    Code:
    console.log('CONNECTED+ sock.remoteAddress +':'+ sock.remotePort);
    I count *THREE* ' marks in there. Where is the fourth one?

    Shouldn't it be
    Code:
    console.log('CONNECTED ' + sock.remoteAddress +':'+ sock.remotePort);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,102
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    NEVER MIND. It looks like this idiot forum software is messing up stuff in [code] tags yet again.

    You might try using [php] tags. But if that still doesn't work, just attach the code as a .txt file.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,390
    Thanks
    11
    Thanked 295 Times in 294 Posts
    There are many places in that code where one or three quotes exist in a line of code. Definitely something wacko going on!

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,102
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    I've had this happen recently to me MANY times. It is definitely the forum software going off the deep end.

    [php] tags seem to work better, but they have limitations I don't like.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    data() is an event than means data is arriving, not that it's done. in this case, maybe only the first char has arrived when you try to do the compare.
    you should be collecting the data that arrives in this event to use once it's all arrived.

    i think you want the close() or end() event instead of the data() event.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,102
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Oh, lordy, I remember being bit by this very thing AT LEAST 20 years ago. (I think 23 years.)

    And, yes, I used the wrong event (heh...except this was in C and not at all nicely OO coded).

    I'd forgotten all about that. Selective memory! Or maybe it's just senility starting in.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •