2013-08-28 37 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])); 

    } 
}