Jump to content

Archived

This topic is now archived and is closed to further replies.

Wanderlei Silva

Warning: socket_bind() [function.socket-bind]: unable to bind address [0]:

Recommended Posts

Hi,

 

I am trying to open up a socket on an XAMP installation (Windows XP host) but keep receiving the following 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\submitscore.php on line 33

Only one usage of each socket address (protocol/network address/port) is normally permitted.

 

And this is the code:

<?php

define("SOCK_PORT", 8080);
define("SQL_HOST", "127.0.0.1");
define("SQL_USER", "root");
define("SQL_PASS", "");
define("SQL_DBASE", "my_db");


// Add Data function, takes data in form gameid.playerid.score and stores it in MySQL table 'scores'
function add_data( $data )
{
global $mysql;

// Parse $data
list($gameid, $playerid, $score) = explode(".", $data);

// Construct SQL Query
$sql = "INSERT INTO scores(gameid, playerid, score) VALUES(${gameid}, ${playerid}, ${score})";

// Run SQL Query
mysql_query( $sql, $mysql ) or die(mysql_error());
}

// Open MySQL and Select Database
$mysql = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die(mysql_error());
mysql_select_db(SQL_DBASE, $mysql) or die(mysql_error());

// Open UDP Socket
$sock = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ) or die(socket_strerror(socket_last_error($sock)));

// Bind Socket to SOCK_PORT on '0.0.0.0'
socket_bind( $sock, '0.0.0.0', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock)));
//socket_bind( $sock, '127.0.0.1', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock)));

// Set Socket to Blocking
socket_set_block( $sock ) or die(socket_strerror(socket_last_error($sock)));

// Loop forever, waiting for connections


while(TRUE) {
// Get first 65536 bytes from Socket and store as $buf (also get $clientip and $clientport)
socket_recvfrom( $sock, $buf, 65535, 0, $clientip, $clientport ) or die(socket_strerror(socket_last_error($sock)));

// Run add_data function on the data recieved from socket
add_data( $buf );
}

// Never Should reach here, but if it is changed to shut down: Close the socket & mysql gracefully
socket_close( $sock );
mysql_close( $mysql );


?> 

 

I figure using Port 8080 would be fine? I then I would enter the games score by passing it through the URL:

 

http://localhost/submitscore.php:8080

 

But when I try to load the page, I keep on getting the error at the top.

 

Could anyone provide a solution?

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

<?php
// Bind Socket to SOCK_PORT on '0.0.0.0'
//socket_bind( $sock, '0.0.0.0', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock))); ## Comment out this line
socket_bind( $sock, '127.0.0.1', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock))); ## Uncomment this line
?>

 

Future Use: The original uncommented line was trying to use the ip address 0.0.0.0 instead of loopback 127.0.0.1 (as per the commented line)

Share this post


Link to post
Share on other sites

Yes, I've tried that and it is still giing me the exact same error.

 

Do I need to configure Apache or anything?

 

The URLs I am attempting to view in my browser are:

 

https://127.0.0.1/submitscore.php

http://127.0.0.1/submitscore.php

http://localhost/submitscore:8080

http://127.0.0.1/script.php?d=11.22.33

 

But all give :

 

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\submitscore.php on line 33

Only one usage of each socket address (protocol/network address/port) is normally permitted.

 

 

netstat - ban command does have:

 

UDP    0.0.0.0:8080          *:*                                    5752

[apache.exe]

showing

Share this post


Link to post
Share on other sites

Have you tried changing define("SOCK_PORT", 8080); to straight 80 and leaving 127.0.0.1(my last post) the same?

Share this post


Link to post
Share on other sites

Thanks for the help iarp :)

 

I have just tried changing ports to port 80 and the exact same error is occurring.

 

I have added an echo to show the contents of the $sock variable, showing:

 

// Open MySQL and Select Database
$mysql = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die(mysql_error());
mysql_select_db(SQL_DBASE, $mysql) or die(mysql_error());

// Open UDP Socket
$sock = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ) or die(socket_strerror(socket_last_error($sock)));


///////////////////////
echo $sock;
/////////////////////

// Bind Socket to SOCK_PORT on '0.0.0.0'
//socket_bind( $sock, '0.0.0.0', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock)));
socket_bind( $sock, '127.0.0.1', SOCK_PORT ) or die(socket_strerror(socket_last_error($sock)));

echo $query;

// Set Socket to Blocking
socket_set_block( $sock ) or die(socket_strerror(socket_last_error($sock)));

 

The database and it's columns do exist inside phpmyadmin, I just cannot escape this error.

 

 

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.