Re: [PHP] Sockets (reading)
On Wed, Aug 26, 2009 at 5:36 PM, Philip Thompson philthath...@gmail.comwrote: On Aug 26, 2009, at 2:47 PM, Bob McConnell wrote: From: Philip Thompson On Aug 26, 2009, at 11:56 AM, Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. Thank you for your input. Is it guaranteed that at least 1 byte will be sent each time? For example, if I know the data length... ?php $input = ''; for ($i=0; $i$dataLength; $i++) { // Read 1 byte at a time if (($data = @socket_read ($socket, 1, PHP_BINARY_READ)) !== false) { $input .= $data; } } return $input; ? Or is this a completely unreasonable and unnecessary way to get the data? While I have written a lot of code to manage sockets over the years, and coded a UDP/IP stack, I have never done it in PHP. And unfortunately, I don't have time to experiment right now. My boss is waiting for the next product release from me. Getting one byte at a time is somewhat wasteful, as it requires more system calls than necessary. That's a lot of wasted overhead. Whether you always get one or more bytes depends on a number of factors, including whether the calls PHP uses are blocking or non-blocking, plus there may be ways to switch the socket back and forth. Have you tried doing a Google search on the group of PHP functions you expect to use. That should come up with some sample code to look at. Bob McConnell I agree that one byte at a time is wasteful. I'm sure others haven't implemented something along these lines, but I wrote a much more efficient way to make sure I grab all the data of a known length... ?php function readSocketForDataLength ($socket, $len) { $offset = 0; $socketData = ''; while ($offset $len) { if (($data = @socket_read ($socket, $len - $offset, PHP_BINARY_READ)) === false) { return false; } $offset += strlen ($data); $socketData .= $data; } return $socketData; } ? If not all the data is obtained on a read, it will loop until the amount of data is the same as the length requested. This is working quite well. Thanks Bob and the others! ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php socket related: Does this solution work on both, blocking and non-blocking sockets ? And what about different read method? solution related: Does strlen works fine with binary data? Does this snippet work for sending/receiving multibytes strings? -- Martin Scotta
Re: [PHP] Sockets (reading)
Philip Thompson wrote: Hi. During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Thanks, ~Philip Is it possible that you are receiving a \r, \n, or \0 and that is stopping the input? Check the Parameters the second argument for Length options. http://us3.php.net/socket_read Here is a shortened version of a script that I use to do something similar. ?php error_reporting(E_ALL); ini_set('display_errors', 'On'); // Set time limit to indefinite execution set_time_limit(0); // Set the ip and port we will listen on define('LISTEN_IP', 'your IP address'); // IP to listin on define('LISTEN_PORT', port); // Port number define('PACKET_SIZE', 512); // 512 bytes define('SOCKET_TIMOUT', 2);// Seconds /* Open a server socket to port 1234 on localhost */ if ( $socket = @stream_socket_server('udp://'.LISTEN_IP.':'.LISTEN_PORT, $errno, $errstr, STREAM_SERVER_BIND) ) { while ( true ) { $buff = stream_socket_recvfrom($socket, PACKET_SIZE, 0, $remote_ip); if ( !empty($buff) ) { print('Received Data: '.PHP_EOL); print('Do something with it...'.PHP_EOL); } else { print('Empty String'.PHP_EOL); } } fclose($socket); } else { print([{$errno}] {$error}.PHP_EOL); } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sockets (reading)
From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets (reading)
On Aug 26, 2009, at 11:56 AM, Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. Bob McConnell Thank you for your input. Is it guaranteed that at least 1 byte will be sent each time? For example, if I know the data length... ?php $input = ''; for ($i=0; $i$dataLength; $i++) { // Read 1 byte at a time if (($data = @socket_read ($socket, 1, PHP_BINARY_READ)) !== false) { $input .= $data; } } return $input; ? Or is this a completely unreasonable and unnecessary way to get the data? Thanks, ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets (reading)
Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. I'm not sure this has much to do with the OP's problem, but this part is backwards. TCP is connection oriented and tracks segments by sequence number for each connection. This enables the stack to pass the data in order to the higher layers. UDP is connectionless and has no way to determine what datagram was sent before the other one, so it is up to the higher layers to reassemble. As for IP in general, if packets need to be fragmented along the way by a router in order to fit the MTU of a different network, then the IP stack on the receiving end will reassemble the fragments based upon information that the router injects into the fragments. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sockets (reading)
From: Shawn McKenzie Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. I'm not sure this has much to do with the OP's problem, but this part is backwards. TCP is connection oriented and tracks segments by sequence number for each connection. This enables the stack to pass the data in order to the higher layers. UDP is connectionless and has no way to determine what datagram was sent before the other one, so it is up to the higher layers to reassemble. As for IP in general, if packets need to be fragmented along the way by a router in order to fit the MTU of a different network, then the IP stack on the receiving end will reassemble the fragments based upon information that the router injects into the fragments. Shawn, You're looking at it inside out. Yes, the individual packets are tracked by the stack, to make sure they arrive in the correct order. But the size and fragmentation of those packets have no relationship at all to any data structure the application layer may imply. They simply implement a communications stream to reliably move octets from one point to another. If the application needs structure, it has to manage that for itself. For UDP, if you write a 32 byte packet, the matching read will get a 32 byte packet, if it arrived at the receiving stack. Missed data detection and retry requests are left up to the application. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sockets (reading)
From: Philip Thompson On Aug 26, 2009, at 11:56 AM, Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. Thank you for your input. Is it guaranteed that at least 1 byte will be sent each time? For example, if I know the data length... ?php $input = ''; for ($i=0; $i$dataLength; $i++) { // Read 1 byte at a time if (($data = @socket_read ($socket, 1, PHP_BINARY_READ)) !== false) { $input .= $data; } } return $input; ? Or is this a completely unreasonable and unnecessary way to get the data? While I have written a lot of code to manage sockets over the years, and coded a UDP/IP stack, I have never done it in PHP. And unfortunately, I don't have time to experiment right now. My boss is waiting for the next product release from me. Getting one byte at a time is somewhat wasteful, as it requires more system calls than necessary. That's a lot of wasted overhead. Whether you always get one or more bytes depends on a number of factors, including whether the calls PHP uses are blocking or non-blocking, plus there may be ways to switch the socket back and forth. Have you tried doing a Google search on the group of PHP functions you expect to use. That should come up with some sample code to look at. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets (reading)
On Aug 26, 2009, at 2:47 PM, Bob McConnell wrote: From: Philip Thompson On Aug 26, 2009, at 11:56 AM, Bob McConnell wrote: From: Philip Thompson During a socket read, why would all the requested number of bytes not get sent? For example, I request 1000 bytes: ?php $data = @socket_read ($socket, 2048, PHP_BINARY_READ); ? This is actually in a loop, so I can get all the data if split up. So, for example, here's how the data split up in 3 iterations (for 1000 bytes): 650 bytes 200 bytes 150 bytes But if I can accept up to 2048 bytes per socket read, why would it not pull all 1000 bytes initially in 1 step? Any thoughts on this would be greatly appreciated! Because that's the way TCP/IP works, by design. TCP is a stream protocol. It guarantees all of the bytes written to one end of the pipe will come out the other end in the same order, but not necessarily in the same groupings. There are a number of buffers along the way that might split them up, as well as limits on packet sizes in the various networks it passed through. So you get what is available in the last buffer when a timer expires, no more, and no less. If you have serialized data that needs to be grouped in specific blocks, your application will need to keep track of those blocks, reassembling or splitting the streamed data as necessary. You could use UDP which does guarantee that packets will be kept together, but that protocol doesn't guarantee delivery. Thank you for your input. Is it guaranteed that at least 1 byte will be sent each time? For example, if I know the data length... ?php $input = ''; for ($i=0; $i$dataLength; $i++) { // Read 1 byte at a time if (($data = @socket_read ($socket, 1, PHP_BINARY_READ)) !== false) { $input .= $data; } } return $input; ? Or is this a completely unreasonable and unnecessary way to get the data? While I have written a lot of code to manage sockets over the years, and coded a UDP/IP stack, I have never done it in PHP. And unfortunately, I don't have time to experiment right now. My boss is waiting for the next product release from me. Getting one byte at a time is somewhat wasteful, as it requires more system calls than necessary. That's a lot of wasted overhead. Whether you always get one or more bytes depends on a number of factors, including whether the calls PHP uses are blocking or non-blocking, plus there may be ways to switch the socket back and forth. Have you tried doing a Google search on the group of PHP functions you expect to use. That should come up with some sample code to look at. Bob McConnell I agree that one byte at a time is wasteful. I'm sure others haven't implemented something along these lines, but I wrote a much more efficient way to make sure I grab all the data of a known length... ?php function readSocketForDataLength ($socket, $len) { $offset = 0; $socketData = ''; while ($offset $len) { if (($data = @socket_read ($socket, $len - $offset, PHP_BINARY_READ)) === false) { return false; } $offset += strlen ($data); $socketData .= $data; } return $socketData; } ? If not all the data is obtained on a read, it will loop until the amount of data is the same as the length requested. This is working quite well. Thanks Bob and the others! ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: php sockets
well i mean even if we would not consider that particular piece of code as an example of the code that i have issues with im still rather interesting if theres some different between the socket model used by say, c++(winsock in my case) and the sockets used in php because when made a simple c++ script (winsock based) which just echoes what its gotten from a client i still get a problem which looks like this: when php client connects to the serv, the server then gets into an endnless loop loading the cpu almost up to max . Although i get a message from it in the php script (which server was supposed to send when the client connects) but the server itself doesnt work correctly for some reason, and thats what im curious about. Again, when i rewrote the whole functionality of the client in c++ it worked just as it was supposed to, while being written in php(client part) it all messes up. Nathan Nobbe [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] figured id top-post on this one, since the original message was so long.. i recommend debugging with a tool like wireshark. that way you can see whats in the packets going over the wire and hopefully it will lead to a solution. -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: php sockets
vixle wrote: well i mean even if we would not consider that particular piece of code as an example of the code that i have issues with im still rather interesting if theres some different between the socket model used by say, c++(winsock in my case) and the sockets used in php because when made a simple c++ script (winsock based) which just echoes what its gotten from a client i still get a problem which looks like this: when php client connects to the serv, the server then gets into an endnless loop loading the cpu almost up to max . Although i get a message from it in the php script (which server was supposed to send when the client connects) but the server itself doesnt work correctly for some reason, and thats what im curious about. Again, when i rewrote the whole functionality of the client in c++ it worked just as it was supposed to, while being written in php(client part) it all messes up. Nathan Nobbe [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] figured id top-post on this one, since the original message was so long.. i recommend debugging with a tool like wireshark. that way you can see whats in the packets going over the wire and hopefully it will lead to a solution. -nathan well, since it is the php version, and this is a php list, why don't you show us your complete PHP source code instead of you C++ source. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: php sockets
With any code doing a basic socket functionality, the code that i gave in the original post is suppossed to connect to a deamon, and get a message from it , instead it makes the deamon go crazy in the sense that it starts endless looping and loads the system resources up to max. Jim Lucas [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] well, since it is the php version, and this is a php list, why don't you show us your complete PHP source code instead of you C++ source. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: php sockets
figured id top-post on this one, since the original message was so long.. i recommend debugging with a tool like wireshark. that way you can see whats in the packets going over the wire and hopefully it will lead to a solution. -nathan On Dec 19, 2007 12:54 AM, vixle [EMAIL PROTECTED] wrote: this code doesn't interact with php client while with c++ based one it works just fine. .anybody? #include stdio.h #include winsock2.h #include iostream #include process.h using namespace std; int i = 0; int ar = 0; const int is = 50; SOCKET stack[is]; void clientserve(void* ws) { SOCKET wsocket = *(SOCKET*)ws; int fgotused = 0; char sendbuf[70]; char recvbuf[70]; int scnt = 0; ar++; int id = ar; while(scnt = ar) { if(stack[scnt] == 0) { stack[scnt] = wsocket; id = scnt; fgotused = 1; scnt = 0; break; } scnt++; } if(fgotused == 0) stack[id] = wsocket; send(stack[id], Server message: You are now successfuly connected., 70, 0 ); while(1) { scnt = 0; if(recv(wsocket, recvbuf, 70, 0 ) == SOCKET_ERROR) { if(WSAGetLastError() == WSAECONNRESET) { i--; stack[id] = 0; cout Client Disconnected. endl; cout Clients connected: i endl; closesocket(wsocket); return; } } if(recvbuf) { cout recvbuf endl; while(scnt = ar) { if(scnt != id) send(stack[scnt], recvbuf, 70, 0); scnt++; } recvbuf = null; } } } void main() { WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2),wsaData); if (iResult != NO_ERROR) printf(Error at WSAStartup()\n); SOCKET m_socket; m_socket = socket (AF_INET, SOCK_STREAM, 0); if (m_socket == INVALID_SOCKET) { printf(Error at socket(): %ld\n, WSAGetLastError()); WSACleanup(); return; } sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(127.0.0.1); service.sin_port = htons(27015); if (bind(m_socket,(SOCKADDR*)service, sizeof(service)) == SOCKET_ERROR) { printf(bind() failed.\n); closesocket(m_socket); return; } if (listen(m_socket, 700) == SOCKET_ERROR) printf( Error listening on socket.\n); SOCKET AcceptSocket; printf(Waiting for a client to connect...\n); while (AcceptSocket = accept(m_socket, NULL, NULL)) { i++; cout Client Connected. endl; cout Clients connected: i endl; _beginthread(clientserve, 0, (void*)AcceptSocket); } } vixle [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ?php /* Get the port for the WWW service. */ //$service_port = getservbyname('www', 'tcp'); /* Get the IP address for the target host. */ //$address = gethostbyname('www.example.com'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //echo Attempting to connect to '$address' on port '$service_port'...; $result = socket_connect($socket, 127.0.0.1, 27015); socket_RECV($socket, $read, 300, null); echo $read; socket_close($socket); ? i have a daemon running on that port that sends a message when it's got a client connected but the script above doesn't output anything it just loads my cpu up to 100 percent and thats it then it basically stops working. While i need it to display the messages sent by server(daemon) to the user running the script has anyone got any idea why it rejects to work? (yeah the daemon is written in c++ if that matters) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: php sockets
this code doesn't interact with with php client while with c++ based one it works just fine. .anybody? #include stdio.h #include winsock2.h #include iostream #include process.h using namespace std; int i = 0; int ar = 0; const int is = 50; SOCKET stack[is]; void clientserve(void* ws) { SOCKET wsocket = *(SOCKET*)ws; int fgotused = 0; char sendbuf[70]; char recvbuf[70]; int scnt = 0; ar++; int id = ar; while(scnt = ar) { if(stack[scnt] == 0) { stack[scnt] = wsocket; id = scnt; fgotused = 1; scnt = 0; break; } scnt++; } if(fgotused == 0) stack[id] = wsocket; send(stack[id], Server message: You are now successfuly connected., 70, 0 ); while(1) { scnt = 0; if(recv(wsocket, recvbuf, 70, 0 ) == SOCKET_ERROR) { if(WSAGetLastError() == WSAECONNRESET) { i--; stack[id] = 0; cout Client Disconnected. endl; cout Clients connected: i endl; closesocket(wsocket); return; } } if(recvbuf) { cout recvbuf endl; while(scnt = ar) { if(scnt != id) send(stack[scnt], recvbuf, 70, 0); scnt++; } recvbuf = null; } } } void main() { WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2),wsaData); if (iResult != NO_ERROR) printf(Error at WSAStartup()\n); SOCKET m_socket; m_socket = socket (AF_INET, SOCK_STREAM, 0); if (m_socket == INVALID_SOCKET) { printf(Error at socket(): %ld\n, WSAGetLastError()); WSACleanup(); return; } sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(127.0.0.1); service.sin_port = htons(27015); if (bind(m_socket,(SOCKADDR*)service, sizeof(service)) == SOCKET_ERROR) { printf(bind() failed.\n); closesocket(m_socket); return; } if (listen(m_socket, 700) == SOCKET_ERROR) printf( Error listening on socket.\n); SOCKET AcceptSocket; printf(Waiting for a client to connect...\n); while (AcceptSocket = accept(m_socket, NULL, NULL)) { i++; cout Client Connected. endl; cout Clients connected: i endl; _beginthread(clientserve, 0, (void*)AcceptSocket); } } vixle [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ?php /* Get the port for the WWW service. */ //$service_port = getservbyname('www', 'tcp'); /* Get the IP address for the target host. */ //$address = gethostbyname('www.example.com'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //echo Attempting to connect to '$address' on port '$service_port'...; $result = socket_connect($socket, 127.0.0.1, 27015); socket_RECV($socket, $read, 300, null); echo $read; socket_close($socket); ? i have a daemon running on that port that sends a message when it's got a client connected but the script above doesn't output anything it just loads my cpu up to 100 percent and thats it then it basically stops working. While i need it to display the messages sent by server(daemon) to the user running the script has anyone got any idea why it rejects to work? (yeah the daemon is written in c++ if that matters) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: php sockets
this code doesn't interact with php client while with c++ based one it works just fine. .anybody? #include stdio.h #include winsock2.h #include iostream #include process.h using namespace std; int i = 0; int ar = 0; const int is = 50; SOCKET stack[is]; void clientserve(void* ws) { SOCKET wsocket = *(SOCKET*)ws; int fgotused = 0; char sendbuf[70]; char recvbuf[70]; int scnt = 0; ar++; int id = ar; while(scnt = ar) { if(stack[scnt] == 0) { stack[scnt] = wsocket; id = scnt; fgotused = 1; scnt = 0; break; } scnt++; } if(fgotused == 0) stack[id] = wsocket; send(stack[id], Server message: You are now successfuly connected., 70, 0 ); while(1) { scnt = 0; if(recv(wsocket, recvbuf, 70, 0 ) == SOCKET_ERROR) { if(WSAGetLastError() == WSAECONNRESET) { i--; stack[id] = 0; cout Client Disconnected. endl; cout Clients connected: i endl; closesocket(wsocket); return; } } if(recvbuf) { cout recvbuf endl; while(scnt = ar) { if(scnt != id) send(stack[scnt], recvbuf, 70, 0); scnt++; } recvbuf = null; } } } void main() { WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2),wsaData); if (iResult != NO_ERROR) printf(Error at WSAStartup()\n); SOCKET m_socket; m_socket = socket (AF_INET, SOCK_STREAM, 0); if (m_socket == INVALID_SOCKET) { printf(Error at socket(): %ld\n, WSAGetLastError()); WSACleanup(); return; } sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(127.0.0.1); service.sin_port = htons(27015); if (bind(m_socket,(SOCKADDR*)service, sizeof(service)) == SOCKET_ERROR) { printf(bind() failed.\n); closesocket(m_socket); return; } if (listen(m_socket, 700) == SOCKET_ERROR) printf( Error listening on socket.\n); SOCKET AcceptSocket; printf(Waiting for a client to connect...\n); while (AcceptSocket = accept(m_socket, NULL, NULL)) { i++; cout Client Connected. endl; cout Clients connected: i endl; _beginthread(clientserve, 0, (void*)AcceptSocket); } } vixle [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ?php /* Get the port for the WWW service. */ //$service_port = getservbyname('www', 'tcp'); /* Get the IP address for the target host. */ //$address = gethostbyname('www.example.com'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //echo Attempting to connect to '$address' on port '$service_port'...; $result = socket_connect($socket, 127.0.0.1, 27015); socket_RECV($socket, $read, 300, null); echo $read; socket_close($socket); ? i have a daemon running on that port that sends a message when it's got a client connected but the script above doesn't output anything it just loads my cpu up to 100 percent and thats it then it basically stops working. While i need it to display the messages sent by server(daemon) to the user running the script has anyone got any idea why it rejects to work? (yeah the daemon is written in c++ if that matters) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets as a module or a separate PHP CLI instance?
Am Mittwoch, 2. Mai 2007 13:29 schrieb [EMAIL PROTECTED]: I need to do some socket work on a production machine that is constantly busy so I don't dare re-compile php. Anybody know if it's possible to load the socket functions dynamically, maybe as if they were in a module? It's possible if it's possible to compile it as shared object. It is, as far as I know. --with-socket=shared There might be some obstacles with threaded servers. I am not sure. Alternatively, would it be possible to compile PHP without apache and with sockets for command line use only? --enable-cli Regards, Oliver -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets as a module or a separate PHP CLI instance?
Am Mittwoch, 2. Mai 2007 14:36 schrieb Oliver Block: --with-socket=shared Actually it should be --enable-sockets=shared Regards, Oliver -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets as a module or a separate PHP CLI instance?
On Wed, May 2, 2007 6:29 am, [EMAIL PROTECTED] wrote: I need to do some socket work on a production machine that is constantly busy so I don't dare re-compile php. Anybody know if it's possible to load the socket functions dynamically, maybe as if they were in a module? If you can do --with-sockets=shared, for the configure, you should end up with a sockets.so file that you can then use http://php.net/dl to load into your scripts that need sockets... At least, *some* extensions this works... Alternatively, would it be possible to compile PHP without apache and with sockets for command line use only? Definitely. Just compile it as CLI or CGI, without the --with-apxs or --with-apxs2 or whatever pulls in Apache. If I do that I would like to give the resulting object a different name than PHP because I do have some cmd line pgms running on the machine. Is that difficult? Then don't do 'make install' but just copy the bin/php over to /usr/local/bin/php-with-sockets Then you can do php-with-sockets -q whatever.php to run that one special PHP binary. Alternatively, you can download the PHP source to a whole new fresh directory, compile it for CLI, and then leave it there, and use: /path/to/php/src/php/bin/php -q whatever.php so that you are running the special php binary from that source directory instead of the usual one in /usr/local/bin (or wherever yours lives) I did that for a CVS version when I needed a patched PHP on a shared host, and it worked pretty well for a cron job. It would be kind of cumbersome for something you wanted to run by hand from the shell a lot, but for a set it and forget it cron job, it was fine for me. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets problem
Thanks for the answer. In the meantime I've managed to solve the problem be removing the pcntl_wait call. Actually I think this is a bug, because as I understand things pcntl_wait shouldn't block the main process, but I don't have experience with either sockets or Unix process, so I might be wrong. On 3/21/07, Richard Lynch [EMAIL PROTECTED] wrote: On Tue, March 20, 2007 8:27 am, Adrian Gheorghe wrote: I've sent a bug report earlier and it got marked as bogus, so I decided to ask here about a possible solution. You can see the bug report at http://bugs.php.net/bug.php?id=40864 Looks like a pretty cogent bug report to me... Perhaps Tony would like to see the same script in non-fork single-connection mode on the same server, to prove that the ports/firewalls/etc are not at fault. Or, perhaps, there's something inherently wrong with that script... I'd also suggest taking out the 'fork' business and just opening up two sockets in two scripts, to remove the pcntl (possible) red herring -- boil it down to absolute minimum. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets problem
On Tue, March 20, 2007 8:27 am, Adrian Gheorghe wrote: I've sent a bug report earlier and it got marked as bogus, so I decided to ask here about a possible solution. You can see the bug report at http://bugs.php.net/bug.php?id=40864 Looks like a pretty cogent bug report to me... Perhaps Tony would like to see the same script in non-fork single-connection mode on the same server, to prove that the ports/firewalls/etc are not at fault. Or, perhaps, there's something inherently wrong with that script... I'd also suggest taking out the 'fork' business and just opening up two sockets in two scripts, to remove the pcntl (possible) red herring -- boil it down to absolute minimum. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
On 7/12/05, daro [EMAIL PROTECTED] wrote: I don't know where should I put this script to be able to receive all datas and respond with proper strings. You would run the script as a shell script from the command line. On windows you can just save the code as a regular php script then start it up from a DOS prompt like: c:\PHP\php.exe -f yoursocketfile.php or if you are using a *nix setup run it just as it is, making sure the first line points to your php binary, for example mine is /usr/bin/php. Chmod it so it's executable. -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
On Tue, 2005-07-12 at 16:49 +0200, daro wrote: Hi. I'm writing a TCP/IP server and client. On your website I found ready php script http://pl2.php.net/sockets but I have a question. For TCP/IP server the example script from your website has to be put on server as index.php file and the access to it could be f.e. http://192.168.1.11/index.php ? I don't know where should I put this script to be able to receive all datas and respond with proper strings. Your sincerely Dariusz Chorążewicz - Rozwiązania sms www.statsms.net, www.smscenter.pl GG: 346444 Tel: 696 061 543 You have to run your script from the command-line. Don't depend on apache to have it running. Also remember to set_time_limit(0) ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
You should use/run it via PHP-CLI. It is a shell script. Ex: /usr/local/bin/php socket.php (on linux) or C:\PHP\php.exe socket.php (on windooz/M$-dos) Good luck. Hidayet Dogan On Tue, 12 Jul 2005, daro wrote: Hi. I'm writing a TCP/IP server and client. On your website I found ready php script http://pl2.php.net/sockets but I have a question. For TCP/IP server the example script from your website has to be put on server as index.php file and the access to it could be f.e. http://192.168.1.11/index.php ? I don't know where should I put this script to be able to receive all datas and respond with proper strings. Your sincerely Dariusz Chor??ewicz - Rozwi?zania sms www.statsms.net, www.smscenter.pl GG: 346444 Tel: 696 061 543 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
On Tue, 2005-07-12 at 09:58 -0500, Greg Donald wrote: On 7/12/05, daro [EMAIL PROTECTED] wrote: I don't know where should I put this script to be able to receive all datas and respond with proper strings. You would run the script as a shell script from the command line. On windows you can just save the code as a regular php script then start it up from a DOS prompt like: c:\PHP\php.exe -f yoursocketfile.php or if you are using a *nix setup run it just as it is, making sure the first line points to your php binary, for example mine is /usr/bin/php. Chmod it so it's executable. -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/ More specificlly, --8- #!/usr/bin/php ?php // your script here ? --8- And then $ chmod +x socket.php $ ./socket.php OR $ /usr/bin/php -q socket.php If your on *NIX, remember the last to put the process on the background. Not sure how one would go on windows tho :( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
Please, PLEASE Reply to All! Yes, you have to add something like --8- while(true) { // code here if( $someConditionThatWillMakeMeExit ) { break; } sleep(1); // to prevent excessive processor usage } --8- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
hi all, On Tue, 2005-07-12 at 16:49 +0200, daro wrote: Also remember to set_time_limit(0) ;) PHP ENTER CONFUSION The *CLI* version of php has no max execution time by default (0) - Where's the php CLI version in php4? [PHP_HOME]/cli/php.exe and it reads a php.ini if it was in the SAME directory or as specified using -c [DIRECTORY] (but apparently ignoring the max_execution_time value even it was specified in php.ini) - Oh and what's the $PHP_HOME/php.exe in a typical php4 distribution? it's the CGI executable (according to the accompanying READM) - And what's the CLI one in php5? [PHP_HOME]/php.exe and it reads php-cli.ini!! (note that this was the CGI one in php4) - Oh la la and where's CGI one in php5? [PHP_HOME]/php-cgi.exe Can i ini_set() the value of max_execution_time during runtime when using the cli versions? YES!! I've done some personal experimentation to confirm all the above. Tested with PHP/4.3.9 and PHP/5.0.2 -ahmed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
Since there was no reference to that on the PHP manual, I thought about mentioning it just to be safe. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
Hi André, On 7/12/05, André Medeiros [EMAIL PROTECTED] wrote: Since there was no reference to that on the PHP manual, I thought about mentioning it just to be safe. yeah the manual is completely drak when it comes to php CLI binary -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
On 7/12/05, Ahmed Saad [EMAIL PROTECTED] wrote: yeah the manual is completely drak when it comes to php CLI binary `php -h` tells you all the command line options.. and all the basic fuctionality is covered in the manual online. Seems complete to me. -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
On 7/13/05, Greg Donald [EMAIL PROTECTED] wrote: `php -h` tells you all the command line options.. and all the basic fuctionality is covered in the manual online. Seems complete to me. ehmm you weren't refering to the CLI version but anyways, I'd be grateful to anyone who points me to more information about other differences between the CGI and CLI versions (besides max_exection_times and configuration files). thanks -ahmed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
Hi, I think curt is right about transfer encoding being a problem, however i feel it may not be 'the' problem. This timing issue looks like you are running into a 'blocking' kind of situation. Cosmin, Have you tried the 'Connection: close' header? Getting back to transfer encoding you might want to look at the RFC - http://www.w3.org/Protocols/rfc2616/rfc2616.html where they explain how it's to be handled. all the best Curt Zirzow wrote: * Thus wrote Cosmin ([EMAIL PROTECTED]): On Sun, 2003-10-26 at 15:30, Raditha Dissanayake wrote: are you getting any 1xx status codes from the web server? here are the full headers: Transfer-Encoding: chunked This is probably the problem. If you inspect your data, you'll notice that it has extra characters in it right now. You can change your protocol version to HTTP/1.0 or send a header to tell the server you don't want chuncked (don't know that off hand). Curt -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha/megaupload/ Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
* Thus wrote Cosmin ([EMAIL PROTECTED]): On Sun, 2003-10-26 at 15:30, Raditha Dissanayake wrote: are you getting any 1xx status codes from the web server? here are the full headers: Transfer-Encoding: chunked This is probably the problem. If you inspect your data, you'll notice that it has extra characters in it right now. You can change your protocol version to HTTP/1.0 or send a header to tell the server you don't want chuncked (don't know that off hand). Curt -- My PHP key is worn out PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
are you getting any 1xx status codes from the web server? Cosmin wrote: I'm trying to make an application using XML-RPC, and I have the following problem: I use fsockopen() to simulate a POST to my local web-server. All goes very well except it's very very slow. Here is my code maybe someone could tell me what I'm doing wrong: = $url= parse_url($this-serverURL); $requestString= POST .$url['path']. HTTP/1.1\r\nHost: .$url['host'].\r\nContent-type: application/x-www.form-urlencoded\r\nContent-length: .strlen($this-requestData).\r\n\r\n.$this-requestData;; $fp = fsockopen($url['host'], 80, $err_num, $err_msg, 5); if ($fp) { //make the request to the xml-rpc server fputs($fp, $requestString); //gets the result while (!feof($fp)) { $response .= fgets($fp, 1024); } fclose($fp); $this-rawResponse=$response; $this-error=false; } else { $this-error=true; $this-errorMessage=$err_msg; } This is the slowest part of my script(about 16 seconds). The server's execution time is only 0.00064206123352051 seconds. I don't know why it takes so much to write a string to the socket and then to read the response. Here are the execution times: Server StartServer Stop 1067090777.5339 1067090777.5346 Client StartClient Stop 1067090777.5303 1067090794.5286 If someone knows a way on how to speed this up please tell me how to do it. Thank you for your time -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha/megaupload/ Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
On Sun, 2003-10-26 at 15:30, Raditha Dissanayake wrote: are you getting any 1xx status codes from the web server? here are the full headers: HTTP/1.1 200 OK Date: Mon, 27 Oct 2003 06:15:30 GMT Server: Apache/1.3.27 (Unix) PHP/4.3.3 X-Powered-By: PHP/4.3.3 Transfer-Encoding: chunked Content-Type: text/xml Cosmin wrote: I'm trying to make an application using XML-RPC, and I have the following problem: I use fsockopen() to simulate a POST to my local web-server. All goes very well except it's very very slow. Here is my code maybe someone could tell me what I'm doing wrong: = $url= parse_url($this-serverURL); $requestString= POST .$url['path']. HTTP/1.1\r\nHost: .$url['host'].\r\nContent-type: application/x-www.form-urlencoded\r\nContent-length: .strlen($this-requestData).\r\n\r\n.$this-requestData;; $fp = fsockopen($url['host'], 80, $err_num, $err_msg, 5); if ($fp) { //make the request to the xml-rpc server fputs($fp, $requestString); //gets the result while (!feof($fp)) { $response .= fgets($fp, 1024); } fclose($fp); $this-rawResponse=$response; $this-error=false; } else { $this-error=true; $this-errorMessage=$err_msg; } This is the slowest part of my script(about 16 seconds). The server's execution time is only 0.00064206123352051 seconds. I don't know why it takes so much to write a string to the socket and then to read the response. Here are the execution times: Server StartServer Stop 1067090777.5339 1067090777.5346 Client StartClient Stop 1067090777.5303 1067090794.5286 If someone knows a way on how to speed this up please tell me how to do it. Thank you for your time -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha/megaupload/ Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
* Thus wrote Cosmin ([EMAIL PROTECTED]): I'm trying to make an application using XML-RPC, and I have the following problem: I use fsockopen() to simulate a POST to my local web-server. All goes very well except it's very very slow. Here is my code maybe someone could tell me what I'm doing wrong: = $url= parse_url($this-serverURL); $requestString= POST .$url['path']. HTTP/1.1\r\nHost: .$url['host'].\r\nContent-type: application/x-www.form-urlencoded\r\nContent-length: .strlen($this-requestData).\r\n\r\n.$this-requestData;; Have you tried to simulate this on a web browser? My guess is there might be some dns issue somewhere causing the webserver to take a while before even processing the request. Try and find out where the bottleneck is by echo'ing between steps to see where the problem is, for example: echo time(), \n; $fp = fsockopen($url['host'], 80, $err_num, $err_msg, 5); echo time(), \n; Curt -- My PHP key is worn out PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets - fine tunning
On Sat, 2003-10-25 at 17:42, Curt Zirzow wrote: * Thus wrote Cosmin ([EMAIL PROTECTED]): I'm trying to make an application using XML-RPC, and I have the following problem: I use fsockopen() to simulate a POST to my local web-server. All goes very well except it's very very slow. Here is my code maybe someone could tell me what I'm doing wrong: = $url= parse_url($this-serverURL); $requestString= POST .$url['path']. HTTP/1.1\r\nHost: .$url['host'].\r\nContent-type: application/x-www.form-urlencoded\r\nContent-length: .strlen($this-requestData).\r\n\r\n.$this-requestData;; Have you tried to simulate this on a web browser? My guess is there might be some dns issue somewhere causing the webserver to take a while before even processing the request. Try and find out where the bottleneck is by echo'ing between steps to see where the problem is, for example: echo time(), \n; $fp = fsockopen($url['host'], 80, $err_num, $err_msg, 5); echo time(), \n; Curt -- My PHP key is worn out PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ Here are the times: 1:1067092779.6042 2:1067092795.7176 And here is the code: echo '1:'.getmicrotime(),\n\n; while($data=fread($fp, 32768)) { $response.=$data; } echo '2:'.getmicrotime(),\n\n; I don't know what else to do ... I've changed the buffer length but it's still the same :(( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: php sockets was Re: [PHP] socket_write eats data - solved
Probably your problems about i can send about seven messages per second may be relationed to OS's tcp connection stream buffering... try to flush every fd after write to it. I wrote a multi-threaded (pcntl_fork()) application in phpcli using many sockets and they worked well... array iterations are fast and easy too, as they have only one level... so I believe that this is really relationed to socket flushing stuffs...you can also use select() to determine when a fd is ready for write and implement a write spool... only a ideia, but probably will spent more time with the same results... well.. flush then may resolve it. Daniel Souza Raditha Dissanayake [EMAIL PROTECTED] escreveu na mensagem news:[EMAIL PROTECTED] Hi thomas, Thomas Weber wrote: IMAP? We were talking about IRC, Used imap as an example. the Internet Relay Chat. In detail, my problems doesn't even refer to IRC directly, as i am developing a server for a html-based webchat, but the server-structure and the messages are nearly the same. Yes my questions was how are you going to maintain the connection between two different connectsion. As far as i know sockets cannot be serialized in php4. Once you realize the basics of socket-multicasting, it is no problem to maintain hundreds of simultanous TCP-connects via arrays of sockets, also called descriptor-sets. PHP seems to directly use the underlying C- libraries, so everything you can imagine is possible. Thanx i am aware of it -- http://www.radinks.com/upload Drag and Drop File Uploader. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
I'm not sure if it would be helpful, but I wrote a quick PHP socket application that implements a specialized HTTP proxy. It's a quick hack of sorts, but I have found it to be very stable, and it uses the latest sockets API. It consists of only one small PHP script, and it's fairly well commented. You're welcome to check it out, and maybe it can answer some of your questions by example. http://protoscope.org/ Chris --- Gareth Thomas [EMAIL PROTECTED] wrote: I realise that sockets is still 'experimental' but any help will be much appreciated. I am developing a queue system to send commands across from a server to a client using sockets. Problem is that the I keep getting a 'connection reset by peer' error after the first command is sent. The server script itself is in a loop which I believe should maintain the socket connection. I am using socket_write() to send the data across the socket, and I am wondering if I should be using socket_send()? Does anyone have any ideas or experience of doing this? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
This is how sockets work, if you close the process holding the socket, the conection is closed. There is no function reopen_the_old_conection. You are not clear about what you are trying to achive, but maybe you should rethink your design. Gareth Thomas wrote: Hi, I am running 4.3.0pre2 on RH 7.2 and on Windows2k I am trying to implement a socket based client/server communication program with the server being on the Linux side and the current test client on windows (although it will be on Linux eventually). A series of commands is sent by the client side which consists of 2 PHP pages, the first with some buttons and the second that sends the button commands to the server using sockets. The problem I am finding is that it appears PHP closes the socket connection once the second script has ended and it returns back to the first. Is there anyway of keeping the socket 'open' or can someone suggest a better way of doing this... Thanks in advance -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets
Yep, if you're using UNIX and compiling from source, you must add this switch to the command line when you're configuring and compiling PHP: --enable-sockets (e.g.: ./configure --enable-sockets). If you're using a prepackaged version (RPM or Windows) then you should look into the documentation for your version to see if this extension was included when the package was compiled from the source. Cheers, Marco On Mon, 2002-10-07 at 14:22, Asmodean wrote: Hey everyone, Does anybody know what the current support / functionality for PHP with sockets is? According to the documentation, all the socket_ functions should be included in PHP = 4.1.0. I'm currently running 4.2.1 and PHP doesn't seem to recognize these functions (socket_send, socket_write, etc). Anybody know if there's anything special I have to do to get it working? // Asmodean -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets 'requested address is not valid in its context'
On Sat, 6 Jul 2002, Zac Hillier wrote: I'm opening a port on a remote machine presently I'm using fsockopen() which is fine for opening the port and sending data however I'm having trouble reading data back from the port. So I've considered using socket functions but do not appear to be able to get a connection. When I run the code below pointing to 127.0.0.1 everything runs fine however when I point to 192.168.123.193 (Another machine on the local network without a server running) I get these errors. You can't bind to a socket on another machine. You have to bind the socket to a local address (i.e., on your machine) and then either listen for incoming connections or initiate an outbound connection from that socket. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets and flush()
dietrich wrote: i have a script that makes a socket connection about halfway through the page. nothing on the page prints until the socket operation is finished, even if i call flush() prior to the socket operation. does anyone now of a way to force PHP to output the buffer prior to executing the socket code? thanks! dietrich [EMAIL PROTECTED] dietrich.ganx4.com Are you using Netscape 4.x and possibly making the socket connection while inside a main table tag? Nescape 4.x series doesn't usually draw a table until the close tag is found or the page ends. Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sockets and flush()
no tables. my test script prints a single string prior to the sockets code. thx, dietrich -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 11:12 AM To: dietrich Cc: [EMAIL PROTECTED] Subject: Re: [PHP] sockets and flush() dietrich wrote: i have a script that makes a socket connection about halfway through the page. nothing on the page prints until the socket operation is finished, even if i call flush() prior to the socket operation. does anyone now of a way to force PHP to output the buffer prior to executing the socket code? thanks! dietrich [EMAIL PROTECTED] dietrich.ganx4.com Are you using Netscape 4.x and possibly making the socket connection while inside a main table tag? Nescape 4.x series doesn't usually draw a table until the close tag is found or the page ends. Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sockets and flush()
Are you using ob_start () and ob_end_flush() ? If not then declaring ob_start () as the very first ? ob_start (ob_gzhandler); function call and ob_end_flush() where you want the string to output should do the trick. Matthew Luchak Webmaster Kaydara Inc. [EMAIL PROTECTED] -Original Message- From: Dietrich Ayala [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 5:39 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: RE: [PHP] sockets and flush() no tables. my test script prints a single string prior to the sockets code. thx, dietrich -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 11:12 AM To: dietrich Cc: [EMAIL PROTECTED] Subject: Re: [PHP] sockets and flush() dietrich wrote: i have a script that makes a socket connection about halfway through the page. nothing on the page prints until the socket operation is finished, even if i call flush() prior to the socket operation. does anyone now of a way to force PHP to output the buffer prior to executing the socket code? thanks! dietrich [EMAIL PROTECTED] dietrich.ganx4.com Are you using Netscape 4.x and possibly making the socket connection while inside a main table tag? Nescape 4.x series doesn't usually draw a table until the close tag is found or the page ends. Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets and flush()
Hmmm I'm no expert on web IO between Apache and PHP, but my guess is that Apache is holding the data and waiting for more. To verify this I recommend that you try running from a shell cgi and see if your string is flushed to stdio as it should. I remember running into a similar problem one time while performing a remote file(). Cheers, Rob. Dietrich Ayala wrote: no tables. my test script prints a single string prior to the sockets code. thx, dietrich -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 09, 2002 11:12 AM To: dietrich Cc: [EMAIL PROTECTED] Subject: Re: [PHP] sockets and flush() dietrich wrote: i have a script that makes a socket connection about halfway through the page. nothing on the page prints until the socket operation is finished, even if i call flush() prior to the socket operation. does anyone now of a way to force PHP to output the buffer prior to executing the socket code? thanks! dietrich [EMAIL PROTECTED] dietrich.ganx4.com Are you using Netscape 4.x and possibly making the socket connection while inside a main table tag? Nescape 4.x series doesn't usually draw a table until the close tag is found or the page ends. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Sockets with windows / win32 - 'call to undefined function: ...'
Socket() function does not exist in PHP at all. Use fsockopen() instead. http://www.php.net/fsockopen Sincerely, Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) [EMAIL PROTECTED] www.phpbeginner.com -Original Message- From: Odd Arne Beck [mailto:[EMAIL PROTECTED]] Sent: Friday, April 05, 2002 1:23 AM To: [EMAIL PROTECTED] Subject: [PHP] Sockets with windows / win32 - 'call to undefined function: ...' Hi.. The error I get is this one: Fatal error: Call to undefined function: socket() i'm 100% positive that all my code is correct.. anyone else have this problem, or anyone else NOT having this problem? I have a win2000 with iis 5.0 and it's latest updates. the latest version of php too.. Best regards, -Oddis- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets and Telnet
On Tue, 19 Mar 2002, Hunter, Ray wrote: Has anyone created a telnet session in php with sockets and can give me some help on setting one up? Take a look at PHP Shell to see how it's done. http://www.gimpster.com/php/phpshell/index.php Cheers, Nick Winfield. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
This should help you. It is the fsockopen version of what I posed about 6 hours ago. function getdata ($host, $port, $data) { $fp = fsockopen ( $host, $port, $ec, $es, 5.0); if ( !$fp ) { exit (ERROR $ec: $es\n); } fputs ($fp, $data); while ( !feof($fp) ) { $buffer = fgets ($fp,128); $string .= $buffer; } fclose ($fp); return $string; } sends $data to $host:$port and returns the response, until the connection is severed. something like getdata (www.php.net, 80, GET / HTTP/1.0\r\n\r\n) should work out well for you On Monday 04 February 2002 07:15, you wrote: Hi, I am trying to access a remote file by opening a socket on port 80 but keep getting refused... The server does listen on port 80 as i can access it via browser... my code reads like this: ?php $Destination = http://srv157:7001/Palmeira2Application/palmeira2.servlets.communicator.Co m municator; $Request = action=$actionusername=$unamepassword=$password; $cfgTimeOut = 20; $theURL = parse_url($Destination); $host = $theURL[host]; $path = $theURL[path]; $port = $theURL[port]; if ($port==) $port=80; $header = POST .$path. HTTP/1.0\r\n; $header .= Host: .$host.\r\n; $header .= Accept: */*\r\n; $header .= Accept-Language: en\r\n; $header .= User-Agent: PHP/4.0.6 (Apache/1.3.20)\r\n; $header .= Content-Type: text/xml\r\n; $header .= Content-Length: .strlen($Request).\r\n; $header .= Content: \r\n\r\n; $msg = $header. $Request ; $Response = ; echo $port; //echo $host; // open a socket if(!$cfgTimeOut) // without timeout $fp = fsockopen($host,$port); else // with timeout $fp = fsockopen($host,$port, $errno, $errstr, $cfgTimeOut); if ($fp) { if (!fputs($fp,$msg,strlen($msg))) { return false; } // S E N D while (!feof($fp)) {$Response .= fgets($fp,32768);} fclose($fp); // R E C E I V E } else { echo Unable to access (.$Destination.).br; echo a href=\javascript:history.go(-1)\Try another/a;} print $response\n; print hello; ? any suggestions pl?? TIa, sands -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sockets
Hi, nope, its still not working.. Along with the Host and port, I Have to send 3 more fields, Action,Username and Password for authentification and it has to be Post for the Servlet does not respond to Get requests.. any more ideas pl??? Thnx... sands -Original Message- From: Evan Nemerson [mailto:[EMAIL PROTECTED]] Sent: segunda-feira, 4 de Fevereiro de 2002 15:25 To: Sandeep Murphy Cc: [EMAIL PROTECTED] Subject: Re: [PHP] sockets This should help you. It is the fsockopen version of what I posed about 6 hours ago. function getdata ($host, $port, $data) { $fp = fsockopen ( $host, $port, $ec, $es, 5.0); if ( !$fp ) { exit (ERROR $ec: $es\n); } fputs ($fp, $data); while ( !feof($fp) ) { $buffer = fgets ($fp,128); $string .= $buffer; } fclose ($fp); return $string; } sends $data to $host:$port and returns the response, until the connection is severed. something like getdata (www.php.net, 80, GET / HTTP/1.0\r\n\r\n) should work out well for you On Monday 04 February 2002 07:15, you wrote: Hi, I am trying to access a remote file by opening a socket on port 80 but keep getting refused... The server does listen on port 80 as i can access it via browser... my code reads like this: ?php $Destination = http://srv157:7001/Palmeira2Application/palmeira2.servlets.communicator.Co m municator; $Request = action=$actionusername=$unamepassword=$password; $cfgTimeOut = 20; $theURL = parse_url($Destination); $host = $theURL[host]; $path = $theURL[path]; $port = $theURL[port]; if ($port==) $port=80; $header = POST .$path. HTTP/1.0\r\n; $header .= Host: .$host.\r\n; $header .= Accept: */*\r\n; $header .= Accept-Language: en\r\n; $header .= User-Agent: PHP/4.0.6 (Apache/1.3.20)\r\n; $header .= Content-Type: text/xml\r\n; $header .= Content-Length: .strlen($Request).\r\n; $header .= Content: \r\n\r\n; $msg = $header. $Request ; $Response = ; echo $port; //echo $host; // open a socket if(!$cfgTimeOut) // without timeout $fp = fsockopen($host,$port); else // with timeout $fp = fsockopen($host,$port, $errno, $errstr, $cfgTimeOut); if ($fp) { if (!fputs($fp,$msg,strlen($msg))) { return false; } // S E N D while (!feof($fp)) {$Response .= fgets($fp,32768);} fclose($fp); // R E C E I V E } else { echo Unable to access (.$Destination.).br; echo a href=\javascript:history.go(-1)\Try another/a;} print $response\n; print hello; ? any suggestions pl?? TIa, sands -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
The server does listen on port 80 as i can access it via browser... http://srv157:7001/Palmeira2Application/palmeira2.servlets.communicator.Co m municator; Am I missing something here? What's srv157:7001? Why don't you first try http://www.google.com? Bogdan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sockets
srv157 Stands for the server same and 7001 is the port.. with www.google.com, get a request denied error... sands -Original Message- From: Bogdan Stancescu [mailto:[EMAIL PROTECTED]] Sent: segunda-feira, 4 de Fevereiro de 2002 16:01 To: Sandeep Murphy; [EMAIL PROTECTED] Subject: Re: [PHP] sockets The server does listen on port 80 as i can access it via browser... http://srv157:7001/Palmeira2Application/palmeira2.servlets.communicator.Co m municator; Am I missing something here? What's srv157:7001? Why don't you first try http://www.google.com? Bogdan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets
I've found fsockopen to be very reliable. I've done a few socket scripts with PHP (WhoisPro, et al). I believe the easiest way to check for a remotely closed socket is to do an fgets() and check for EOF: while (!feof($connection)) { $buffer .= fgets($connection, 4096); // 4096 is the chunk size, you can adjust it } I *think* EOF on a socket deonotes a remote socket close but I could be horribly wrong. Mike Frazer Evan Nemerson [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I am aware of cURL, but I want to just use the standard PHP stuff if I can because I plan on releasing this when I'm done, and want to KISS for other people. I know people have to compile PHP with sockets, but they will anyways for this project- I'm going to need socket_listen and socket_create_listen too. This is for a proxy server which will work kinda like multiproxy, but should be more powerful. It will support direct connections or going through another proxy server. It seperates anonymous from non-anonymous proxy servers, then sorts them by speed. Data is stored in tab seperated value text files (I'm even avoiding mySQL!!!) I just signed up for a page @ sourceforge. If anyone is interesting in helping out e-mail me. Thanks for the idea, though. I think right now my fall-back is fsockopen. I would really love to get sockets working for this... On Sunday 03 February 2002 23:32, you wrote: A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote: Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send GET / HTTP/1.0\r\n\r\n over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) { /* well, the below comment would be true if i could get it working! */ /* This function sends $data to $host:$port, then returns the response * until connection is severed. Great for HTTP, but won't usually work * too well in protocols where data needs to be analyzed, and replied * to appropriatly, such as POP v3 */ // Create a socket $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname(TCP)); if ( !$so ) { exit(Could not create socket.\n); } // Connect... $ec = socket_connect ($so, $host, $port); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } /* Write $data to socket. The manual doesn't say what it returns, but I'll * assume (even though it makes an ass out of you and me) that it is the same * as socket_connect() because it wouldn't be logical to return a descriptor. */ $ec = socket_write ( $so, $data, ( strlen($data) )); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } else { /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */ $x = 0; while ( $x == 0 ) { $buffer = socket_read ( $so, 1, PHP_BINARY_READ); $string .= $buffer; } } // And (hopefully) return $string, for your viewing pleasure. return $string; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sockets
You have two options. You could use cURL, or you could do a post request by hand. I reccomend the first, but if you need to do it by hand, you'll need to read up on the HTTP/1.1 RFC (2616, if memory serves, but im not 100% on that one). NOTE: With the script i sent you, if you need to send more headers just add them onto the $data arg. On Monday 04 February 2002 07:35, you wrote: Hi, nope, its still not working.. Along with the Host and port, I Have to send 3 more fields, Action,Username and Password for authentification and it has to be Post for the Servlet does not respond to Get requests.. any more ideas pl??? Thnx... sands -Original Message- From: Evan Nemerson [mailto:[EMAIL PROTECTED]] Sent: segunda-feira, 4 de Fevereiro de 2002 15:25 To: Sandeep Murphy Cc: [EMAIL PROTECTED] Subject: Re: [PHP] sockets This should help you. It is the fsockopen version of what I posed about 6 hours ago. function getdata ($host, $port, $data) { $fp = fsockopen ( $host, $port, $ec, $es, 5.0); if ( !$fp ) { exit (ERROR $ec: $es\n); } fputs ($fp, $data); while ( !feof($fp) ) { $buffer = fgets ($fp,128); $string .= $buffer; } fclose ($fp); return $string; } sends $data to $host:$port and returns the response, until the connection is severed. something like getdata (www.php.net, 80, GET / HTTP/1.0\r\n\r\n) should work out well for you On Monday 04 February 2002 07:15, you wrote: Hi, I am trying to access a remote file by opening a socket on port 80 but keep getting refused... The server does listen on port 80 as i can access it via browser... my code reads like this: ?php $Destination = http://srv157:7001/Palmeira2Application/palmeira2.servlets.communicator.Co m municator; $Request = action=$actionusername=$unamepassword=$password; $cfgTimeOut = 20; $theURL = parse_url($Destination); $host = $theURL[host]; $path = $theURL[path]; $port = $theURL[port]; if ($port==) $port=80; $header = POST .$path. HTTP/1.0\r\n; $header .= Host: .$host.\r\n; $header .= Accept: */*\r\n; $header .= Accept-Language: en\r\n; $header .= User-Agent: PHP/4.0.6 (Apache/1.3.20)\r\n; $header .= Content-Type: text/xml\r\n; $header .= Content-Length: .strlen($Request).\r\n; $header .= Content: \r\n\r\n; $msg = $header. $Request ; $Response = ; echo $port; //echo $host; // open a socket if(!$cfgTimeOut) // without timeout $fp = fsockopen($host,$port); else // with timeout $fp = fsockopen($host,$port, $errno, $errstr, $cfgTimeOut); if ($fp) { if (!fputs($fp,$msg,strlen($msg))) { return false; } // S E N D while (!feof($fp)) {$Response .= fgets($fp,32768);} fclose($fp); // R E C E I V E } else { echo Unable to access (.$Destination.).br; echo a href=\javascript:history.go(-1)\Try another/a;} print $response\n; print hello; ? any suggestions pl?? TIa, sands -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets
That's the first thing I tried- doesn't work with the lower-level sockets. (the socket_* functions) Right now I have an fsockopen version, and I'm commenting out the socket version- hopefully I'll be able to get it to work later. Thanks, but do you have any other ideas??? -Evan On Monday 04 February 2002 14:53, you wrote: I've found fsockopen to be very reliable. I've done a few socket scripts with PHP (WhoisPro, et al). I believe the easiest way to check for a remotely closed socket is to do an fgets() and check for EOF: while (!feof($connection)) { $buffer .= fgets($connection, 4096); // 4096 is the chunk size, you can adjust it } I *think* EOF on a socket deonotes a remote socket close but I could be horribly wrong. Mike Frazer Evan Nemerson [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I am aware of cURL, but I want to just use the standard PHP stuff if I can because I plan on releasing this when I'm done, and want to KISS for other people. I know people have to compile PHP with sockets, but they will anyways for this project- I'm going to need socket_listen and socket_create_listen too. This is for a proxy server which will work kinda like multiproxy, but should be more powerful. It will support direct connections or going through another proxy server. It seperates anonymous from non-anonymous proxy servers, then sorts them by speed. Data is stored in tab seperated value text files (I'm even avoiding mySQL!!!) I just signed up for a page @ sourceforge. If anyone is interesting in helping out e-mail me. Thanks for the idea, though. I think right now my fall-back is fsockopen. I would really love to get sockets working for this... On Sunday 03 February 2002 23:32, you wrote: A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote: Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send GET / HTTP/1.0\r\n\r\n over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) { /* well, the below comment would be true if i could get it working! */ /* This function sends $data to $host:$port, then returns the response * until connection is severed. Great for HTTP, but won't usually work * too well in protocols where data needs to be analyzed, and replied * to appropriatly, such as POP v3 */ // Create a socket $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname(TCP)); if ( !$so ) { exit(Could not create socket.\n); } // Connect... $ec = socket_connect ($so, $host, $port); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } /* Write $data to socket. The manual doesn't say what it returns, but I'll * assume (even though it makes an ass out of you and me) that it is the same * as socket_connect() because it wouldn't be logical to return a descriptor. */ $ec = socket_write ( $so, $data, ( strlen($data) )); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } else { /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */ $x = 0; while ( $x == 0 ) { $buffer = socket_read ( $so, 1, PHP_BINARY_READ); $string .= $buffer; } } // And (hopefully) return $string, for your viewing pleasure. return $string; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets
As I said, you're probably better off with fsockopen() anyway. Remember, the oure socket functions are experimental (or at least were last time I checked that part of the manual) and you never really know with experimental things. As well, they may change at any time, rendering your scripts useless on later versions of PHP. The Network functions, as classified in the manual, are very solid and widely supported. If it works the way you want it to with one method, why recreate your own wheel? Mike Frazer Evan Nemerson [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... That's the first thing I tried- doesn't work with the lower-level sockets. (the socket_* functions) Right now I have an fsockopen version, and I'm commenting out the socket version- hopefully I'll be able to get it to work later. Thanks, but do you have any other ideas??? -Evan On Monday 04 February 2002 14:53, you wrote: I've found fsockopen to be very reliable. I've done a few socket scripts with PHP (WhoisPro, et al). I believe the easiest way to check for a remotely closed socket is to do an fgets() and check for EOF: while (!feof($connection)) { $buffer .= fgets($connection, 4096); // 4096 is the chunk size, you can adjust it } I *think* EOF on a socket deonotes a remote socket close but I could be horribly wrong. Mike Frazer Evan Nemerson [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I am aware of cURL, but I want to just use the standard PHP stuff if I can because I plan on releasing this when I'm done, and want to KISS for other people. I know people have to compile PHP with sockets, but they will anyways for this project- I'm going to need socket_listen and socket_create_listen too. This is for a proxy server which will work kinda like multiproxy, but should be more powerful. It will support direct connections or going through another proxy server. It seperates anonymous from non-anonymous proxy servers, then sorts them by speed. Data is stored in tab seperated value text files (I'm even avoiding mySQL!!!) I just signed up for a page @ sourceforge. If anyone is interesting in helping out e-mail me. Thanks for the idea, though. I think right now my fall-back is fsockopen. I would really love to get sockets working for this... On Sunday 03 February 2002 23:32, you wrote: A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote: Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send GET / HTTP/1.0\r\n\r\n over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) { /* well, the below comment would be true if i could get it working! */ /* This function sends $data to $host:$port, then returns the response * until connection is severed. Great for HTTP, but won't usually work * too well in protocols where data needs to be analyzed, and replied * to appropriatly, such as POP v3 */ // Create a socket $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname(TCP)); if ( !$so ) { exit(Could not create socket.\n); } // Connect... $ec = socket_connect ($so, $host, $port); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } /* Write $data to socket. The manual doesn't say what it returns, but I'll * assume (even though it makes an ass out of you and me) that it is the same * as socket_connect() because it wouldn't be logical to return a descriptor. */ $ec = socket_write ( $so, $data, ( strlen($data) )); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } else { /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */ $x = 0; while ( $x == 0 ) { $buffer = socket_read ( $so, 1, PHP_BINARY_READ); $string .= $buffer; } } // And (hopefully) return
Re: [PHP] Sockets
A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote: Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send GET / HTTP/1.0\r\n\r\n over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) { /* well, the below comment would be true if i could get it working! */ /* This function sends $data to $host:$port, then returns the response * until connection is severed. Great for HTTP, but won't usually work * too well in protocols where data needs to be analyzed, and replied * to appropriatly, such as POP v3 */ // Create a socket $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname(TCP)); if ( !$so ) { exit(Could not create socket.\n); } // Connect... $ec = socket_connect ($so, $host, $port); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } /* Write $data to socket. The manual doesn't say what it returns, but I'll * assume (even though it makes an ass out of you and me) that it is the same * as socket_connect() because it wouldn't be logical to return a descriptor. */ $ec = socket_write ( $so, $data, ( strlen($data) )); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } else { /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */ $x = 0; while ( $x == 0 ) { $buffer = socket_read ( $so, 1, PHP_BINARY_READ); $string .= $buffer; } } // And (hopefully) return $string, for your viewing pleasure. return $string; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets
I am aware of cURL, but I want to just use the standard PHP stuff if I can because I plan on releasing this when I'm done, and want to KISS for other people. I know people have to compile PHP with sockets, but they will anyways for this project- I'm going to need socket_listen and socket_create_listen too. This is for a proxy server which will work kinda like multiproxy, but should be more powerful. It will support direct connections or going through another proxy server. It seperates anonymous from non-anonymous proxy servers, then sorts them by speed. Data is stored in tab seperated value text files (I'm even avoiding mySQL!!!) I just signed up for a page @ sourceforge. If anyone is interesting in helping out e-mail me. Thanks for the idea, though. I think right now my fall-back is fsockopen. I would really love to get sockets working for this... On Sunday 03 February 2002 23:32, you wrote: A quick note... If you are not aware of cURL (curl.haxx.se), then you may want to look into it. If you are, then please disregard this post. -Jason Garber At 11:06 PM 2/3/2002 -0800, Evan Nemerson wrote: Anyone know if there is a way yet to see if a socket is still connected to a host? I want to use a socket to send GET / HTTP/1.0\r\n\r\n over a socket, and retrieve everything the server sends. That part works great, but I can't figure out when the remote host disconnects. I have the CVS version of php. Here is the function so far. The problem is at the end. function getdata ($host, $port, $data) { /* well, the below comment would be true if i could get it working! */ /* This function sends $data to $host:$port, then returns the response * until connection is severed. Great for HTTP, but won't usually work * too well in protocols where data needs to be analyzed, and replied * to appropriatly, such as POP v3 */ // Create a socket $so = socket_create (AF_INET, SOCK_STREAM, getprotobyname(TCP)); if ( !$so ) { exit(Could not create socket.\n); } // Connect... $ec = socket_connect ($so, $host, $port); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } /* Write $data to socket. The manual doesn't say what it returns, but I'll * assume (even though it makes an ass out of you and me) that it is the same * as socket_connect() because it wouldn't be logical to return a descriptor. */ $ec = socket_write ( $so, $data, ( strlen($data) )); if ( $ec 0 ) { exit (ERROR $ec: .socket_strerror($ec)); } else { /* PROBLEM IS HERE- what do I put instead of while ( $x == 0 )??? */ $x = 0; while ( $x == 0 ) { $buffer = socket_read ( $so, 1, PHP_BINARY_READ); $string .= $buffer; } } // And (hopefully) return $string, for your viewing pleasure. return $string; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sockets on FreeBSD Mac OS X
Hi, A bind error means that the port is being used by another application, incase that's incorrect you can assure yourself by doing this : socket_setopt(listener, SOL_SOCKET, SO_REUSEADDR, 1); This function is undocumented (yet) and would require you to download the latest version from CVS. Hope that helps. = * Know more about me: http://www.geocities.com/mimodit * __ Do You Yahoo!? Listen to your Yahoo! Mail messages from any phone. http://phone.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sockets on FreeBSD Mac OS X
Oops, socket_setopt($listener, SOL_SOCKET, SO_REUSEADDR, 1); = * Know more about me: http://www.geocities.com/mimodit * __ Do You Yahoo!? Listen to your Yahoo! Mail messages from any phone. http://phone.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sockets on FreeBSD Mac OS X
On Wed, 3 Oct 2001 02:02, Devon Weller wrote: Has anyone successfully gotten socket functions to work with FreeBSD? More specifically, Mac OS X? I always get the following error: Can't bind to port 12345, exiting. The script works fine on Linux machines. Is there a patch in the works for FreeBSD? If so, I would be very happy. There's a known trojan (netbus?) that operates on 12345. That's not your problem is it? Do a netstat -an on the machine and see if that port IS in use. Cheers, Brad -- Brad Hubbard Congo Systems 12 Northgate Drive, Thomastown, Victoria, Australia 3074 Ph: +61-3-94645981 Fax: +61-3-94645982 Mob: +61-419107559 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sockets in Win98.
If you get the call to undefined function error, I'd say that it doesn't have the socket functions built in. I'm not positive, that'd be my guess though. Tyler Longren Captain Jack Communications [EMAIL PROTECTED] www.captainjack.com On Thu, 09 Aug 2001 15:19:40 +0100 Michael Quinn [EMAIL PROTECTED] wrote: Hi, Have been trying to open a socket with PHP on Windows98 and get a call to undefined function error for the socket function. Does the Win32 version of PHP come with socket support. BTW: Using PHP 4.10 Thanks in advance. M -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sockets
No, your problem is most likely that you didn't do anything with the telnet negotiation phase. Read the RFC on telnet, and you will find that there is a whole client-server negotiation phase going on. Kinda like a modem handshake. It is really quite difficult, and no one has ported a Telnet class to PHP yet. (big big task) There is a Net::Telnet class in perl that I ended up using to write a script to do something similar, and pass results back to the php script that called it. Telnet is simply not as easy as opening a socket. If you don't send the right set of characters as the first block, it won't work. On 4/9/01 10:16 AM, "Matthias Winkelmann" [EMAIL PROTECTED] wrote: Hi! I'm trying to speed up a big application by splitting it into a codebase acting as a server and the actual scripts communicating with the server using sockets. I got the server working, at least it works when I send a request via telnet. When I try to let a script act as the client I get no response. I think the problem is the length parameter in the read()-function. Not all requests and results are 2048 bytes, but I have no idea what to use as a delimiter instead. Here are the scripts so far: Server: // Socket was created, bind listen executed do { if (($msgsock = accept_connect($sock)) 0) { // wait for request echo "accept_connect() failed: reason: " . strerror ($msgsock) . "\n"; break; } do { $buf = ''; $ret = read ($msgsock, $buf, 2048); // read request echo "request: $ret br"; if ($ret 0) { echo "read() failed: reason: " . strerror ($ret) . "\n"; break 2; } if ($ret == 0) { break 2; } $buf = trim ($buf); $talkback = eval($buf);// request verabeiten write ($msgsock, $talkback, strlen ($talkback)); // write result to socket echo "$buf\n"; } while (true); close ($msgsock); } while (true); Client: // Socket was created; submitting request write ($socket, $in, strlen ($in)); while (read ($socket, $out, 2048)) // reading response. what if the response is 2048 bytes? { echo $out; } As I said: The server works perfectly using telnet, but the script-client does not give any output, allthough the connection was created successfully. Thanks in advance for any answers. I hope I was able to describe the problem well enough. Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sockets
well, if they did, and they made a full telnet class, then great!! give us all the class!! BUT, if they wrote their own socket listener, essentially their own protocol, then it would work. but if you are using sockets to connect to a real telnet server (as it were) then you MUST have the negotiation part. http (port 80) is just raw socket transport. telnet (port 23) is not. so, communication like you want is possible, just don't count on a real telnet connection right yet. On 4/9/01 3:27 PM, "Matthias Winkelmann" [EMAIL PROTECTED] wrote: -Original Message- From: Lindsay Adams [mailto:[EMAIL PROTECTED]] No, your problem is most likely that you didn't do anything with the telnet negotiation phase. [..] That doesn't sound very encouraging :-( Anyway, I got that idea of splitting an application into a codebase listening on a port and a frontend connecting with it in the zend tips section http://www.zend.com/tips/tips.php?id=169single=1 . It seems as if somebody got it to work (in 15 min as he writes). Unfortunately, I couldn't find his email adress anywhere. Therefore: If anybody has done this before, has an idea how it could work or knows this author 'npeen', it could really save me a lot of time. Thanks for all ideas, Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sockets (long)
Yasuo Ohgaki wrote: set_nonblock() is in PHP C source, but not in the PHP Manual. May be it's dead? It seems it take one parameter (file descriptor), let us know if it works. I might want to use it in the future :) Yasuo, There is one mention of set_nonblock() in the manual under accept_connect() under the socket functions. I believe that the new socket functions non blocking mode is dead in the current version of php at least under linux. Regards, Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sockets (long)
set_nonblock() is in PHP C source, but not in the PHP Manual. May be it's dead? It seems it take one parameter (file descriptor), let us know if it works. I might want to use it in the future :) From socket.c 529 /* {{{ proto bool set_nonblock(int fd) 530Sets nonblocking mode for file descriptor fd */ 531 PHP_FUNCTION(set_nonblock) 532 { 533 zval **fd; 534 int ret; 535 536 if (ZEND_NUM_ARGS() != 1 || 537 zend_get_parameters_ex(1, fd) == FAILURE) { 538 WRONG_PARAM_COUNT; 539 } 540 convert_to_long_ex(fd); 541 542 ret = fcntl(Z_LVAL_PP(fd), F_SETFL, O_NONBLOCK); 543 544 RETURN_LONG(((ret 0) ? -errno : ret)); 545 } 546 /* }}} */ Hope this helps. -- Yasuo Ohgaki "Joseph Blythe" [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Plutarck wrote: Very recently (a few days at most ago) someone was complaining about the problem you are having. According to them they can't get the socket function to accept socket nonblocking. It would seem that the function is broken, so you can't set nonblocking to a valid value at the current time. Hopefully it will be fixed in 4.0.5, due out in a few days. It was probaly me as I posted a message about this a few days ago ;-) I really hope that it does get fixed in 4.0.5 Oh well as they say !@#$ happens, Thanks, Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sockets (long)
Plutarck wrote: Very recently (a few days at most ago) someone was complaining about the problem you are having. According to them they can't get the socket function to accept socket nonblocking. It would seem that the function is broken, so you can't set nonblocking to a valid value at the current time. Hopefully it will be fixed in 4.0.5, due out in a few days. It was probaly me as I posted a message about this a few days ago ;-) I really hope that it does get fixed in 4.0.5 Oh well as they say !@#$ happens, Thanks, Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sockets
The online manual has some working examples Also, using the socket funcs in Php isn't very different from using them in C - try searching www.google.com if the examples in the manual aren't enough jason - Original Message - From: "Boget, Chris" [EMAIL PROTECTED] To: "Php (E-mail)" [EMAIL PROTECTED] Sent: Wednesday, January 24, 2001 8:00 AM Subject: [PHP] Sockets Are there any tutorials on how to use Sockets in PHP? If so, where can I find them? I've looked on several of the sites that are linked from the main php.net site but found nothing... :/ Thanks. Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]