1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

telnet: connect to address xxx.x.x.x: Connection refused

Discussion in 'Site & Server Administration' started by Nestrer, Jan 6, 2008.

  1. #1
    Hi,

    I just wrote and ran a PHP socket server, but the telnet connection was refused.

    The SSH/Telnet command used:
    $ /usr/bin/php -q socketserver.php
    telnet xxx.x.x.x 9000

    The socketserver.php script:
    PHP:
    1. <?php
    2. // Set time limit to indefinite execution
    3.  
    4. // Set the ip and port we will listen on
    5. $address = 'xxx.x.x.x';
    6. $port = 9000;
    7.  
    8. // Create a TCP Stream socket
    9. $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    10. // Bind the socket to an address/port
    11. socket_bind($sock, $address, $port) or die('Could not bind to address');
    12. // Start listening for connections
    13.  
    14. /* Accept incoming requests and handle them as child processes */
    15. $client = socket_accept($sock);
    16.  
    17. // Read the input from the client – 1024 bytes
    18. $input = socket_read($client, 1024);
    19.  
    20. // Strip all white spaces from input
    21. $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
    22.  
    23. // Display output back to client
    24. socket_write($client, $output);
    25.  
    26. // Close the client (child) socket
    27. socket_close($client);
    28.  
    29. // Close the master sockets
    30. socket_close($sock);
    31. ?>
    Please help,

    Thanks
    Nestrer, Jan 6, 2008 IP
  2. boltok

    boltok Active Member

    Messages:
    257
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Can you firstly verify if the script is actually listening on that port? Try using netstat to see if this is true:

    netstat -nap | grep LISTEN
    boltok, Jan 6, 2008 IP
  3. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #3
    How to use netstat? It's an apache server, I tried the command using SSH, but:
    Sorry for being dumb..
    Nestrer, Jan 6, 2008 IP
  4. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just log into the server with SSH and type at the command prompt:
    netstat -tulpen | grep LISTEN

    If your server is actually running and listening on port 9000 you should see a line with an
    IP followed by :9000.
    norbert, Jan 6, 2008 IP
  5. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Yes, I did. But the result is:
    -sh: netstat: command not found
    Nestrer, Jan 6, 2008 IP
  6. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok, please make sure that you are root and that netstat is installed, e.g. try
    whereis netstat
    norbert, Jan 6, 2008 IP
  7. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #7
    What do you mean? type "whereis netstat" at the command prompt?

    I am at root, not sure if netstat is installed..

    Thanks
    Nestrer, Jan 6, 2008 IP
  8. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes exactly, type the following commands at the shell command prompt and let me know the output:
    whoami
    whereis netstat

    It would be helpful to know the system you are running, Gentoo, Debian, BSD or ...
    norbert, Jan 6, 2008 IP
  9. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #9
    whoami worked, whereis didn't.
    -sh: whereis: command not found

    The server type is Apache/2.0.52 (Red Hat)

    Thanks
    Nestrer, Jan 6, 2008 IP
  10. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    what was the output of whoami ?
    norbert, Jan 6, 2008 IP
  11. linsys

    linsys Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Do this :

    /bin/netstat -a | grep 9000

    If you receive /bin/netstat not found then there is a problem with your server I've ran 1,000s of red hat servers in my live and have never seen one without netstat installed. It should be part of the core install.
    linsys, Jan 6, 2008 IP
  12. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #12
    It output my user name.

    $ /bin/netstat -a | grep 9000
    -sh: /bin/netstat: No such file or directory

    I forgot to mention it's a virtual dedicated server.

    Thanks
    Nestrer, Jan 6, 2008 IP
  13. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You say that it output your username. So I assume you are not root? Type
    su -
    to become root, then verify with whoami and try again with netstat.
    When you type
    ls -lha /bin
    is there a line with netstat ?
    norbert, Jan 6, 2008 IP
  14. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #14
    $ su -
    -sh: su: command not found

    I can't find a line with netstat :(
    ls -lha /bin
    Nestrer, Jan 6, 2008 IP
  15. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    This is a bit strange, netstat and su should be available on the system. Have you noticed other strange behaviour of your server recently? Missing logs, crashed apps, unknown files, unusual amount of traffic or similar things?
    Does "su root" work, without the dash?

    Another way is to start your server and then type
    ps auxf
    Look for a line which contains socketserver.

    You could install nmap if it isn't already installed and then run
    nmap localhost
    to list the open ports
    norbert, Jan 7, 2008 IP
  16. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #16
    I don't find any strange behaviour of the server.

    "su root" didn't work either.. Tried "ps auxf" but there is no socketserver.
    Nestrer, Jan 7, 2008 IP
  17. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    if you started the server before your "ps auxf" and there is no line for it this simply means that the server is not running and thus the port 9000 is not open. That's why you can't connect with telnet.

    The code you posted looks a bit like it just runs through and finishes while it should run permanently like a daemon. You should check the PHP docu to find instructions how to write a permanently running server. The examples I found there contain "endless" while loops for example.

    http://www.php.net/manual/pl/function.stream-socket-server.php
    norbert, Jan 7, 2008 IP
    Nestrer likes this.
  18. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #18
    ...
    Just found what you need:
    http://devzone.zend.com/node/view/id/1086#Heading7
    norbert, Jan 7, 2008 IP
  19. Nestrer

    Nestrer Well-Known Member

    Messages:
    1,666
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    135
    #19
    Thanks norbert, I will test it, but how do I stop it using SSH?
    Nestrer, Jan 7, 2008 IP
  20. norbert

    norbert Guest

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #20
    As soon as you have a running server and find it with ps auxf look in the column "PID" which is the Process ID. Let's say it is 4281. Then you stop it with
    kill 4281
    or
    kill -9 4281
    norbert, Jan 7, 2008 IP