Java编写SIP协议

avatar
作者
猴君
阅读量:0

1、编写Server代码

package com.genersoft.iot.vmp.sip; import javax.sip.*; import javax.sip.message.*; import javax.sip.header.*; import java.util.*;  public class SimpleSipServer implements SipListener {     private SipFactory sipFactory;     private SipStack sipStack;     private SipProvider sipProvider;     private MessageFactory messageFactory;     private  String ip = "127.0.0.1";      public SimpleSipServer() {         try {             sipFactory = SipFactory.getInstance();             sipFactory.setPathName("gov.nist");              Properties properties = new Properties();             properties.setProperty("javax.sip.STACK_NAME", "SimpleSipServer");             properties.setProperty("javax.sip.IP_ADDRESS", ip);             properties.setProperty("javax.sip.RETRANSMISSION_FILTER", "on");              sipStack = sipFactory.createSipStack(properties);             ListeningPoint listeningPoint = sipStack.createListeningPoint(ip, 5060, "udp");             sipProvider = sipStack.createSipProvider(listeningPoint);             sipProvider.addSipListener(this);              messageFactory = sipFactory.createMessageFactory();         } catch (Exception e) {             e.printStackTrace();         }     }      @Override     public void processRequest(RequestEvent requestEvent) {         Request request = requestEvent.getRequest();         System.out.println("Received Request: " + request.getMethod());          if (request.getMethod().equals(Request.INVITE)) {             try {                 Response response = messageFactory.createResponse(Response.OK, request);                 sipProvider.sendResponse(response);                 System.out.println("Sent 200 OK response to INVITE");             } catch (Exception e) {                 e.printStackTrace();             }         }     }      @Override     public void processResponse(ResponseEvent responseEvent) {}      @Override     public void processTimeout(TimeoutEvent timeoutEvent) {         System.out.println("Transaction Timed Out");     }      @Override     public void processIOException(IOExceptionEvent exceptionEvent) {         System.out.println("IO Exception");     }      @Override     public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {         System.out.println("Transaction Terminated");     }      @Override     public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {         System.out.println("Dialog Terminated");     }      public static void main(String[] args) {         new SimpleSipServer();     } }  

2、编写Client端代码

package com.genersoft.iot.vmp.sip; import javax.sip.*; import javax.sip.address.*; import javax.sip.message.*; import javax.sip.header.*; import java.util.*; public class SimpleSipClient implements SipListener {     private SipFactory sipFactory;     private SipStack sipStack;     private SipProvider sipProvider;     private AddressFactory addressFactory;     private MessageFactory messageFactory;     private HeaderFactory headerFactory;       private  String ip = "127.0.0.1";      public SimpleSipClient() {         try {             sipFactory = SipFactory.getInstance();             sipFactory.setPathName("gov.nist");              Properties properties = new Properties();             properties.setProperty("javax.sip.STACK_NAME", "SimpleSipClient");             properties.setProperty("javax.sip.OUTBOUND_PROXY", ip+":5060/udp");             properties.setProperty("javax.sip.IP_ADDRESS", ip);              sipStack = sipFactory.createSipStack(properties);             ListeningPoint listeningPoint = sipStack.createListeningPoint(ip, 5070, "udp");             sipProvider = sipStack.createSipProvider(listeningPoint);             sipProvider.addSipListener(this);              addressFactory = sipFactory.createAddressFactory();             messageFactory = sipFactory.createMessageFactory();             headerFactory = sipFactory.createHeaderFactory();         } catch (Exception e) {             e.printStackTrace();         }     }      public void sendInvite() {         try {             SipURI fromUri = addressFactory.createSipURI("client", ip);             Address fromAddress = addressFactory.createAddress(fromUri);             fromAddress.setDisplayName("Client");              SipURI toUri = addressFactory.createSipURI("server", ip);             Address toAddress = addressFactory.createAddress(toUri);             toAddress.setDisplayName("Server");              SipURI requestUri = addressFactory.createSipURI("server", ip+":5060");              FromHeader fromHeader = headerFactory.createFromHeader(fromAddress, "client123");             ToHeader toHeader = headerFactory.createToHeader(toAddress, null);             CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);             CallIdHeader callIdHeader = sipProvider.getNewCallId();             MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);             ViaHeader viaHeader = headerFactory.createViaHeader(ip, 5070, "udp", null);             ContactHeader contactHeader = headerFactory.createContactHeader(fromAddress);              ArrayList<ViaHeader> viaHeaders = new ArrayList<>();             viaHeaders.add(viaHeader);              Request request = messageFactory.createRequest(requestUri, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader);             request.addHeader(contactHeader);              ClientTransaction transaction = sipProvider.getNewClientTransaction(request);             transaction.sendRequest();             System.out.println("Sent INVITE request");         } catch (Exception e) {             e.printStackTrace();         }     }      @Override     public void processRequest(RequestEvent requestEvent) {}      @Override     public void processResponse(ResponseEvent responseEvent) {         Response response = responseEvent.getResponse();         System.out.println("Received Response: " + response.getStatusCode());     }      @Override     public void processTimeout(TimeoutEvent timeoutEvent) {         System.out.println("Transaction Timed Out");     }      @Override     public void processIOException(IOExceptionEvent exceptionEvent) {         System.out.println("IO Exception");     }      @Override     public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {         System.out.println("Transaction Terminated");     }      @Override     public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {         System.out.println("Dialog Terminated");     }      public static void main(String[] args) {         SimpleSipClient client = new SimpleSipClient();         client.sendInvite();     } }  

3、使用wireshark抓包,看实际效果
在这里插入图片描述

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!