Server.java
上传用户:pdtewte
上传日期:2021-10-16
资源大小:13k
文件大小:4k
源码类别:

IP电话/视频会议

开发平台:

Java

  1. import java.net.*;
  2. public class Server 
  3. {
  4. private int port;
  5. private String username;
  6. private DatagramSocket ListenSocket;  //UDP 
  7. private InetAddress localAddr;
  8. private String destMediaIP;
  9. private int destMediaPort;
  10. public Server( String username, int port)
  11. {
  12. this.port = port;
  13. try
  14. {
  15. localAddr = InetAddress.getLocalHost(); //localhost
  16. ListenSocket = new DatagramSocket(this.port); //create udp 
  17. }
  18. catch(Exception X)
  19. {
  20. }
  21.         /* 循环处理客户请求 */
  22. while (true)
  23. {
  24. byte[] buf = new byte[1024];
  25. DatagramPacket packet = new DatagramPacket(buf, buf.length); /* receive packet*/
  26. try 
  27. {
  28. ListenSocket.receive(packet);
  29. if (packet.getLength() < 10) /*length of udp header is 8 ,ip header is 20*/
  30. {
  31. continue;
  32. }
  33. }
  34. catch(Exception X)
  35. {
  36. }
  37. Decode decodeMessage = new Decode(new String(packet.getData()));
  38. //System.out.println("sMediaIP:"+decodeMessage.sMediaIP+"sMediaPort"+decodeMessage.sMediaPort);
  39. /* 对ACK报文的处理函数 */
  40. if (decodeMessage.sPacketType.equalsIgnoreCase("ACK"))
  41. {
  42. System.out.println("呼叫建立");
  43. //Integer i = new Integer(decodeMessage.sMediaPort);
  44. RTPVoiceSender rtpVoiceSender = new RTPVoiceSender(this.destMediaIP,this.destMediaPort);
  45. }
  46. /* 对INVITE报文的处理函数 */
  47.     if (decodeMessage.sPacketType.equalsIgnoreCase("INVITE"))
  48.     {
  49. //System.out.println(decodeMessage.sContactUsername + "(" + decodeMessage.sContactIP + ":" + decodeMessage.sContactPort+")呼入......");
  50.      this.destMediaIP = decodeMessage.sMediaIP;
  51.      Integer i = new Integer(decodeMessage.sMediaPort);
  52.      this.destMediaPort = i.intValue();
  53.     
  54.      String SDP = "v=0rn"
  55. + "o=- 3403572143 3403572143 IN IP4 " + localAddr.getHostAddress() + "rn"
  56.                  + "s=SIPPER for 3CX Phonern"
  57. + "c=IN IP4 " + localAddr.getHostAddress() + "rn"
  58.                  + "t=0 0rn"
  59.                  + "m=audio 5033 RTP/AVP 8 0 2 3 97 110 101rn"
  60.                  + "a=rtpmap:8 PCMA/8000rn"
  61.                  + "a=rtpmap:0 PCMU/8000rn"
  62.                  + "a=rtpmap:2 G726-32/8000rn"
  63.                  + "a=rtpmap:3 GSM/8000rn"
  64.                  + "a=rtpmap:97 iLBC/8000rn"
  65.                  + "a=rtpmap:110 speex/8000rn"
  66.                  + "a=rtpmap:101 telephone-event/8000rn";
  67.     
  68.      String OK = "SIP/2.0 200 OKrn"
  69. + "Via: SIP/2.0/UDP " + decodeMessage.sVia + " ;" + "branch=" + decodeMessage.sBranch + "rn"
  70. + "From:" + decodeMessage.sFROM + "rn"
  71. + "To: " + decodeMessage.sTO + "rn"
  72. + "Call-ID: " + decodeMessage.sCallID + "rn"
  73. + "CSeq: " + decodeMessage.sCSeq + "rn"
  74. + "Contact: <sip:" +this.username+"@"+localAddr.getHostAddress()+":"+this.port + ">rn"
  75. + "Max-Forwards: 70 rn"
  76. + "Server: SIPPER for 3CX Phonern"
  77. + "Allow: INVITE,ACK, BYE, CANCELrn"
  78. + "Content-Type: application/sdprn"
  79. + "Content-Length: "+SDP.length()+"rnrn";
  80.     
  81.         String pdu = OK + SDP;
  82.          
  83.          //System.out.println(pdu);
  84. byte[] buffer = new byte[1024];
  85. buffer = pdu.getBytes();
  86. DatagramPacket packet2 = new DatagramPacket(buffer, buffer.length, packet.getAddress(), packet.getPort());
  87. try 
  88. {
  89. ListenSocket.send(packet2);
  90.     System.out.println("接受呼叫......");
  91. }
  92. catch(Exception X)
  93. {
  94. }
  95.     }
  96.     
  97. }
  98. }
  99. public static void main(String[] args) 
  100. {
  101. Server main = new Server("127.0.0.1",5060);
  102. }
  103. }