Server.java
资源名称:SIPPhone.zip [点击查看]
上传用户:pdtewte
上传日期:2021-10-16
资源大小:13k
文件大小:4k
源码类别:
IP电话/视频会议
开发平台:
Java
- import java.net.*;
- public class Server
- {
- private int port;
- private String username;
- private DatagramSocket ListenSocket; //UDP
- private InetAddress localAddr;
- private String destMediaIP;
- private int destMediaPort;
- public Server( String username, int port)
- {
- this.port = port;
- try
- {
- localAddr = InetAddress.getLocalHost(); //localhost
- ListenSocket = new DatagramSocket(this.port); //create udp
- }
- catch(Exception X)
- {
- }
- /* 循环处理客户请求 */
- while (true)
- {
- byte[] buf = new byte[1024];
- DatagramPacket packet = new DatagramPacket(buf, buf.length); /* receive packet*/
- try
- {
- ListenSocket.receive(packet);
- if (packet.getLength() < 10) /*length of udp header is 8 ,ip header is 20*/
- {
- continue;
- }
- }
- catch(Exception X)
- {
- }
- Decode decodeMessage = new Decode(new String(packet.getData()));
- //System.out.println("sMediaIP:"+decodeMessage.sMediaIP+"sMediaPort"+decodeMessage.sMediaPort);
- /* 对ACK报文的处理函数 */
- if (decodeMessage.sPacketType.equalsIgnoreCase("ACK"))
- {
- System.out.println("呼叫建立");
- //Integer i = new Integer(decodeMessage.sMediaPort);
- RTPVoiceSender rtpVoiceSender = new RTPVoiceSender(this.destMediaIP,this.destMediaPort);
- }
- /* 对INVITE报文的处理函数 */
- if (decodeMessage.sPacketType.equalsIgnoreCase("INVITE"))
- {
- //System.out.println(decodeMessage.sContactUsername + "(" + decodeMessage.sContactIP + ":" + decodeMessage.sContactPort+")呼入......");
- this.destMediaIP = decodeMessage.sMediaIP;
- Integer i = new Integer(decodeMessage.sMediaPort);
- this.destMediaPort = i.intValue();
- String SDP = "v=0rn"
- + "o=- 3403572143 3403572143 IN IP4 " + localAddr.getHostAddress() + "rn"
- + "s=SIPPER for 3CX Phonern"
- + "c=IN IP4 " + localAddr.getHostAddress() + "rn"
- + "t=0 0rn"
- + "m=audio 5033 RTP/AVP 8 0 2 3 97 110 101rn"
- + "a=rtpmap:8 PCMA/8000rn"
- + "a=rtpmap:0 PCMU/8000rn"
- + "a=rtpmap:2 G726-32/8000rn"
- + "a=rtpmap:3 GSM/8000rn"
- + "a=rtpmap:97 iLBC/8000rn"
- + "a=rtpmap:110 speex/8000rn"
- + "a=rtpmap:101 telephone-event/8000rn";
- String OK = "SIP/2.0 200 OKrn"
- + "Via: SIP/2.0/UDP " + decodeMessage.sVia + " ;" + "branch=" + decodeMessage.sBranch + "rn"
- + "From:" + decodeMessage.sFROM + "rn"
- + "To: " + decodeMessage.sTO + "rn"
- + "Call-ID: " + decodeMessage.sCallID + "rn"
- + "CSeq: " + decodeMessage.sCSeq + "rn"
- + "Contact: <sip:" +this.username+"@"+localAddr.getHostAddress()+":"+this.port + ">rn"
- + "Max-Forwards: 70 rn"
- + "Server: SIPPER for 3CX Phonern"
- + "Allow: INVITE,ACK, BYE, CANCELrn"
- + "Content-Type: application/sdprn"
- + "Content-Length: "+SDP.length()+"rnrn";
- String pdu = OK + SDP;
- //System.out.println(pdu);
- byte[] buffer = new byte[1024];
- buffer = pdu.getBytes();
- DatagramPacket packet2 = new DatagramPacket(buffer, buffer.length, packet.getAddress(), packet.getPort());
- try
- {
- ListenSocket.send(packet2);
- System.out.println("接受呼叫......");
- }
- catch(Exception X)
- {
- }
- }
- }
- }
- public static void main(String[] args)
- {
- Server main = new Server("127.0.0.1",5060);
- }
- }