gpt4 book ai didi

c - SIP 客户端不接受响应

转载 作者:行者123 更新时间:2023-11-30 17:08:04 26 4
gpt4 key购买 nike

我正在尝试制作简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com