资源简介
Linux下局域网内IP对讲程序,
执行gcc -o sound_send sound.c -lasound
gcc -o sound_recv sound.recv.c -lasound编译程序。
./sound_recv 4600(端口号)运行接收程序
./sound_send 192.168.1.1(IP地址) 4600(端口号)运行发送程序。
使用ALSA驱动 C语言编写
参数可自己设置重新编译。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc char* argv[]) {
int sin_len;
int socket_descriptor;
int port;
struct sockaddr_in sin;
if (argc != 2){
printf(“Usage: sound_recv PORT \n“);
exit(1);
}
port = atoi(argv[1]);
int rc;
int dir;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
unsigned int val;
char *buffer;
printf(“Waiting for data form sender \n“);
bzero(&sinsizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(port);
sin_len=sizeof(sin);
socket_descriptor=socket(AF_INETSOCK_DGRAM0);
bind(socket_descriptor(struct sockaddr *)&sinsizeof(sin));
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle “default“
SND_PCM_STREAM_PLAYBACK 0);
if (rc < 0) {
fprintf(stderr
“unable to open pcm device: %s/n“
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle params
SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle params
SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle params 1);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
snd_pcm_hw_params_set_rate_near(handle params
&val &dir);
/* Set period size to 16 frames. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle
params &frames &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle params);
if (rc < 0) {
fprintf(stderr
“unable to set hw parameters: %s/n“
snd_strerror(rc));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params &frames
&dir);
size = frames * 2; /* 2 bytes/sample 1 channels */
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params
&val &dir);
while(1)
{
rc = recvfrom(socket_descriptorbuffersize0(struct sockaddr *)&sin&sin_len);
fprintf(stderr
“ %s/n“
snd_strerror(rc));
rc = snd_pcm_writei(handle buffer sizeof(buffer));
}
close(socket_descriptor);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
相关资源
- libnet发送udp和tcp包
- Linux c语言 学生成绩管理系统
- Linux开发工具手册
- MFC UDP编程
- Linux操作系统下C语言编程从零开始
- 基于Linux下C语言开发的员工管理系统
- 超级玛丽c++源码win32Linux平台
- UNIX/LINUX下C语言中文短信UCS2编码和解
- 嵌入式工程师必知必会 (完整高清中
- linux-2.6.24.rar
- 使用socket套接字发送udp数据包
- Linux下C语言操作静态ARP表,包括增加
- c语言 linux 贪吃蛇.doc
- Linux多线程服务端编程:使用muduo C+
- C语言实现TCP/IP协议通信和UDP协议通信
- libstdc++.so.6.0.23_linux7
- libstdc++-devel-4.1.2-48.el5.x86_64
- C++版仿Linux文件管理系统
- c语言+UDP+DES加密+socket编程
- 基于SDL的贪吃蛇游戏
- 基于udp的socket编程
- RakNet 基于UDP网络传输协议的C++网络库
- 基于UDP的简单可视化界面聊天程序c
- 链表栈的基本操作(C语言
- 基于linux C/C++和Qt的聊天程序
- 基于VC++6.0的UDP通讯
- Linux+gladeGTK++C语言+mysql的模仿QQ聊天工
- 自己编写的C++基于socket编程的UDP聊天
- VC++UDP网络聊天程序
- 嵌入式linuxC语言程序设计基础教程
评论
共有 条评论