...

View Full Version : Socket Programing



510carlos
11-12-2012, 01:14 AM
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);
?>

firepages
11-12-2012, 02:56 PM
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

alemcherry
11-13-2012, 02:46 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum