PROBLEM in SOCKET programming

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

PROBLEM in SOCKET programming

Post by susrisha »

Hi,
I have a C# server which runs on certain port number..
Earlier i had created a C# client to send the message to this server and it was working all fine..
now since i figured out PHP has socket programming, i want to move the client program to php..

In a nut shell....
There is a server running on PORT : 1234 and i want to send some information to it through PHP code..

Can anyone help me in this regard.. i tried socket_create function but it doesnt create a socket connection since the socket is already used by the server..

HOW TO WRITE A CLIENT TO SEND MESSAGE TO A SERVER RUNNING ON A PARTICULAR SOCKET??
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PROBLEM in SOCKET programming

Post by it2051229 »

C# sockets run on TCP(transmission control protocol) channel... PHP for the web RUNS on HTTP(hyper text transfer protocol).. is it possible for the two ports to communicate? am sorry mate but NO.. anyways, C# can run via HTTP too but not for sockets, its another form of networking technology for C# you need to learn.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PROBLEM in SOCKET programming

Post by onion2k »

it2051229 wrote:C# sockets run on TCP(transmission control protocol) channel... PHP for the web RUNS on HTTP(hyper text transfer protocol).. is it possible for the two ports to communicate? am sorry mate but NO.. anyways, C# can run via HTTP too but not for sockets, its another form of networking technology for C# you need to learn.
What? That's not right. PHP can use TCP for sockets.

http://uk2.php.net/manual/en/function.socket-create.php
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PROBLEM in SOCKET programming

Post by it2051229 »

wait i'm confused.. i used to create C# network applications and C# provides TCP or HTTP for the protocol to use.. I've tried both, although for the HTTP it is compatible with other web applications... either way I know it won't work.. i've tried doing this experiment before.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PROBLEM in SOCKET programming

Post by onion2k »

PHP can do both HTTP and TCP (and UDP) over an IPv4, IPv6 or Unix socket. If your experiment failed it's because you did it wrong, not because PHP is incapable.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PROBLEM in SOCKET programming

Post by it2051229 »

hmm.. interesting... ok so let's say i have a C# application that is listening to port 5000... using PHP code, i would like to connect to my C# application server running at localhost listening to port 5000 using sockets... if successful, the C# application should be able to retrieve like for example the username and or password which the user has entered on the PHP page. If you think it is possible you might want to show me an article or procedure on how to do this.. cause if it is, that would be great, am going to continue what i was working back then...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PROBLEM in SOCKET programming

Post by onion2k »

I'm not sure what you mean by "the C# application should be able to retrieve like for example the username and or password" ... if the C# app is the server then it'll just respond to connections and do what it's told. It won't be able to retrieve anything. It'll be able to listen for connections from PHP and do things when you write to the socket that's connected to it. That doesn't seem to be what you have in mind though.

Just read the comments section of http://uk.php.net/socket_create and http://uk.php.net/manual/en/function.socket-connect.php
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PROBLEM in SOCKET programming

Post by it2051229 »

it <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> worked.. aarrrggghhhh oohh nooo wasted time...
anyways yeah i managed to let it work... here's how to do it

The code below is a C# application code which runs in a console application. The C# acts as a server which listens to port 5000 and waiting for PHP to connect if executed

Code: Select all

 
using System;
using System.Text;
 
/*
 * Systems we need for network communications
 * and some message box
 */
using System.Net.Sockets;
using System.Windows.Forms;
using System.IO;
 
namespace ConsoleApplication
{
    class Program
    {
        /**
         * @name: main
         * @desc: entry point of the program
         */
        static void Main(string[] args)
        {
            TcpListener tcpListener = new TcpListener(5000);
            tcpListener.Start();
 
            while (true)
            {
                // wait for an application to connect to this server
                Console.Write("Waiting for php to connect...");
                Socket serverSocket = tcpListener.AcceptSocket();
                Console.WriteLine("Connected");
 
                // if connected, create the necessary streams we need for accepting and returning values
                NetworkStream networkStream = new NetworkStream(serverSocket);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);
 
                // waiting for PHP to send data
                Console.Write("Waiting for php data...");
                string messageFromPHP = streamReader.ReadLine();
                Console.WriteLine("Received");
 
                // display
                MessageBox.Show(messageFromPHP);
            }
 
            return;
        }
    }
}
 
This is the PHP that we can make use of to connect to the C# application which is currently listening to port 5000.

Code: Select all

 
<?php
    if(isset($_POST["sendMessage"]))
    {
        /*
        * We're going to create a socket for IPv4 which can stream
        * data in TCP or UDP.. but for this experiment, it would be TCP
        */
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        
        /*
        * Next to do is to connect this socket to the C# application
        * who is listening at port 5000.
        */
        if(socket_connect($socket, "localhost", 5000))
        {
            /*
            * Then we're going to send the data to C#
            * and IF it happens that C# will accept the data, it will display it in a message dialog box.
            */
            socket_write($socket, $_POST["message"]);
            
            echo "DATA SENT. OH <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>, IT WORKED.";
        }
        else
        {
            echo "SORRY MATE I TOLD YOU THIS EXPERIMENT WILL NOT WORK";
        }
    }
?>
<html>
    <head>
        <title>PHP and C#</title>
    </head>
    <body>
        <form method='post' action='<?php echo $_SERVER['PHP_SELF'] ?>'>
            Enter something damn it....<br/>
            <textarea name='message'>
            </textarea><br/>
            <input type='submit' value='Send message to C#' name='sendMessage' />
        </form>
    </body>
</html>
 
There... mystery solved :twisted: now to move to my evil plan...
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PROBLEM in SOCKET programming

Post by susrisha »

Thank you guys for ur help. I got wrote the following which happens to be working properly..

Code: Select all

 
$remote_socket= "192.168.1.103:3201";
$message = "server message";
           $stream = stream_socket_client($remote_socket,$errno,$errstr,30);
           if(!$stream)
             {
               echo " $errstr ($errorno)";
 
             }
             else
             {
 
                 fwrite($stream,$message );
 
             }
              fclose($stream);
 
this one establishes a client to the server running on that socket..and writes message as fwrite..
Thread
Forum Newbie
Posts: 3
Joined: Tue Nov 11, 2008 7:54 am

Re: PROBLEM in SOCKET programming

Post by Thread »

And thank you, too... Susrisha, 8) I had a similiar problem.
Post Reply