0

PHP から C ソケット サーバーに接続するための PHP 拡張機能を作成しました。次のコードで PHP 拡張機能を作成しました。

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <WinSock.h>
    #pragma comment(lib, "Ws2_32.lib")
    #include <windows.h>
    #include "stdafx.h"
    #define WSA_VERSION  MAKEWORD(2,2)
    #define SIZE sizeof(struct sockaddr_in)
    #define SOCKETBUF_SIZE 1025

    ////////////////////////// Globals
    int g_iSockfd;
    char g_acRcvData[SOCKETBUF_SIZE];

    ////////////////////////// type defines 
    //typedef unsigned short ssize_t;
    struct sockaddr_in server;


    /* declaration of functions to be exported */
    ZEND_FUNCTION(ConnectSocket);

    /* compiled function list so Zend knows what's in this module */
    zend_function_entry CustomExtModule_functions[] = {
        ZEND_FE(ConnectSocket, NULL)
        {NULL, NULL, NULL}
    };

    /* compiled module information */
    zend_module_entry CustomExtModule_module_entry = {
        STANDARD_MODULE_HEADER,
        "CustomExt Module",
        CustomExtModule_functions,
        NULL, NULL, NULL, NULL, NULL,
        NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
    };

    /* implement standard "stub" routine to introduce ourselves to Zend */
    ZEND_GET_MODULE(CustomExtModule)

    /* ConnectSocket function */
    ZEND_FUNCTION(ConnectSocket){
        char* i_pcIPAddress; 
        int i_iPortNumber;
        zend_bool return_long = 0;
                    // Receiving parameters
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd|b", &i_pcIPAddress, &i_iPortNumber, &return_long) == FAILURE) {
             RETURN_NULL();
        }
        //RETURN_STRING(i_pcIPAddress,1);
        int errorcode=0;
        WSADATA     WSAData = { 0 };

        //server. = {AF_INET, i_iPortNumber, INADDR_ANY};
        server.sin_family=AF_INET;
        server.sin_addr.s_addr = inet_addr(i_pcIPAddress);
        server.sin_port = i_iPortNumber;


        if ( 0 != WSAStartup( WSA_VERSION, &WSAData ) )
        {
            // Tell the user that we could not find a usable
            // WinSock DLL.
            if ( LOBYTE( WSAData.wVersion ) != LOBYTE(WSA_VERSION) ||
                 HIBYTE( WSAData.wVersion ) != HIBYTE(WSA_VERSION) )
                //printf("Incorrect version of WS2_32.dll found");

            WSACleanup( );
            errorcode = WSAGetLastError();
            RETURN_LONG(errorcode);
        }

        // close socket of same ipaddress and port if its open 
        if(g_iSockfd != -1)
        {
            int nRet = closesocket( g_iSockfd );
            nRet=shutdown(g_iSockfd,2);
        }
        if((g_iSockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {       
            errorcode = WSAGetLastError();
            RETURN_LONG(errorcode);
        }

        while( 1 )
        {
            if(connect(g_iSockfd, (struct sockaddr *)&server, SIZE) == -1) {
                errorcode = WSAGetLastError();
                RETURN_LONG(errorcode);
            }
            RETURN_STRING("Success",1);
        }   


    }

私の PHP 拡張機能は正常に動作しています。

しかし、適切な IP アドレスとポート番号で ConnectSocket 関数を呼び出そうとすると、応答としてエラー コード10061が返されます。つまり、ソケット接続が拒否されました。

ダイレクト C クライアント コードから接続している場合、その時点でソケット接続を正常に確立できます。そのコードで PHP 拡張機能を作成している場合、接続拒否エラーが発生します。

私の問題を解決するのを手伝ってください。

4

1 に答える 1