向SIP UDP服务器发送选项命令

时间:2013-08-28 12:13:10

标签: java udp sip datagram udpclient

我正在开发一个程序,需要确定是否可以从客户端计算机访问远程SIP UDP端口5060.

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

任何帮助/方向都会有很大的帮助!

谢谢, Anupam

感谢您的回复,我尝试了下面的代码,但没有收到服务器的任何回复:

String message = "OPTIONS sip:opensips@host;transport=udp SIP/2.0\r\nCall-ID: 7df5e96c6b1b98af25ad6c7845d48f5d@49.249.132.30\r\nCSeq: 1 OPTIONS\r\nFrom: \"Anupam\" <sip:Anupam@localhost:5080>;tag=textclientv1.0\r\nTo: \"opensips\" <sip:opensips@host>\r\nVia: SIP/2.0/UDP 49.249.132.30:5080;branch=z9hG4bK-3938-f66aaa8dda2fe3b863b4acde5fbcab67\r\nMax-Forwards: 70\r\nContact: \"Anupam\" <sip:Anupam@localhost: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 ) ;

但它不起作用。

2 个答案:

答案 0 :(得分:0)

要通过本书(符合RFC 3261)执行此操作,您将需要创建相当多的机制来处理重新传输等,或者使用像JAIN-SIP这样的库。< / p>

但是,您可以通过简单地打开UDP套接字,在套接字上发送包含格式正确的OPTIONS消息的字符串,然后等待一段时间来查看是否能够获得大部分路径在该套接字上获得SIP响应。任何旧的SIP响应(成功或错误)都将验证服务器是否可访问。

以下是来自RFC的示例OPTIONS消息:

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

答案 1 :(得分: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:alice@" + 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]));

    }
}