I've written a socket server that accepts normal commands using socket_read() however, socket read uses a max_length var.

I now have a need to pass entire csv files over the socket connection. How can I set it to read these files in that I don't know how large they are without doing a kludgy solution such as setting a really high max_length?

Is there an option that I can set for socket_read() that will read until it recieves \0 and ignore the max_length?

Thanks,

Jesse

    socket read has the option:

    PHP_NORMAL_READ - reading stops at \n or \r. (Default in PHP <= 4.0.6)

    Which is completely bugged on windows. I wrote myself a complete socket class that works, and might save you an absolute hell of headaches that allows you do to massive reads and terminate with any phrase you like - and defaults to \0 - if you want it I'll shove it on as an attachment.

      Yeah, I read those options on php.net too and it left me wondering well what about an option for /0 lol. It says early on that the terminator can be /n /r OR /0. The OR lead me to believe that there was a way to specify but the rest of the docs didnt have any options for that.

      That sounds exactly what i want to do. If you could post it, or even show me an example how to set it up to make it so /0 is the terminator that would be awesome.

      I cant really use /n or /r as the csv will contain these.

      Thanks so much!

      Jesse

        I'm not sure if this is the latest version, but there's probably enough in there to get you going - have a look at the read_big function

          Thanks so much! I also found a solution on php-tools.de in the patServer. I'll take a look at yours as well. Here's his solution so ya know

          while( $buf = @socket_read( $this->clientFD[$clientId], $this->readBufferSize ) )
          		{
          			$data	.=	$buf;
          
          		$endString	=	substr( $buf, - strlen( $this->readEndCharacter ) );
          		if( $endString == $this->readEndCharacter )
          			break;
          	}
          

          Thanks again

          Jesse

            7 months later

            I have 2 dedicated servers

            Server A has a php script running a socket server on port 9000
            I can login to server a and run >telnet 127.0.0.1 9000
            and access the socket connection perfectly

            Now if I try and login to Server B and do >telnet 1.1.serverAIP.1 9000
            i either get connection refused error or "no route to host"

            I have disabled all firewalls on both servers, so those ports are not being blocked by the firewall.

            The socket server script is very simple.

            $listenfd = socket_create(AF_INET, SOCK_STREAM, 0);
            if ($listenfd)
                print "Listening on port " . PORT . "\n";
            else
                die("AIEE -- socket died!\n");
            
            socket_setopt($listenfd, SOL_SOCKET, SO_REUSEADDR, 1);
            if (!socket_bind($listenfd, "0.0.0.0", PORT))
            {
                socket_close($listenfd);
                die("AIEE -- Couldn't bind!\n");
            }
            socket_listen($listenfd, LISTENQ);
            ///then  a while loop to keep listening.
            

            My question is this: Is there anything special I need to do script-wise to make this socket server accessable outside the localhost?

              Write a Reply...