开发者

php and c# tcp communication problem

开发者 https://www.devze.com 2023-02-04 08:38 出处:网络
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 attempt

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开发者_Python百科.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);
                        }
                    }
                }


I think your issue is with this line:

$msg = $host + " connected;";

The string concatenation operator in PHP is .:

$msg = $host . " connected;";
0

精彩评论

暂无评论...
验证码 换一张
取 消