Re: php sockets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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"" <mentard@xxxxxxxxx> wrote in message 
news:54.17.23767.FBF06674@xxxxxxxxxxxxxxx
> <?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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux