c - SIP 客户端不接受响应

标签 c client sip

我正在尝试制作简单的 SIP 客户端(UDP、BSD 套接字),但我被 REGISTER 消息困住了。我设法将 REGISTER 消息发送给 asterisk,然后他会做出响应,但客户端没有收到响应,因此他永远等待。

#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);
    }
  }
}

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

最佳答案

您忘记绑定(bind)到套接字:

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

关于c - SIP 客户端不接受响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843738/

相关文章:

android - Programmaitcally 在 Android 平板电脑上使用 Zoiper 进行 VOIP 调用

c - Json解析数据并通过提供key获取值

c++ - 以下语句中逗号的作用是什么?

c - 读取纺织品并添加到 C 中的链接列表

c - C语言,TCP服务器和客户端

google-chrome - 自由切换 WebRTC 到 SIP

c - 尝试写入使用 cudaMalloc3D 分配的二维数组时出现“非法内存访问”

java - Apache HTTP 客户端 socks 代理

java - 使用 JDBC 的简单客户端中间件模拟

web-applications - 从 Web 应用程序打开 Microsoft Teams 聊天