所以我说我的php网站连接到我的C#Server, 但每当我刷新我的网页时,程序都会关闭并显示错误:
“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);
?>
有谁知道如何解决这个问题?
谢谢!