Click here to Skip to main content
13,050,896 members (75,716 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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

   echo "SERVER start --------- <br/>\n" ;
    $server_socket = stream_socket_server ("tcp://");    
    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"  );

// client -------------------
    $socket = stream_socket_client ('tcp://'); 
        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 /> " );
Posted 5-Sep-12 3:31am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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).
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100