KingOfHeart Posted October 13, 2010 Share Posted October 13, 2010 I'm trying to use sockets to see if I can connect to a chatroom. I got it started but not sure where to go from here. <? $host = "79.99.135.253"; $port = 6667; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $data = "NICK OpenZelda"; $data .= chr(13); $data .= 'USER whoever "thegaminguniverse.com" "irc.thegaminguniverse.nl" :OZ Website'; $data .= chr(13); socket_connect($socket, $host, $port); socket_sendto($socket, $data, strlen($data), 0,$host,$port); socket_recv($socket, $buffer, 1024, 0); echo $buffer; ?> I managed to receive one message. However I know for a fact there were two messages sent back. So how do I set it up to keep receiving messages? Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 13, 2010 Author Share Posted October 13, 2010 Update! I got it to stay connected and receive messages. Now I wonder how I can send messages without disconnecting. <? $host = "79.99.135.253"; $port = 6667; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $data = "NICK Freddy_Krueger"; $data .= chr(13); $data .= 'USER Freddy_Krueger "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer'; $data .= chr(13); socket_connect($socket, $host, $port); socket_sendto($socket, $data, strlen($data), 0,$host,$port); $join; while(true) { socket_recv($socket, $buffer, 1024, 0); if(strlen($buffer) > 0) echo "Recieved: $buffer<br>"; $pingpos = strpos($buffer,"PING :"); $pingcode; if($pingpos !== false) { for($n = $pingpos + 6; $n < strlen($buffer); $n++) { if($buffer[$n] == 13 || $buffer[$n] == 10) break; $pingcode .= $buffer[$n]; } $data = "PONG :" . $pingcode . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; if($join == false) { $data = "JOIN #oz" . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 15, 2010 Author Share Posted October 15, 2010 When I use an include file to send a message it takes it forever to send. However receiving messages is instant. Reading a file takes just as long. So what method can I use to send out messages just as fast as receiving them? Quote Link to comment Share on other sites More sharing options...
btherl Posted October 15, 2010 Share Posted October 15, 2010 What file are you including to send the message? I don't understand how including a file would slow down sending data over a socket. Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 16, 2010 Author Share Posted October 16, 2010 Just a file with a single variable. I have the word include within the loop. I'm thinking it just takes it a while to understand it's updated. Do you think a submit button would work for submitting data? How would you script a simple forum with a submit button that uses java script? Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 16, 2010 Author Share Posted October 16, 2010 I notice it does not loop until I receive a message. socket_recv($socket, $buffer, 1024, 0); < So this is wrong. I tried changing the flag number but either got a flood or an error. Quote Link to comment Share on other sites More sharing options...
btherl Posted October 16, 2010 Share Posted October 16, 2010 It sounds like you want the MSG_DONTWAIT flag, aka "non-blocking". That will let your code continue even if there's nothing to read from the socket. Have you looked at socket_select() ? That's one way to deal with the problem of knowing when to read and when to write to a socket. I still don't know what you mean with "include". Can you show me that code? Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 16, 2010 Author Share Posted October 16, 2010 <? $host = "79.99.135.253"; $port = 6667; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $data = "NICK Freddy_Krueger2"; $data .= chr(13); $data .= 'USER Freddy_Krueger2 "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer'; $data .= chr(13); socket_connect($socket, $host, $port); socket_sendto($socket, $data, strlen($data), 0,$host,$port); $join; $chatmsg; while(true) { include "msgs.php"; if($join) { echo "<u>" . $msgs . "| $nomsg</u><br>"; if($msgs != $chatmsg) { $chatmsg = $msgs; $data = "PRIVMSG #oz :" . $chatmsg . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $chatmsg<br>"; } } $sock = array($socket,$socket); if(socket_select($sock[0],$write = null, $except = null, null) == 0) continue; socket_recv($socket, $buffer, 1024, 1); if(strlen($buffer) == 0) continue; echo "Recieved: $buffer<br>"; $nomsg = 0; $pingpos = strpos($buffer,"PING :"); $pingcode; if($pingpos !== false) { for($n = $pingpos + 6; $n < strlen($buffer); $n++) { if($buffer[$n] == 13 || $buffer[$n] == 10) break; $pingcode .= $buffer[$n]; } $data = "PONG :" . $pingcode . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; if($join == false) { $data = "JOIN #oz" . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; $join = true; } } } ?> include "msgs.php"; < This is what I meant when I said include. The java part can wait a bit, just want to get it to send and receive properly first. I don't think socketselect will work for just one socket. Even when I made it an array it still didn't work. Quote Link to comment Share on other sites More sharing options...
KingOfHeart Posted October 16, 2010 Author Share Posted October 16, 2010 I don't think socket select was needed as I got it to loop much faster now. <? $host = "79.99.135.253"; $port = 6667; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $data = "NICK Freddy_Krueger2"; $data .= chr(13); $data .= 'USER Freddy_Krueger2 "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer'; $data .= chr(13); socket_connect($socket, $host, $port); socket_sendto($socket, $data, strlen($data), 0,$host,$port); $join; $chatmsg; $n = 0; while(true) { if ($n == 0) { include "msgs.php"; if($join) { echo "<u>" . $msgs . "| $nomsg</u><br>"; if($msgs != $chatmsg) { $chatmsg = $msgs; $data = "PRIVMSG #oz :" . $chatmsg . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $chatmsg<br>"; } } } $n ++; if($n > 20) $n = 0; if($nomsg > 50000) { socket_close($socket); } socket_recv($socket, $recieve, 1024, 2); if($recieve == $buffer) { $nomsg ++; continue; } $buffer = $recieve; echo "Recieved: $buffer<br>"; $nomsg = 0; $pingpos = strpos($buffer,"PING :"); $pingcode; if($pingpos !== false) { for($n = $pingpos + 6; $n < strlen($buffer); $n++) { if($buffer[$n] == 13 || $buffer[$n] == 10) break; $pingcode .= $buffer[$n]; } $data = "PONG :" . $pingcode . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; if($join == false) { $data = "JOIN #oz" . chr(13); socket_sendto($socket,$data, strlen($data), 0,$host,$port); echo "Sent: $data<br>"; $join = true; } } } ?> Looks like include will no longer work because of a 30 second execution time. So I'm hoping I can use Javascript to submit future messages. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.