PROBLEM in SOCKET programming
Moderator: General Moderators
PROBLEM in SOCKET programming
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??
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??
Re: PROBLEM in SOCKET programming
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.
Re: PROBLEM in SOCKET programming
What? That's not right. PHP can use TCP for sockets.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.
http://uk2.php.net/manual/en/function.socket-create.php
Re: PROBLEM in SOCKET programming
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.
Re: PROBLEM in SOCKET programming
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.
Re: PROBLEM in SOCKET programming
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...
Re: PROBLEM in SOCKET programming
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
Just read the comments section of http://uk.php.net/socket_create and http://uk.php.net/manual/en/function.socket-connect.php
Re: PROBLEM in SOCKET programming
it <span style='color:blue' title='I'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
This is the PHP that we can make use of to connect to the C# application which is currently listening to port 5000.
There... mystery solved
now to move to my evil plan...
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;
}
}
}
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'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>
Re: PROBLEM in SOCKET programming
Thank you guys for ur help. I got wrote the following which happens to be working properly..
this one establishes a client to the server running on that socket..and writes message as fwrite..
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);
Re: PROBLEM in SOCKET programming
And thank you, too... Susrisha,
I had a similiar problem.