【问题标题】:C++ and PHP socket connectionC++ 和 PHP 套接字连接
【发布时间】:2011-12-19 12:25:10
【问题描述】:

我想使用 php 连接到我的 c++ 服务器... 到目前为止,我得到了:

服务器.cpp

#include <winsock2.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET mySocket;
SOCKET myBackup;
SOCKET acceptSocket;
sockaddr_in myAddress;


 // setup WSA and socket....     
 if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
  {
    cerr<<"Socket Initialization: Error with WSAStartup\n";
    system("pause");
    WSACleanup();
    exit(10);
}

//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( mySocket == INVALID_SOCKET )
{
    cerr<<"Socket Initialization: Error creating socket"<<endl;
    system("pause");
    WSACleanup();
    exit(11);
}

myBackup = mySocket;


//bind
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
myAddress.sin_port = htons( 25555 );


if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
{
    cerr<<"ServerSocket: Failed to connect\n";
    system("pause");
    WSACleanup();
    exit(14);
}

//listen
if ( listen ( mySocket, 10 ) == SOCKET_ERROR )
{
    cout<<"ERR: ServerSocket: Error listening on socket\n";
    system("pause");
    WSACleanup();
    exit(15);
}
cout<<"srv started. listeninig...\n";

//accept
acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
    acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;

cout<<"done.\n";

return 0;
}

和php客户端

<?php
$port = 25555;
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
#socket_bind($socket,'',$port);
socket_connect($socket, '/my_ip/', $port);

echo 'connecting...';
?>

我不知道该怎么办。 c++ 服务器只是看不到 php 客户端...

【问题讨论】:

  • 您是否确认您的服务器完全接受来自任何事物的连接? telnet your.ip.add.ress 25555

标签: php c++ sockets


【解决方案1】:

检查错误代码...

socket_create

socket_create() returns a socket resource on success, or FALSE on error. 

socket_connect

Returns TRUE on success or FALSE on failure.
The error code can be retrieved with socket_last_error().
This code may be passed to socket_strerror() to get a textual explanation of the error.

【讨论】:

    【解决方案2】:

    您需要在 PHP 中进行正确检查以确保一切正常。

    试试这个:

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            die("socket_create() failed: Reason: " . socket_strerror(socket_last_error()) . "\n\n");
            return false;
        }
    
        echo("Attempting to connect to '{$IP}' on port '{$PORT}'");
        $result = socket_connect($socket, $IP, $PORT);
        if ($result === false) {
            echo("socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n\n");
            return false;
        } else {
            echo "OK.\n\n";
        }
    

    【讨论】:

    • 感谢您的快速回复。我尝试了 Neal 的代码,我得到:尝试在端口 '25555' 上连接到 '83.24.47.45' 确定。即使服务器关闭与否...
    • @user1023753 确保该端口不是某处的共享或开放端口
    猜你喜欢
    • 2012-04-05
    • 2015-04-06
    • 2011-05-31
    • 1970-01-01
    • 2020-04-29
    • 2011-02-05
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多