Php客户端在C#中向Supersocket发送请求时关闭

时间:2015-12-18 10:53:36

标签: c# php supersocket.net

我在c#中使用服务器,在PHP中使用客户端。我使用Supersocket [https://supersocket.codeplex.com/]在客户端和服务器之间进行通信。

C#with Supersocket - Serverside

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.Common;
using SuperSocket.SocketEngine;
using SuperSocket;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args) 
{
        Console.WriteLine("Press any key to start the WebSocketServer!");
        Console.ReadKey();
        Console.WriteLine();
        var appServer = new AppServer();
        //Setup the appServer
        if (!appServer.Setup(2020))
        {
            Console.WriteLine("Failed to setup!");
            Console.ReadKey();
            return;
        }
        appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConnected);
        appServer.NewRequestReceived += new RequestHandler<AppSession, SuperSocket.SocketBase.Protocol.StringRequestInfo>(appServer_NewRequestReceived);
        appServer.SessionClosed += new SessionHandler<AppSession, CloseReason>(appServer_SessionClosed);
        Console.WriteLine();

        //Try to start the appServer
        if (!appServer.Start())
        {
            Console.WriteLine("Failed to start!");
            Console.ReadKey();
            return;
        }

        Console.WriteLine("The server started successfully, press key 'q' to stop it!");

        while (Console.ReadKey().KeyChar != 'q')
        {
            Console.WriteLine();
            continue;
        }

        //Stop the appServer
        appServer.Stop();

        Console.WriteLine();
        Console.WriteLine("The server was stopped!");
        Console.ReadKey();
    }

    static void appServer_NewSessionConnected(AppSession session)
    {
        session.Send("Swelcome");
    }

    static void appServer_SessionClosed(AppSession session, CloseReason value)
    {
        session.Send("Server: " + "welcome");
    }

    static void appServer_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
    {
        try
        {
            switch (requestInfo.Key.ToUpper())
            {
                case ("ECHO"):
                    session.Send(requestInfo.Body);
                    break;

                case ("ADD"):
                    session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());
                    break;

                case ("MULT"):

                    var result = 1;

                    foreach (var factor in requestInfo.Parameters.Select(p => Convert.ToInt32(p)))
                    {
                        result *= factor;
                    }

                    session.Send(result.ToString());
                    break;
                default:
                    Console.WriteLine("Default");
                    session.Send(requestInfo.Body);
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

}
}

PHP代码 - 客户端

class XoneDataReciver
{
var $socketPtr;      
function OpenConnection($server, $port)
{
    $this->socketPtr = fsockopen($server, $port, $errno, $errstr, 0.4);
    if (!$this->socketPtr) {
        echo "Network down. Please refresh the page again or try again later."; exit();
    } else {
        return 0;
    }
}


function MakeRequest($action, $params = array())
{
    if (!$this->socketPtr)
        return "error";               
    $this->sendRequest($action); //Error - Client closing

    return $this->readAnswer(); // Got msg from server 
}



function sendRequest($request)
{   
    fputs($this->socketPtr, $request);
}


}
$xone_ip ="127.0.0.1";
$xone_port = "2020";
$xonerequest   = new XoneDataReciver;
$xonerequest->OpenConnection($xone_ip, $xone_port);
?>

我从服务器到客户端(PHP)收到了消息。但是当我尝试将msg从php发送到c#时,SessionClosed事件触发并且错误显示“客户端关闭”。有人帮我通过Supersocket将php客户端传递给c#服务器。提前谢谢。

1 个答案:

答案 0 :(得分:0)

增加服务器配置中的MaxConnectionNumber对我有用。