Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ PHP socket
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!
 
   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 5-Sep-12 4:31am

1 solution

Rate this: bad
good
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).
  Permalink  
Comments
Member 7999536 at 5-Sep-12 14:59pm
   
Thanks ! do you mean that I cannot use Winsock ?
Albert Holguin at 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
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 587
2 Maciej Los 325
3 Mathew Soji 195
4 BillWoodruff 190
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
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