Jump to content

Php sockets - cant bind

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
19 replies to this topic

#1
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
So i was thinking of geting into php sockets, and i have 1 small problem...I cant seem to bind, i think its something to do with my PORT, becouse what i did is i echo $_SERVER['REMOTE_PORT'] so i could get the port, but everytime i refresh the page it changes -_-, any way heres the source code:

<?php


echo $_SERVER['REMOTE_PORT'];


// vars

  $host = "192.168.1.2";

  $port = "8080";

// dont time out

  set_time_limit(0);

// create socket

  $socket = socket_create(AF_INET,SOCK_STREAM,0) or die ("Could no create socket");

      

// bind socket to port

  $result = socket_bind($socket,$host,$port) or die ("Could not bind socket");

  

// start listening for connections 

  $result = socket_listen($socket,3) or ("Could not set up socket listener");  

  

// accept incoming connections 

// spawn another socket to handle communucation

 $spawn = socket_accept($socket) or die("Could not accept incoming connection");

 

// read clients 

 $input = socket_read($spawn,1024);  

 

// reverse client input and send back

 $output = strrev($input);

 

 socket_write($spawn,$output,strlen($output)) or die("could not write output");  

 

 socket_close($spawn);

 socket_close($socket);



?>

so yeah... when i load the page it gives me this error: Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\xampp\htdocs\sites\sockets\index.php on line 14
Could not bind socket

So yeah, i think its something todo with the port idk, please help :)

Edited by WingedPanther, 20 March 2009 - 01:33 PM.
add code tags (the PHP sheet)

Posted Image

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
It port 8080 already open on your computer? Try something random like 19423 instead. Nothing in your code strikes me as incorrect.

#3
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Oh, that port (8080),is just a random number i used, dont ask why...what i did, was, i got the port from when i echo $_SERVER['SERVER_REMOTE'] and used it...

So your saying i just need to add a random port number, then it should work?
Posted Image

#4
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Nope still not working :(, tried diffrent ports, still giving me the same error
Posted Image

#5
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Anyone? :(
Posted Image

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I can't execute your code on my Linux machine here because I didn't compile socket support into my PHP. Once I get onto a Windows machine I'll uncomment the .dll and try your code. As I said earlier, it doesn't look like anything is wrong with the code.

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
an idea worth testing. Do you have a personal firewall that blocks php:s access to open a socket?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Good thinking orjan, I didn't think about that.

#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Rather than 192.168.1.2 try 127.0.0.1

#10
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Hmmm...i tried 127.0.0.1, no luck with that...and no, im not behind a firewall either :(
Posted Image

#11
killcode

killcode

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
So jordan, did you get to test it out?
Posted Image

#12
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
To bind to certain ports you may need to be an administrator, so make sure you are not binding to a restricted port. I am not sure the range or even if it applies under Windows, but under Ubuntu I've needed root privileges to bind to certain ports. It's worth a shot.