tags:

views:

587

answers:

6

Hi guys

I need to create a TCP client using PHP to send commands to a server with an IP and a port. greetings

A: 

The option that gives you the most direct control is to use the Sockets extension (http://uk2.php.net/manual/en/ref.sockets.php). Note that this is not compiled into PHP by default so you may have to rebuild PHP to enable this.

If you have a more generic problem (SMTP/HTTP/NNTP/FTP/etc. client) then you are probably better off looking for a library or built in function to do it for you.

Mat
A: 

Check this example.

klausbyskov
A: 

I'm not sure why you'd want to use PHP for this sort of TCP/IP programming. Have a look at some other languages - they provide simple ways to accomplish this sort of requirement.

Helen Neely
Downvote. This isn't really a helpful answer - what if PHP is the only language the OP has available to him for this task?
richsage
+1  A: 

If you're familiar with file operations in PHP, you can just open a socket with fsockopen() and use its returned handle almost like a regular file handle.

Techpriester
A: 
function send_data($type,$host,$port='80',$path='/',$data='') { 
    $_err = 'lib sockets::'.__FUNCTION__.'(): '; 
    switch($type) { case 'http': $type = ''; case 'ssl': continue; default: die($_err.'bad $type'); } if(!ctype_digit($port)) die($_err.'bad port'); 
    if(!empty($data)) foreach($data AS $k => $v) $str .= urlencode($k).'='.urlencode($v).'&'; $str = substr($str,0,-1); 

    $fp = fsockopen($host,$port,$errno,$errstr,$timeout=30); 
    if(!$fp) die($_err.$errstr.$errno); else { 
        fputs($fp, "POST $path HTTP/1.1\r\n"); 
        fputs($fp, "Host: $host\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
        fputs($fp, "Content-length: ".strlen($str)."\r\n"); 
        fputs($fp, "Connection: close\r\n\r\n"); 
        fputs($fp, $str."\r\n\r\n"); 

        while(!feof($fp)) $d .= fgets($fp,4096); 
        fclose($fp); 
    } return $d; 
} 

usage example

send_data('http','somesite.com','80','filename.php',array('name'=>'somename','password'=>'passwordData'));
apis17
This is more a browser simulator. Something curl can do a lot faster/more efficiently. I believe he is looking for sending/receiving raw TCP messages to some sort of server.
St. John Johnson
owh. thank you for your suggestion. maybe that would help :) my example can be used where curl is not installed :)
apis17
A: 

Repost from duplicate question.

Here is a quick function I wrote to communicate with a TCP server.

  • $ip is the IP Address of the server
  • $port is the Port you are connection to
  • $data is the message you want to send

Let me know if you have any questions. Also take a look at the Sockets Section of the PHP Manual.

function socket_get($ip, $port, $data) {
  $output = "";

  // Create a TCP Stream Socket
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if ($socket === false)
    throw new Exception("Socket Creation Failed");

  // Connect to the server.
  $result = socket_connect($socket, $ip, $port);
  if ($result === false)
    throw new Exception("Connection Failed");

  // Write to socket!
  socket_write($socket, $data, strlen($data));

  // Read from socket!
  do {
    $line = socket_read($socket, 1024, PHP_NORMAL_READ);
    $output .= $line;
  } while ($line != "");

  // Close and return.
  socket_close($socket);
  return $output;
}
St. John Johnson