Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和#x2B;接收消息时出现PHP错误_C#_Php - Fatal编程技术网

C#和#x2B;接收消息时出现PHP错误

C#和#x2B;接收消息时出现PHP错误,c#,php,C#,Php,所以我让我的php网站连接到我的C#服务器, 但每当我刷新网页时,程序就会关闭并出现错误: “System.Net.Sockets.SocketException”类型的未处理异常 发生在System.dll中 错误出现在这里: private static void ReceiveCallback(IAsyncResult AR) { Socket current = (Socket)AR.AsyncState; int received; try {

所以我让我的php网站连接到我的C#服务器, 但每当我刷新网页时,程序就会关闭并出现错误:

“System.Net.Sockets.SocketException”类型的未处理异常 发生在System.dll中

错误出现在这里:

private static void ReceiveCallback(IAsyncResult AR)
{
    Socket current = (Socket)AR.AsyncState;
    int received;

    try
    {
        received = current.EndReceive(AR);
    }
    catch (SocketException)
    {
        Console.WriteLine("Client forcefully disconnected");
        current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
        _clientSockets.Remove(current);
        return;
    }

    byte[] recBuf = new byte[received];
    Array.Copy(_buffer, recBuf, received);
    string text = Encoding.ASCII.GetString(recBuf);
    Console.WriteLine("Received Text: " + text);

    if (text.ToLower() == "get time") // Client requested time
    {
        Console.WriteLine("Text is a get time request");
        byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
        current.Send(data);
        Console.WriteLine("Time sent to client");
    }
    else if (text.ToLower() == "exit") // Client wants to exit gracefully
    {
        // Always Shutdown before closing
        current.Shutdown(SocketShutdown.Both);
        current.Close();
        _clientSockets.Remove(current);
        Console.WriteLine("Client disconnected");
        return;
    }
    else
    {
        Console.WriteLine("Text is an invalid request");
        byte[] data = Encoding.ASCII.GetBytes("Invalid request");
        current.Send(data);
        Console.WriteLine("Warning Sent");
    }

    current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
}
可以肯定的是,这一行:

current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
编辑:返回的捕获:

10053=>软件导致连接中止。已建立的联系 被主机中的软件中止,可能是由于 数据传输超时或协议错误

所以我认为是PHP做了错误的数据传输:

<?PHP

require_once('connection.php');
if(isset($_POST['submit'])){

$porn = "Test";
$user = $_POST['user'];
$username = $user;
$ulen = strlen($username);
//socket_send($socket, $username, $ulen, 0);
socket_write($socket, $username);
$read = socket_read($socket,2048) or die("Cannot read from socket");

}else{
  echo "Not loaded";
}
?>

<html>
<head>
</head>
<body>
  <div class="status">
    <?php echo $read; ?>
  </div>
  <form method="POST">
    <input type="text" name="user">
    <input type="submit" name="submit" value="Load">
  </form>
</body>
</html>

Connection.php:

<?php
    $service_port = 100;
    $address = '127.0.0.1';
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_connect($socket, $address, $service_port)or die("Cannot connect");
    socket_set_nonblock($socket);
?>

有人知道如何解决这个问题吗


谢谢

您应该捕获异常,并查看消息和属性-在那里您将找到内部错误代码。然后查看并找到错误代码,它很可能会告诉您出了什么问题。我只从捕获中得到:您主机上的软件已终止连接<抱歉,我用我的语言翻译了它。我没有获得SocketException.ErrorCode属性SocketException引发以下问题:软件导致连接中止。主机中的软件中止了已建立的连接,可能是由于数据传输超时或协议错误。