【发布时间】:2013-03-08 05:14:57
【问题描述】:
我要做的是实现一个 SIP 客户端,它监听 SIP 消息。好的,所以我在服务器 192.168.0.246 上运行了 SIP 客户端,并且 SIP 服务器在 192.168.2.40 上运行。现在看看在下面的屏幕截图中。
它是在192.168.0.246 上运行客户端代码的服务器的跟踪文件。如您所见,服务器使用 SIP/SDP 协议从192.168.2.40 接收消息,但是当在 192.168.0.246 上运行的客户端程序使用 UDP 协议将消息发送回 192.168.2.40 时,它显示为 UDP 协议,这是正确的。但没有响应在此之后从 192.168.2.40 开始。
所以我假设它与显示为UDP的协议有关。所以如果我有礼貌,我应该把它交给 SIP/SDP。
所以我的问题是如何将此 UDP 更改为 SIP/SDP。
这是我的 php 代码:
<?php
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "192.168.0.246" , 5060) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
function GetBranchValue($message)
{
$data = "";
if(preg_match('/branch=.*/i', $message, $output))
$data = explode("=",$output[0]);
if(sizeOf($data)>1)
return $data[1];
else
return "None";
}
function GetTag($message)
{
$data = "";
if(preg_match('/tag=.*/i',$message, $output))
$data = explode("=", $output[0]);
if(sizeOf($data)>1)
return $data[1];
else
return "None";
}
function GetCallId($message)
{
$data = "";
if(preg_match('/Call-ID:.*/i', $message, $output))
$data = explode(":",$output[0]);
if(sizeOf($data)>1)
return $data[1];
else
return "None";
}
function GetCSeq($message)
{
$data = "";
if(preg_match('/CSeq:.*/i', $message, $output))
{
$data = explode(":", $output[0]);
$data = explode(" ",$data[1]);
}
if(sizeOf($data[1])>0)
return $data[1];
else
return "None";
}
function CreateResponse($message)
{
$msg = "SIP/2.0 302 Moved temporarily
Via:SIP/2.0/UDP 192.168.2.40:5060;branch=".GetBranchValue($message)."
From: <sip:+12012030008@192.168.2.40:5060>;tag=".GetTag($message)."
To:<sip:+17066458407@192.168.0.246:5060;user=phone>;tag=883069368-1363286882583
Call-ID:".GetCallId($message)."
CSeq:".GetCSeq($message)." INVITE
Contact:<sip:+17066458407@192.168.0.246:5060;user=phone>;q=0.5,<sip:+17066458407@192.168.0.246:5060;user=phone>;q=0.25
Content-Length:0";
return $msg;
}
function Create300Response($message)
{
$msg = "SIP/2.0 300 Multiple Choices
Via: SIP/2.0/UDP 192.168.2.40:5060;branch=".GetBranchValue($message)."
From: <sip:+12012030008@192.168.2.40:5060>;tag=".GetTag($message).";isup-oli;isup-oli=00
To:<sip:+17066458407@192.168.0.246:5060;user=phone>;tag=123
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>
Contact: <sip: +17066458407@192.168.0.246:5060;dtg=16>";
return $msg;
}
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
file_put_contents("Log.txt","\n",FILE_APPEND);
file_put_contents("Log.txt","Received Response------\n",FILE_APPEND);
file_put_contents("Log.txt",$buf,FILE_APPEND);
$respMessage = Create300Response($buf);
//Send back the data to the client
socket_sendto($sock, "OK " . $respMessage , 100 , 0 , $remote_ip , $remote_port);
file_put_contents("Log.txt","\n",FILE_APPEND);
file_put_contents("Log.txt","\n",FILE_APPEND);
file_put_contents("Log.txt",$respMessage,FILE_APPEND);
}
socket_close($sock);
【问题讨论】: