My FAQ,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 未整理篇
IPphone日志1
作者:yirui 时间:2003-02-11 11:09 出处:互联网 责编:MyFAQ
              摘要:IPphone日志1

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


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有