套接字,PHP,本地端口 [英] Sockets, PHP, local port

查看:121
本文介绍了套接字,PHP,本地端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用现有系统的套接字编写基于PHP的客户端.

I'm writing a PHP-based client using sockets for an existing system.

实现该协议基本上是可行的,但是下一步,我需要知道在其上创建套接字的本地端口.

Implementing the protocol is basically working, but for the next step I need to know the local port, on which the socket is created.

在服务器上,我有以下输出:Socket [addr =/Client-IP,port = 40546,localport = 1338]

On the server I have the following output: Socket[addr=/Client-IP,port=40546,localport=1338]

为使通信正常进行,我需要获取端口40546(或连接时的端口号).遗憾的是,由于各种原因,无法更改协议.

For communication to work, I need to get the port 40546 (or whatever it is when connecting). Sadly it is NOT possible to change the protocol for various reasons.

非常感谢

艾德里安

编辑(代码段):

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, 192.168.0.12, 1338);
// Need to know local socket port (e.g. 40546)
$socketport = ??????
$message = "somestring";
$message = $protocol->encode($message, $socketport);
socket_write($socket, $message, strlen($message));

基本上就是这个.在创建和连接套接字之后,我需要知道客户端的端口,这意味着无需手动设置它.

This is basically what it is. I need to know the port on the clientside, AFTER creating and connecting the socket, meaning there's no need to set it manually.

推荐答案

我在 php.net .我认为这很适合您.

I found out a function in php.net.I think this works for you.

socket_getsockname($socket, $ip, $port);
echo "ip: ".$ip;
echo "port: ".$port;

在这里$ip为您提供本地地址,而$port为您提供本地端口号.

Here $ip gives you the local addr and $port gives you a local port no.

这篇关于套接字,PHP,本地端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆