个人想做一个Linux下的IP phone,找了一些SIP资料,发现这协议够大,看osip项目都做了一两年,我也觉得难度太高,但想想难度高才有挑战嘛,自我安慰,呵呵. 第一步我想开始熟悉linux下的进程通讯, 预想有socket, pipe,queue,sigaction等要先掌握. 先不做服务器, 以两个客户程序User Agent通信为主. 划分几个模块: 1.Socket处理模块 2.信令解析模块 3.命令处理 4.UI 5.维护模块 6.其它模块 1与2之间采用queue通讯.socket以UDP为基础. 写了两个udp通讯的客户与服务程序: 服务程序: bigdogsrv.c *********************************************** #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100
main() { int sockfd; struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int addr_len, numbytes; char buf[MAXBUFLEN]; // memset(buf,"",MAXBUFLEN-1); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(MYPORT); /* short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \ == -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); while(strncmp(buf,"bye",3)!=0){//接到bye 就结束 if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \ (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("got packet from %s\n",inet_ntoa(their_addr. sin_addr)); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf); }
close(sockfd ) ; } ********************************
客户端: bigdogclient.c *************************************** #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <sys/socket.h> #include <sys/wait.h> #define MYPORT 4950 /* the port users will be sending to */
int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in their_addr; /* connector's address information */ struct hostent *he; int numbytes; if (argc != 3) { fprintf(stderr,"usage: talker hostname message\n"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */ herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; /* host byte order */ their_addr.sin_port = htons(MYPORT); /* short, network byte order */ their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \ (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { perror("sendto") ; exit(1); } printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr)); close(sockfd);
return 0; } ************************************
makefile:写的简单了 ************************** CC=gcc
srv:bigdogsrv.o $(CC) -o srv bigdogsrv.o bigdogsrv.o:bigdogsrv.c $(CC) -c -g bigdogsrv.c
client:bigdogclient.o $(CC) -o client bigdogclient.o bigdogclient.o:bigdogclient.c $(CC) -c -g bigdogclient.c
clean: rm -f *.o rm -f srv rm -f client
************************************** make make client 然后先运行srv, 再运行client 192.168.1.168 love you

|