Page 1 of 1

socket programming help

Posted: Wed Jul 16, 2003 7:09 am
by minds_gifts
Hello everybody,

Its been a while i have'nt worked on my project.Back here again.
I've written a small program where it requests the user to input some text through an HTML form and this creates a connection to the other pc in the network via the ip-address i mentioned in my code.The user i/p is sent from my machine to the other machine and return the value from the other machine(the same string but in reversed order) and is displayed to the user on an HTML page.

Errors: I dont get any text in the reverse order.
Windows NT is the environment.

Heres my code:

Code: Select all

<html> 
<head 
</head> 

<body> 
<?php 
//Form not yet submitted 
    if (!$submit) 
    { 
?> 
<form action="<? echo $PHP_SELF; ?>" method="post"> 
Enter some text:<br> 
<input type="Text" name="message" size="15"><input type="submit" name="submit" value="send"> 
</form> 
<? 
    } 
    else{ 
//form submitted 

//Where is the socket server? 
    $host="heres the ip-address of the machine I'm connecting"; 
    $port = portno; 
//create a socket 
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die ("could not create socket\n" ); 
//connect to server 
    $result = socket_connect($socket, $host, $port) or die ("could not connect to server\n" ); 

    socket_read ($socket, 1024) or die ("could not read server response\n" ); 
//send string to server 
    socket_write($socket, $message. strlen($message)) or die ("could not send data to server\n" ); 
//get server response 
    socket_read ($socket, 1024) or die ("could not read server response\n" ); 
//end session 
    socket_write($socket, "END", 3) or die ("could not end session\n" ); 
//close socket 
    socket_close($socket); 
//clean up result 
    $result = trim($result); 
    $result = substr($result, 0, strlen($result)-1); 
//print result to browser 
?> 
    Server said: <b><? echo $result; ?></b> 
<? 
    } 
?> 
</body> 
</html> 

 
Report Post | IP: Logged

Posted: Wed Jul 16, 2003 8:01 am
by Stoker
what do you have on the other machine to accept connections?

Posted: Wed Jul 16, 2003 9:21 am
by minds_gifts
what do you have on the other machine to accept connections?
I dont get you.Do you mean, should i have any other programme running or sort of a service??
Could you please explain me.
Thanks for your time.

Posted: Wed Jul 16, 2003 10:06 am
by Stoker
a client socket always connect to a server socket, so the machine you connect to must accept socket connections, "a server".. Just like a web server accepts connections on port 80 for http and FTP servers accepts on port 21 etc..

Your client is the plug - the server is the receptacle -- socket

Posted: Wed Jul 16, 2003 10:25 am
by minds_gifts
Stoker,

Its clear now.So, how should i check if the other machine accepts the connection and what steps should i take to run my scripts??

Thanks once again

Posted: Wed Jul 16, 2003 10:40 am
by Stoker
uhm.. what exactly are you trying to achieve?

It doesnt make sense to just try to connect to some other machine wihtout it running a service you set up for this specific purpose..

Posted: Wed Jul 16, 2003 11:19 am
by minds_gifts
OK, Heres the explanation of what I'm trying to do.

First, I would like to establich the connection between 2 computers in the network, IP-address being the input.
In one machine, I'll install Matlab software(This software is used for simulation purpose or for some mathematical calculations).Once, I'm connected, I would like to issue a command or send a file or a variable in order to run those scripts.Once, the scripts are executed, i would like to ge those results back to my computer.This is what I'm trying to do.

I've read the file system and sockets in the PHP manual and got some idea.In-order to communicate, I know that i need a service running in the other machine all the time.How can i write this service program or is it available on the web for free??
If somebody can guide me, i can give a try.

Thanks once again

Posted: Wed Jul 16, 2003 1:55 pm
by Stoker
I would say that your best approach would be using a web server on the other machine ,l ike apache (free) with php, then do not use sockets but standard http get or post...

e.g. on the "service" machine make a script that uses the parameteres in the url to perform calulations and return values..
So a simple client/server could be like

Code: Select all

<?php
  # The Client
  $result = file('http://theothermachine/scriptname.php?value1=2&value2=3');
  echo '<p>The server replied with '.count($result).' lines.<br>'."\n";
  foreach ($result as $line) echo htmlspecialchars($line)."<br>\n";
  echo "</p>\n";

  # The server
  header('Content-type: text/plain');
  echo $_REQUEST['value1']."\n";
  echo "+\n";
  echo $_REQUEST['value2']."\n";
  echo '='. (float) ($_REQUEST['value1']+$_REQUEST['value2']) ."\n";
  die();

?>
You could call the simplest form of a web-service...