個人想做一個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