2

Ok, well im working on a c# application that is acting as the server on port 4. And a php script on my website acting as the client. It can connect to the server but, when the server or client attempts to send data. The data turns out to just be random numbers and symbols. Scripts below

PHP:

$msg = $host + " connected;";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $host, 4);
socket_write($socket, $msg, strlen($msg));
socket_close($socket);

C#:

while (heartbeat == false)
                {
                    if (listener.Pending())
                    {
                        heartbeatC = listener.AcceptTcpClient();
                        //heartbeatS = listener.AcceptSocket();
                        NetworkStream heartBeatStream = heartbeatC.GetStream();
                        string heartbeatEP = heartbeatC.Client.RemoteEndPoint.ToString();
                        string heartbeatIP = heartbeatEP.Remove(heartbeatEP.IndexOf(':'), heartbeatEP.Length - heartbeatEP.IndexOf(':'));
                        if (heartbeatIP == Dns.GetHostAddresses("***.********.com")[0].ToString())
                        {
                            dottime.Enabled = false;
                            Console.WriteLine("\nHeartbeat.");
                            bool heartbeatR = false;
                            while (heartbeatR == false)
                            {
                                if (heartBeatStream.DataAvailable)
                                {
                                    //StreamReader sr = new StreamReader(heartBeatStream);
                                    byte[] message = new byte[1024];
                                    int bytesRead = 0;
                                    bytesRead = heartbeatC.Client.Receive(message);
                                    ASCIIEncoding encoder = new ASCIIEncoding();
                                    string msg = encoder.GetString(message);
                                    Console.WriteLine("Server: " + msg);
                                    heartbeatR = true;
                                }
                            }
                            heartbeat = true;
                        }
                        else
                        {
                            heartbeatC.Client.Disconnect(true);
                        }
                    }
                }
2
  • Unrelated tip: Use the using statement to ensure that resources like streams and sockets get closed correctly. Now, by "random numbers and symbols" you mean they're different each run (random), or they're always the same, but wrong? Commented Jan 17, 2011 at 1:13
  • I reverted back your changes (you deleted the whole question!). Let it be, for future reference to others who might have the same problem. This is what StackOverflow is all about. ;) Commented Jan 17, 2011 at 2:33

1 Answer 1

3

I think your issue is with this line:

$msg = $host + " connected;";

The string concatenation operator in PHP is .:

$msg = $host . " connected;";
Sign up to request clarification or add additional context in comments.

1 Comment

IMO that's a simple mistake that shouldn't make you feel embarrassed. It's a small syntactical error and I do that with PHP every once in awhile since I mostly do C# work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.