Click here to Skip to main content
15,885,195 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!
SOCKET
Can anyone show me the code of
server in C , windows
and client in PHP using socket

I need only the simple option for one connection
SERVER in C and client in PHP

I have managed to do socket from php to php
Here is the code that worked for me :
But I dont know how to replace the PHP server side to C
Help!

PHP
   echo "SERVER start --------- <br/>\n" ;
    $server_socket = stream_socket_server ("tcp://192.168.0.234:80");    

    if ($server_socket ) {
        echo "server: OPENED <br/>" ;
    }
    else {
        echo "server: FAILED to open OPENED <br/>" ;
    }

    if ($server_socket ) {
	    while ($socket = stream_socket_accept($server_socket)) 	    {
            fwrite($socket, "Hi Hi Hi"  );
		    fclose($socket);
	    }
	    fclose($server_socket);
     }	


// client -------------------
    $socket = stream_socket_client ('tcp://84.109.216.234:80'); 

    if(!$socket){
        echo "<br>CLIENT:  error FAILED to open <br>";
	    echo "<br>$errstr<br>";
    }

    else    {
	    while (!feof($socket)) 	{
            $server_msg = fread($socket, 100);
	    }
        printf ( "client: reached eof <br /> " );
	    fclose($socket);
    }
Posted

1 solution

It's somewhat similar (as far as flow of things), but you have to use some sort of socket library to accomplish this (i.e. sockets aren't built into the language). In Windows, the native library/API is Winsock (or Winsock2) but if you require cross platform sockets, then you'd have to use some other socket library (from sys\socket.h to other libraries available that try to simplify the task, a library I've seen before called PracticalSockets comes to mind, but I believe that's also Winsock based).
 
Share this answer
 
Comments
Member 7999536 5-Sep-12 14:59pm    
Thanks ! do you mean that I cannot use Winsock ?
Albert Holguin 5-Sep-12 16:03pm    
Sure... but only if Windows is your only target platform.... Actually, Winsock does work in Linux across the Wine interoperability layer, but that's a whole different bag of worms. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900