【问题标题】:SIP client doesn't accept the responseSIP 客户端不接受响应
【发布时间】:2015-11-21 13:11:10
【问题描述】:

我正在尝试制作简单的 SIP 客户端(UDP、BSD 套接字),但我遇到了 REGISTER 消息。我设法将 REGISTER 消息发送到星号,然后他会响应,但客户端没有收到响应,所以他永远等待。

#include <stdio.h> //printf
#include <string.h> //memset
#include <stdlib.h> //exit(0);
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>

#define SERVER "127.0.0.1"
#define BUFLEN 1024  //Max length of buffer
#define PORT 5060   //The port on which to send data


int main(int argc, char **argv)
{
  struct sockaddr_in si_other;
  int s,slen=sizeof(si_other);
  char buf[BUFLEN];
  char message[]= "REGISTER sip:127.0.0.1 SIP/2.0\r\nVia: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bKnashds7\r\nFrom: <sip:bob@127.0.0.1>;tag=as58f4201b\r\nCall-ID: 843817637684230@hefrst\r\nTo: <sip:bob@127.0.0.1>\r\nCSeq: 1 REGISTER\r\nContact: <sip:bob@127.10.10.1>\r\nAllow: INVITE,ACK,OPTIONS,BYE,CANCEL,SUBSCRIBE,NOTIFY,REFER,MESSAGE,INFO,PING\r\nExpires: 3600\r\n\r\n";  

  if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
   fprintf(stderr, "socket failed");
   exit(1);
}

  memset((char *) &si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);

 if (inet_aton(SERVER , &si_other.sin_addr) == 0) 
{
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
}
while(1){
 if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
    {
        fprintf(stderr, "sendto failed");
        exit(1);
    }
memset(buf,'\0', BUFLEN);
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
    {
        fprintf(stderr, "recvfrom() failed");
        exit(1);
    }
  }
}

这只是似乎无法接收数据的部分代码。

【问题讨论】:

  • 我不知道 SIP,但是 1. 您不希望在听到(接收)之前发送消息吗? 2、使用\n,而不是\r\n, in the middle of the message`对吗?
  • 是的,我在接收之前发送消息,我只是没有将它包含在示例中。 2,我必须在消息中的每个标题之后添加换行符。
  • s 结果在对 recvfrom 的调用中未初始化。如果有更多代码,请发布。
  • 使用\n而不是\r\n作为行的分隔符是错误的。引用SIP: Session Initiation Protocol 7 SIP 消息(第 26 页):“起始行、每个消息标题行和空行必须由回车换行序列 (CRLF) 终止。”
  • @terencehill 更新问题

标签: c client sip


【解决方案1】:

您忘记绑定到套接字:

   if( bind(s , (struct sockaddr*)&si_other, sizeof(si_other) ) == -1)
    {
        fprintf(stderr, "bind failed\n");
        exit(1);
    }

【讨论】:

  • 是UDP客户端,不需要绑定。
  • 我已经尝试了绑定和工作的代码。另外:使用 UDP,您必须在客户端绑定()套接字,因为 UDP 是无连接的,因此堆栈没有其他方法可以知道将数据报传递到特定端口的哪个程序。见 (stackoverflow.com/questions/3057029/…)
【解决方案2】:

问题出在邮件标题中。 Via 部分必须以;rport 结尾,因此星号确实会报告。

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 2019-12-03
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2012-04-20
    相关资源
    最近更新 更多