您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > JAVA实现SIP一个例子
java实现SIP一个例子(如果你贴子有附件,请一定按照下面模板发贴,否则贴子会被立刻删除,谢谢合作!)【资料名称】:【作者】:【语言】:【页数】:【格式】:【发表时间】:【摘要或目录】:(请根据需要,保留上面的相应选项)packagecom.voip.sipphone;importgov.nist.javax.sip.address.SipUri;importgov.nist.javax.sip.header.CSeq;importgov.nist.javax.sip.header.Contact;importgov.nist.javax.sip.header.ContentLength;importgov.nist.javax.sip.header.ContentType;importgov.nist.javax.sip.header.From;importgov.nist.javax.sip.header.Via;importjava.text.ParseException;importjava.util.ArrayList;importjava.util.Hashtable;importjava.util.List;importjava.util.Properties;importjava.util.Timer;importjavax.sip.*;importjavax.sip.address.Address;importjavax.sip.address.AddressFactory;importjavax.sip.address.URI;importjavax.sip.header.CallIdHeader;importjavax.sip.header.ContactHeader;importjavax.sip.header.ExpiresHeader;importjavax.sip.header.FromHeader;importjavax.sip.header.Header;importjavax.sip.header.HeaderFactory;importjavax.sip.header.MaxForwardsHeader;importjavax.sip.header.RecordRouteHeader;importjavax.sip.header.ToHeader;importjavax.sip.header.ViaHeader;importjavax.sip.message.MessageFactory;importjavax.sip.message.Request;importjavax.sip.message.Response;publicclassSipPhoneimplementsSipListener{publicvoidprocessDialogTerminated(DialogTerminatedEventarg0){//TODOAuto-generatedmethodstubSystem.out.println(processDialogTerminated+arg0.toString());}publicvoidprocessIOException(IOExceptionEventarg0){//TODOAuto-generatedmethodstubSystem.out.println(processIOException+arg0.toString());}/***保存当前注册的用户*/privatestaticHashtableURI,URIcurrUser=newHashtable();/***@authorsoftware*注册定时器*/classTimerTaskextendsTimer{/***defaultconstructor*/publicTimerTask(){}/***如果定时任务到,则删除该用户的注册信息*/publicvoidrun(){}}/***服务器侦听IP地址*/privateStringipAddr=192.168.0.20;/***服务器侦听端口*/privateintport=5060;/***处理register请求*@paramrequest请求消息*/privatevoidprocessRegister(Requestrequest,RequestEventrequestEvent){if(null==request){System.out.println(processInviterequestisnull.);return;}//System.out.println(Request+request.toString());ServerTransactionserverTransactionId=requestEvent.getServerTransaction();try{Responseresponse=null;ToHeaderhead=(ToHeader)request.getHeader(ToHeader.NAME);AddresstoAddress=head.getAddress();URItoURI=toAddress.getURI();ContactHeadercontactHeader=(ContactHeader)request.getHeader(Contact);AddresscontactAddr=contactHeader.getAddress();URIcontactURI=contactAddr.getURI();System.out.println(processRegisterfrom:+toURI+requeststr:+contactURI);intexpires=request.getExpires().getExpires();//如果expires不等于0,则为注册,否则为注销。if(expires!=0||contactHeader.getExpires()!=0){currUser.put(toURI,contactURI);System.out.println(registeruser+toURI);}else{currUser.remove(toURI);System.out.println(unregisteruser+toURI);}response=msgFactory.createResponse(200,request);System.out.println(sendregisterresponse:+response.toString());if(serverTransactionId==null){serverTransactionId=sipProvider.getNewServerTransaction(request);serverTransactionId.sendResponse(response);//serverTransactionId.terminate();System.out.println(registerserverTransaction:+serverTransactionId);}else{System.out.println(processRequestserverTransactionIdisnull.);}}catch(ParseExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(SipExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InvalidArgumentExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}/***处理invite请求*@paramrequest请求消息*/privatevoidprocessInvite(Requestrequest,RequestEventrequestEvent){if(null==request){System.out.println(processInviterequestisnull.);return;}try{//发送100TryingserverTransactionId=requestEvent.getServerTransaction();if(serverTransactionId==null){serverTransactionId=sipProvider.getNewServerTransaction(request);callerDialog=serverTransactionId.getDialog();Responseresponse=msgFactory.createResponse(Response.TRYING,request);serverTransactionId.sendResponse(response);}//查询目标地址URIreqUri=request.getRequestURI();URIcontactURI=currUser.get(reqUri);System.out.println(processInviterqStr=+reqUri+cUDP,callerVia.getBranch()+sipphone);//FIXME需要测试是否能够通过设置VIA头域来修改VIA头域值cliReq.removeHeader(Via.NAME);cliReq.addHeader(via);//更新contact的地址ContactHeadercontactHeader=headerFactory.createContactHeader();Addressaddress=addressFactory.createAddress(sip:sipsoft@+ipAddr+:+port);contactHeader.setAddress(address);contactHeader.setExpires(3600);cliReq.setHeader(contactHeader);clientTransactionId=sipProvider.getNewClientTransaction(cliReq);clientTransactionId.sendRequest();System.out.println(processInviteclientTransactionId=+clientTransactionId.toString());System.out.println(sendinvitetocallee:+cliReq);}catch(TransactionUnavailableExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}catch(SipExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(ParseExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();}}/***处理SUBSCRIBE请求*@paramrequest请求消息*/privatevoidprocessSubscribe(Requestrequest){if(null==request){System.out.println(processSubscriberequestisnull.);return;}ServerTransactionserverTransac
本文标题:JAVA实现SIP一个例子
链接地址:https://www.777doc.com/doc-5928813 .html