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.

PHP TCP Socket

Discussion in 'PHP' started by NITRO23456, Apr 1, 2018.

  1. #1
    Hi there

    Please see the screenshot below. I have a device at ip address 192.168.1.112 that connects and sends data to another device 192.168.1.100:8888. I can see this using a GUI Windows tool that I have (screenshot).

    upload_2018-4-2_13-28-22.png

    What I would like is a php script that connects, binds and listens to 192.168.1.100:8888 and extracts the data heard on that port and enters it into MySQL database.

    Can anyone help me with a script that will do this please?
     
    NITRO23456, Apr 1, 2018 IP
  2. AlphaNine_Vini

    AlphaNine_Vini Active Member

    Messages:
    218
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    88
    #2
    What is the format for your data and where is the mysql server hosted ? here only >> 192.168.1.100:8888
     
    AlphaNine_Vini, Apr 2, 2018 IP
  3. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    516
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    MySQL server is at localhost and the data is sent in HEX format. What I would like is to be able to read the data being sent to 192.168.1.100:8888 from 192.168.1.112

    To start with, how can I get this to even output in a browser?

    I have this code, but it just sits there in the browser but doesn't output anything:

    
    <?php
    /* Open a server socket to port 8888 */
    $server = stream_socket_server('tcp://192.168.1.100:8888');
    
    /* Accept a connection */
    $socket = stream_socket_accept($server);
    
    /* Grab a packet (1500 is a typical MTU size) of OOB data */
    echo "Received Out-Of-Band: '" . stream_socket_recvfrom($socket, 1500, STREAM_OOB) . "'\n";
    
    /* Take a peek at the normal in-band data, but don't comsume it. */
    echo "Data: '" . stream_socket_recvfrom($socket, 1500, STREAM_PEEK) . "'\n";
    
    /* Get the exact same packet again, but remove it from the buffer this time. */
    echo "Data: '" . stream_socket_recvfrom($socket, 1500) . "'\n";
    
    /* Close it up */
    fclose($socket);
    fclose($server);
    ?>
    
    Code (markup):
    I can see that a connection is being established using netstat in windows:

    upload_2018-4-3_16-36-46.png
     
    Last edited: Apr 2, 2018
    NITRO23456, Apr 2, 2018 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Try something like this:

    
    set_time_limit (0);
    
    $address ='46.49.41.188';
    
    $port =7777;
    $con =1;
    $word ="";
    
    $sock = socket_create(AF_INET, SOCK_STREAM,0);
    $bind = socket_bind($sock, $address, $port);
    
    socket_listen($sock);
    
    while($con ==1){
    $client = socket_accept($sock);
    $input = socket_read($client,2024);
    
    if($input =='exit'){
    $close = socket_close($sock);
    $con =0;}
    
    if($con ==1){
    $word .= $input;}}
    
    echo $word;
    
    Code (markup):
    Source: https://stackoverflow.com/questions/12999900/php-socket-listening-loop/13000206#13000206
     
    ThePHPMaster, Apr 3, 2018 IP
  5. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    516
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    This isn't showing anything either, the page just hangs. It establishes the connection OK, as I can see it as 'Established' in 'netstat' but nothing is echoed out to the browser.

    If I refresh, I just get the error below, which indicates the port is in use:

    Warning: socket_bind(): unable to bind address [10048]: Only one usage of each socket address (protocol/network address/port) is normally permitted
     
    Last edited: Apr 3, 2018
    NITRO23456, Apr 3, 2018 IP