[PHP] php sockets

2007-12-17 Thread vixle


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

2007-12-18 Thread vixle
this code doesn't interact with with php client while with c++ based one it
works just fine.
.anybody?
#include 
#include 
#include 
#include 
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]
> 
> /* 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

2007-12-18 Thread vixle
this code doesn't interact with php client while with c++ based one it
works just fine.
.anybody?
#include 
#include 
#include 
#include 
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]
> 
> /* 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] Re: php sockets

2007-12-20 Thread vixle
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

2007-12-20 Thread vixle
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