views:

167

answers:

3

Hi there,

I'm using php (the sockets extension) to handle sending and receiving xml files. I'd like to be able to fix the outgoing clients port number as the server has a set amount of incoming connections. I find that each time the php script is run it creates a new port number. The client side script I have so far is this:-



send_message('192.9.2.50','10220',$xmlCmd->asXML());

function send_message($ipaddr, $port, $msg)
{
  $fp = stream_socket_client("tcp://".$ipaddr.":".$port, $errno, $errstr);

  if (!$fp)
  {
    echo "ERR : $errno - $errstr";
  }
  else
  {
    fwrite($fp,$msg);
    $response = fread($fp,1024);
    // Make a SimpleXML object from the response
    $xml = new SimpleXMLElement($response);

    echo $xml->Channel->Air->Index;

    fclose($fp);
  }
}


A: 

http://ilia.ws/archives/51-PHP-bind-support-via-stream-context.html

mst
I tried using file_get_contents but this didn't seem to return an XML... I'll try again but am having trouble writing the options for stream_context_create... see below if you can help
jon
A: 

I'll try using file_get_contents again but the xml only seemed to pass from client to server ie no reply. Could anyone help me with the stream_context_create options, I need to combine these two but can't seem to get it right. Code:-


    $opts = array('http' => 
      array( 'method'  => 'POST',
        'header'  => 'Content-type: text/xml;',
        'content' => $msg)                             
    );
//combine with these options

$opts = array('socket'=>array('bindto'=>"192.9.2.60:2800"));
jon
A: 

Do a basic checklist on the network level:

  • Is 192.9.2.60 an IP on an actual interface on the server?
  • Are there any routing or firewall rules that affect it? In Linux try ``ip route get x.x.x.x src 192.9.2.60'' and iptables -L
  • Are there any other settings (eg SELinux) that interfere with PHP from binding to high ports?
  • Run a sniffer and see what the server response actually says
mst
Maybe I'm getting confused... I'd like to make the port on the client constant. If I ran the script in the first post the port number starts at 2600 or so and goes up by 3 every connection, the connection work though.Also I find that I didn't get a response using file_get_contents hence I used fwrite() and fread() as per the first script and they worked, don't know if this works with steam_context_create or not.I'll check if port binding is disabled somehow, but I'm still stuck with combining the two sets of stream_context_create options.
jon
I don't see why you would want to have a constant outgoing port. An IP address would make sense. Port not so much! It is hard for me to conceive a scenario in which this would be useful. I am not sure if binding to a specific outgoing port is even possible in any TCP stack.
mst
The application that I'm sending to has a finite number of connections and creates a new connection every time it receives something from the same ip but different port. I hoped to make the port constant and keep the connection alive using a heartbeat.. as the receiving application specifies. It's not a show stopper of the port can't be made constant, I can just set the timeout on the receiving application to 1 second or something. If it can't be done then no problem!
jon