2013-08-28 50 views
0

我正在研究一个程序,该程序需要确定是否可以从客户端计算机访问远程SIP UDP端口5060。将选项命令发送到SIP UDP服务器

由于没有直接的方法来检查UDP端口的可用性。我想创建一个简单的java类,它将发送OPTIONS消息到SIP UDP服务器,然后服务器将回复到客户端在Java中。

任何帮助/方向将是很大的帮助!

感谢, 阿努邦

感谢您的回复,我想下面的代码,但它并没有从服务器获取任何回复:

String message = "OPTIONS sip:[email protected];transport=udp SIP/2.0\r\nCall-ID: [email protected]\r\nCSeq: 1 OPTIONS\r\nFrom: \"Anupam\" <sip:[email protected]:5080>;tag=textclientv1.0\r\nTo: \"opensips\" <sip:[email protected]>\r\nVia: SIP/2.0/UDP 49.249.132.30:5080;branch=z9hG4bK-3938-f66aaa8dda2fe3b863b4acde5fbcab67\r\nMax-Forwards: 70\r\nContact: \"Anupam\" <sip:[email protected]:5080>\r\nContent-Length: 0\r\n\r\n"; 


System.out.println("Message is "+ message); 
byte [] data = message.getBytes(); 
DatagramPacket packet = new DatagramPacket(data, data.length, host, port) ; 

但没有奏效。

回答

0

经书(符合RFC 3261)做到这一点,你要么需要建立相当多的机制,可以处理重传等,或使用一个库像JAIN-SIP

但是,通过简单地打开一个UDP套接字,通过套接字发送一个包含适当格式的OPTIONS消息的String,然后等待一段时间以查看是否获得SIP在该套接字上的响应。任何旧的SIP响应(成功或错误)将验证服务器是否可达。

下面是一个例子OPTIONS消息从RFC:

OPTIONS sip:[email protected] SIP/2.0 
Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKhjhs8ass877 
Max-Forwards: 70 
To: <sip:[email protected]> 
From: Alice <sip:[email protected]>;tag=1928301774 
Call-ID: a84b4c76e66710 
CSeq: 63104 OPTIONS 
Contact: <sip:[email protected]> 
Accept: application/sdp 
Content-Length: 0 
+0

感谢您的回复,我想下面的代码,但它并没有从服务器获取任何回复: – user2725290

+0

我不知道你指的是什么代码。您是否有权访问服务器以查看它是否收到了该消息?如果没有,您可以设置另一个SIP UA并将其用于测试您的代码。 (它应该回复'OPTIONS'消息。) – yotommy

+0

我无法回答,因为我是这个门户的新手。我编辑了主要问题。 – user2725290

0

为了使用REGISTER方法可以使用下面的代码来检查远程侧UDP SIP服务的可用性。

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import java.util.Random; 

public class CheckSipUdp{ 
    //Check remote SIP service availability 
    public void checkSipUdp(String ipAddress, int outPort)throws Exception{ 
     DatagramSocket sipSocket = new DatagramSocket(0); 
     sipSocket.setSoTimeout(1000); 
     InetAddress inetIpAddress = InetAddress.getByName(ipAddress); 
     byte [] sendData = new byte[1024]; 
     byte [] receiveData = new byte[1024]; 

     //Message/Method which will be used for checking remote server availability. 
     String method = "REGISTER sip:" + ipAddress + ":" + outPort + " SIP/2.0\r\nCall-ID: " + generateCallId() + "@" + InetAddress.getLocalHost().getHostAddress() +"\r\nCSeq: 1 REGISTER\r\nFrom: <sip:" + InetAddress.getLocalHost().getHostAddress() + ":" + sipSocket.getLocalPort() + ">;tag=" + new Random().nextInt() + "\r\nTo: <sip:[email protected]" + ipAddress + ":" + outPort + ">\r\nVia: SIP/2.0/UDP " + InetAddress.getLocalHost().getHostAddress() + ":" + sipSocket.getLocalPort() + ";branch=z9hG4bK-323032-" + generateCallId() + "\r\nMax-Forwards: 70\r\nContact: <sip:" + InetAddress.getLocalHost().getHostAddress()+ ":" + sipSocket.getLocalPort() + ">\r\nContent-Length: 0\r\n\r\n"; 
     sendData = method.getBytes(); 

     DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, inetIpAddress, 5060); 
     sipSocket.send(sendPacket); 

     DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
     sipSocket.receive(receivePacket); 

     String response = new String(receivePacket.getData()); 
     System.out.println(ipAddress + "\n" + response); 
     sipSocket.close(); 
    } 

    //Generating unique callID 
    public static String generateCallId(){ 
     Random r = new Random(); 
     long l1 = r.nextLong() * r.nextLong(); 
     long l2 = r.nextLong() * r.nextLong(); 
     return Long.toHexString(l1) + Long.toHexString(l2); 

    } 

    public static void main(String [] args) throws Exception{ 
     CheckSipUdp sip = new CheckSipUdp(); 
     sip.checkSipUdp(args[0], Integer.parseInt(args[1])); 

    } 
}