[PHP] What is wrong with this code?

XUnreal

New Member
Messages
370
Reaction score
0
Points
0
I have had a problem trying to make a PHP Socket. The code is below:
PHP:
<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '{REMOVED}';
$port = 551;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
   echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
}

if (($ret = socket_bind($sock, $address, $port)) < 0) {
   echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}

if (($ret = socket_listen($sock, 5)) < 0) {
   echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}

do {
   if (($msgsock = socket_accept($sock)) < 0) {
	   echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
	   break;
   }
   do {
	 explode(" ", $buf)
	 if ($buf[0] == "LVER") {
	   $lver = "0.1"
	   if ($buf[1] < $lver) {
		 socket_write($msgsock, "OLD", "3");
	   }
	   if ($buf[1] > $lver) {
		 socket_write($msgsock, "NEW", "3");
	   }
	   if ($buf[1] == $lver) {
		 socket_write($msgsock, "NEW", "3");
	   }
	   }
	 }
	   if (false === ($buf = socket_read($msgsock, "550", PHP_NORMAL_READ))) {
		   echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
		   break 2;
	   }
	   if (!$buf = trim($buf)) {
		   continue;
	   }
	   if ($buf == 'quit') {
		   break;
	   }
	   if ($password == '{REMOVED}') {
		   socket_close($msgsock);
		   break 2;
	   }
   } while (true);
   socket_close($msgsock);
} while (true);

socket_close($sock);
?>
NedreN would probably know the answer but if anyone else knows PLEASE reply.

Thanks in advance,
- dphiance.

NOTE: Sorry about that other post, I accidently pressed Tab then Enter before I finished the post... Please delete the old post.
 
Last edited:

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Are you getting an error, or ? Any other information ? :)

-Corey
 

Cynical

Active Member
Messages
3,492
Reaction score
0
Points
36
I get this error:
ERRORRR'D said:
Parse error: parse error, unexpected T_IF in _debug_tmp.php on line 33
You are missing a ; on line 32 and 35. Then I get this error:
ERRORRR'D AGAIN said:
Parse error: parse error, unexpected T_IF, expecting T_WHILE in _debug_tmp.php on line 46
... which I don't know how to fix.

EDIT: 200th post! *lame*
 
Last edited:

XUnreal

New Member
Messages
370
Reaction score
0
Points
0
Doesnt matter, fixed it but I get this now:
Warning: socket_bind() unable to bind address [98]: Address already in use in /home/xunreal/public_html/socket.php on line 19

Auvee said it was fine to use Sockets, but I either get that error or "Access Denied".

Corey, Please help :(

Thanks in advance,
- dark0r.
 

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
I'm not really familiar with the permissions on that, see if you can find something online regarding it. I don't have time at the moment to look it up :)

-Corey
 

XUnreal

New Member
Messages
370
Reaction score
0
Points
0
Ok, thanks Corey. Is Auvee away or something? Hes not on MSN :|
 

Phil

Retired Staff
Messages
7,344
Reaction score
0
Points
36
XUnreal said:
Ok, thanks Corey. Is Auvee away or something? Hes not on MSN :|
He's on a leave from x10 for a while I know that.
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
I can help you out and attempt to see what is up. I can try in a few minutes, but I cannot promise. It might not be until I get home from school when I get the chance. I'll post back whenever that is.
 

flapietoetoe

New Member
Messages
226
Reaction score
0
Points
0
N/m i posted an error fix wich has been fixed already...
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
I don't get it? Do you still need help? (Him?) What?
 

XUnreal

New Member
Messages
370
Reaction score
0
Points
0
Yes I still need help. I get "Address in use" or "Access Denied"
 

chitwa

New Member
Messages
128
Reaction score
0
Points
0
All ports below 1024 (not very sure) are usually reserved for system or critical services e.g 25 is for SMTP. 98 could be for another service (will give it to you later am in cyber cafe at the moment) . Use something higher than 1024 for less conflicts. If you get access deneid, use something higher
 

Richard

Active Member
Messages
2,028
Reaction score
0
Points
36
I assume the address you are trying to connect to is outside the server? If so, its probably been blocked by the firewall.

Also most ports if not all below 1024 require root access on a linux server.
 

XUnreal

New Member
Messages
370
Reaction score
0
Points
0
its not even been connected to it sends the error when you start the script. I have also tried "4567" and that didn't work, including other numerous stuff.
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
I'm looking at it now, I'll see what I can come up with. Give me a few minutes.
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
XUnreal, I fixed the errors but am now stuck. I don't have "something" to test this on, and I don't know what I should. You removed the IP, so socket_bind() doesn't have an address to bind the socket with. I looked up what the port is used for, and that confused me.


cybercash 551/tcp cybercash
cybercash 551/udp cybercash

If you would, will you PM me the IP address so I can test this for you futher and see if I can find out what is up with it?


Edit: Nevermind actually.. I get the error with whatever I use.

Edit 2: The only thing I can think of is the address you are attempting to establish a connection to is not "accepting" them on the port you specify/at all.

Are you trying to do something with like the example(s) on here?

I'm really unsure of what the "Warning: socket_bind()" error is caused by.
 
Last edited:
Top