请教在局域网传输语音的问题

karuo 2005-04-18 05:15:11
我现在在做一个VOIP网络电话,想先在局域网实现.我的语音处理已经做好了,在单机上从麦克风说可以从耳机上听到,但在局域网上运行时,用UDP收发数据有问题.下面是我的程序.请高手指点!
服务器:
#include<unistd.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<stdlib.h>
#include<linux/soundcard.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>


#define LENGTH 1
#define RATE 8000
#define SIZE 8
#define CHANNELS 1

unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/80];
int main(int argc,char**argv)
{
int fd,sd;
int arg,status;
int address_len;
struct sockaddr_in address;
sd=open("/dev/dsp",O_WRONLY);
if(sd<0){
perror("open write device failed");
exit(1);
}
/*arg=SIZE;
ioctl(sd,SOUND_PCM_WRITE_BITS,&arg);
arg=CHANNELS;
ioctl(sd,SOUND_PCM_WRITE_CHANNELS,&arg);
arg=RATE;
ioctl(sd,SOUND_PCM_WRITE_RATE,&arg);
*/
fd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
address.sin_addr.s_addr=htonl(INADDR_ANY);
address.sin_port=htons(1234);
address_len=sizeof(address);
bind(fd,(struct sockaddr*)&address,address_len);
int i=1024*240;
setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&i,sizeof(i));
while(1){
struct sockaddr_in client_address;
socklen_t len=sizeof(client_address);
int n,m;
char line[80]="server has received a packet\n";;
printf("waiting...");
fflush(stdout);
n=recvfrom(fd,buf,LENGTH*RATE*SIZE*CHANNELS/80,0,
(struct sockaddr*)&client_address,&len);
printf("server received %d",n);
write(sd,buf,sizeof(buf));
/*sendto(fd,line,strlen(line)+1,0,(struct sockaddr*)&client_address,len);*/
}
}
客户机
#include<unistd.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<stdlib.h>
#include<linux/soundcard.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define LENGTH 1
#define RATE 8000
#define SIZE 8
#define CHANNELS 1

unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/80];
int main(int argc,char**argv)
{
int fd,sd;
int arg,status;
int address_len;
struct sockaddr_in address;
/*char line[80]="client to server string!\n";*/
int n;
sd=open("/dev/dsp",O_RDONLY);
if(sd<0){
perror("open of /dev/dsp failed");
exit(1);
}
arg = SIZE; /* sample size */
status = ioctl(sd, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_BITS ioctl failed");
if (arg != SIZE)
perror("unable to set sample size");

arg = CHANNELS; /* mono or stereo */
status = ioctl(sd, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
if (arg != CHANNELS)
perror("unable to set number of channels");

arg = RATE; /* sampling rate */
status = ioctl(sd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");

fd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=htons(1234);
address_len=sizeof(address);
while(1){
char line[80];*/
printf("say somthing:\n");
status=read(sd,buf,sizeof(buf));
sendto(fd,buf,strlen(buf)+1,0,(struct sockaddr*)&address,sizeof(address));

}
}


...全文
74 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qfxx 2005-04-19
  • 打赏
  • 举报
回复
我试了一下
可以收到数据啊
address.sin_addr.s_addr=inet_addr("192.168.13.1");
//192.168.13.1是服务器的ip地址

23,222

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧