1

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

C# 与 Supersocket - 服务器端

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# 服务器进行通信。提前致谢。

4

1 回答 1

0

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

于 2017-09-09T10:43:56.730 回答