Page 1 of 1

Sockets

Posted: Sun Sep 02, 2007 1:47 pm
by psychotomus
I am writing a online pc game but i want all data stored on my server related to the game such as registering threw site to the pc game. so I connect to the game server when creating an accoutn and make an account.

I get this message. i changed ip adress and port to keep it hidden =)

Warning: socket_connect() [function.socket-connect]: unable to connect [115]: Operation now in progress in /mounted-storage/home60b/sub001/sc18478-RGIJ/thenarutommorpg.com/test.php on line 4
[+] Connected

with this code

how can I stop the warning from coming up?

Code: Select all

<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock,"someIP", somePORT);
socket_set_block($sock);
switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 252))
{
        case 2:
                echo "[-] Connection Refused\n";
                break;
        case 1:
                echo "[+] Connected\n";
                break;
        case 0:
                echo "[-] Timeout\n";
                break;
}
?>

Posted: Sun Sep 02, 2007 2:34 pm
by volka
Why do you set the socket to non-blocking for socket_connect?

Posted: Sun Sep 02, 2007 2:54 pm
by psychotomus
volka wrote:Why do you set the socket to non-blocking for socket_connect?
I have no idea. I got this code snipplet from php.net

Posted: Sun Sep 02, 2007 3:14 pm
by josa
Am I correct in thinking that you got this code from the "User Contributed Notes" section in the PHP manual under the socket_connect() function? The second note from the bottom might answer your question...

/josa

Posted: Sun Sep 02, 2007 3:39 pm
by volka
Do you need a timeout for the connect function or can you assume the server does respond in a reasonable timespan?

Code: Select all

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ( !socket_connect($sock,"someIP", somePORT) ) {
  // error handling here
}

Posted: Sun Sep 02, 2007 4:04 pm
by psychotomus
aight. I got rid of the warning. now I can't get it to send and receive some data.


after I send socket_write. the Server should respond with either RegOk or RegNo. If its RegOk, I want to insert user into db. =) anybody got any ideas?

I think its just stuck in an infinite loop
while (strlen($terminator) <= 5)
{
$nr=socket_recv($sock,$terminator,5,0);
}

Code: Select all

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
			socket_connect($sock,"some ip", somePORT);
			socket_set_block($sock);
			switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 30))
			{
				case 2:
						$template->set_filenames(array('body' => 'register_failed.tpl'));
						break;
				case 1:
						echo 'connected';
						$msg = $_POST['textUserName'] . ':' . $_POST['textPassword'] . ':' . $_POST['textEmailAddress'] . '';
						socket_write($sock, $msg, strlen($msg)); //Send data
						//loop till we get 5 ore more chars
						while (strlen($terminator) <= 5) 
						{
							$nr=socket_recv($sock,$terminator,5,0);
						}
						echo 'Term: ' . $terminator;
						if(strpos($terminator,"RegOk") === true )
						{
							$user = mysql_real_escape_string($_POST['textUserName']);
							$email = mysql_real_escape_string($_POST['textEmailAddress']);
							$pass = mysql_real_escape_string($_POST['textPassword']);
							$new_pass = md5($pass);
							$sql = 'INSERT INTO ' . USERS_TABLE . " (username, user_password, user_email) VALUES ('$user', '$new_pass', '$email')";
							
							if (!($result = $db->sql_query($sql)))
							{
								message_die(GENERAL_ERROR, 'Could not insert user data', '', __LINE__, __FILE__, $sql);
							}
						}
						socket_close($sock);
						$template->set_filenames(array('body' => 'register_complete.tpl'));
						break;
				case 0:
						$template->set_filenames(array('body' => 'register_failed.tpl'));
						break;
			}

Posted: Sun Sep 02, 2007 5:29 pm
by psychotomus
I got it working. Thanks for looking.

Posted: Mon Sep 03, 2007 5:13 am
by shivam0101
can you please post, how you got it working?

Thanks

Posted: Wed Sep 12, 2007 8:11 pm
by psychotomus
sorry for the delay.
I just took out
socket_set_block($sock);

Code: Select all

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
                        socket_connect($sock,"some ip", somePORT); 
                        switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 30)) 
                        { 
                                case 2: 
                                                $template->set_filenames(array('body' => 'register_failed.tpl')); 
                                                break; 
                                case 1: 
                                                echo 'connected'; 
                                                $msg = $_POST['textUserName'] . ':' . $_POST['textPassword'] . ':' . $_POST['textEmailAddress'] . ' '; 
                                                socket_write($sock, $msg, strlen($msg)); //Send data 
                                                //loop till we get 5 ore more chars 
                                                while (strlen($terminator) <= 5) 
                                                { 
                                                        $nr=socket_recv($sock,$terminator,5,0); 
                                                } 
                                                echo 'Term: ' . $terminator; 
                                                if(strpos($terminator,"RegOk") === true ) 
                                                { 
                                                        $user = mysql_real_escape_string($_POST['textUserName']); 
                                                        $email = mysql_real_escape_string($_POST['textEmailAddress']); 
                                                        $pass = mysql_real_escape_string($_POST['textPassword']); 
                                                        $new_pass = md5($pass); 
                                                        $sql = 'INSERT INTO ' . USERS_TABLE . " (username, user_password, user_email) VALUES ('$user', '$new_pass', '$email')"; 
                                                        
                                                        if (!($result = $db->sql_query($sql))) 
                                                        { 
                                                                message_die(GENERAL_ERROR, 'Could not insert user data', '', __LINE__, __FILE__, $sql); 
                                                        } 
                                                } 
                                                socket_close($sock); 
                                                $template->set_filenames(array('body' => 'register_complete.tpl')); 
                                                break; 
                                case 0: 
                                                $template->set_filenames(array('body' => 'register_failed.tpl')); 
                                                break; 
                        }