0

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

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

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

谢谢, 阿努帕姆

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

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 ) ;

但它没有用。

4

2 回答 2

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]));

    }
}
于 2013-11-20T12:33:12.793 回答
0

要按本书执行此操作(符合RFC 3261),您将需要创建相当多的机器来处理重传等,或者使用JAIN-SIP 之类的库。

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

OPTIONS这是来自 RFC的示例消息:

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
于 2013-08-28T14:15:40.400 回答