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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Socket Programing

    I need help I have spent countless hours attempting to learn and I am about to pull my hair.

    My task that I want to accomplish is: Send a packet to the server and make the server ping back the client. I want to calculate the time it takes to complete the connection.

    I have been trying to send a packet form the client to the server. Receive it from the server and send it back to the client.

    I have code that I have been trying but it doesn't not work.

    Server:
    PHP Code:
    <?php

        
    //Create the socket on the specified port.
        
    $socket socket_create_listen($port);
        
        
    //Wait for incoming connections.
        
    $connection socket_accept($socket);
        
        
    //Create a socket and connect it to the server.
        
    $socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
        
    socket_connect($socket'localhost'33335);
        
        
        
    //Create a message, and send it to the server on $socket.
        
    $message "This is a message from the client.\n";
        
    socket_send($socket$messagestrlen($message), MSG_EOF);

        
    //Close the socket.
        
    socket_close($socket);
    ?>
    Client:
    PHP Code:
    <?php
        
    //Create a socket and connect it to the server.
        
    $socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
        
    socket_connect($socket'localhost'33335);
        
        
    //Create a message, and send it to the server on $socket.
        
    $message "This is a message from the client.\n";
        
    socket_send($socket$messagestrlen($message), MSG_EOF);

        
    //Close the socket.
        
    socket_close($socket);
    ?>

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,055
    Thanks
    10
    Thanked 94 Times in 92 Posts
    if you are using socket_create_listen() you don't then need to socket_create()

    this is a good example using socket_create_listen(), and start off using telnet or similar to test its working before working on the client..
    http://www.tuxradar.com/practicalphp/15/1/4
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    firepages is right. First create the deamon and test it from the command line. Once it is working fine, make the client application.

    You should test if the application actually runs and if it is able to listen on the port. Firewall /antivirus etc could block it.

    Also your scripts show no action being taken. You will have to write the message somewhere to verify that it is working.
    Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon


  •  

    Posting Permissions

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