繁体   English   中英

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

[英]Php Client closing when Send request to Supersocket in C#

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM