Jump to content

Socket Programing


510carlos

Recommended Posts

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

//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_INET, SOCK_STREAM, SOL_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, $message, strlen($message), MSG_EOF);

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

 

Client:

<?php
//Create a socket and connect it to the server.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_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, $message, strlen($message), MSG_EOF);

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.