#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LENGTH 1 /* 存储秒数 */
#define RATE 8000 /* 采样频率 */
#define SIZE 8 /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */
// 用于保存数字音频数据的内存缓冲区 8000bytes
//unsigned char sound_buf[1024];
#define sound_play_buf sound_buf
unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/400];
//unsigned char sound_play_buf[LENGTH*RATE*SIZE*CHANNELS/16];
int get_sound(void);
int main(int argc, char **argv)
{
struct sockaddr_in recv_addr,send_addr;
struct sockaddr_in client_addr;
int recv_sock,send_sock;
socklen_t addr_len;
int len,i;
int state, sound_fd;
//char buff[128],recvs[128];
char *buff;
switch(argc){
case 1:
argv[1]=0;
case 2:
argv[2]=0;
break;
default:
break;
};
/* 创建 socket , 关键在于这个 SOCK_DGRAM */
if ((recv_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(errno);
} else
printf("create socket.\n\r");
if ((send_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket_s");
exit(errno);
} else
printf("create s_socket.\n\r");
memset(&recv_addr, 0, sizeof(struct sockaddr_in));
/* 设置地址和端口信息 */
recv_addr.sin_family = AF_INET;
#if 1
recv_addr.sin_port = htons(7840);
recv_addr.sin_addr.s_addr = INADDR_ANY;
#else
if (argv[1])
recv_addr.sin_port = htons(atoi(argv[1]));
else
recv_addr.sin_port = htons(7838);
if (argv[2])
recv_addr.sin_addr.s_addr = inet_addr(argv[2]);
else
recv_addr.sin_addr.s_addr = INADDR_ANY;
#endif
/* 绑定地址和端口信息 */
if ((bind(recv_sock, (struct sockaddr *) &recv_addr, sizeof(recv_addr))) == -1) {
perror("bind_r");
exit(errno);
} else
printf("bind address to receive socket.\n\r");
//recv_addr.sin_port =ntohs();
memset(&send_addr, 0, sizeof(struct sockaddr_in));
send_addr.sin_port = htons(7850);
send_addr.sin_addr.s_addr = INADDR_ANY;
if ((bind(send_sock, (struct sockaddr *) &send_addr, sizeof(send_addr))) == -1) {
perror("bind_s");
exit(errno);
} else
printf("bind address to sending socket.\n\r");
/* 循环接收数据 */
//ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
//ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
sound_fd=get_sound();
while (1) {
//for(i=0;i<25;i++)
{
addr_len = sizeof(client_addr);
len = recvfrom(recv_sock, sound_buf, sizeof(sound_buf) - 1, 0, (struct sockaddr *) &client_addr, &addr_len);
if (len < 0) {
perror("recvfrom");
exit(errno);
}
printf("收到来自%s:%d的消息\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
//memcpy(&sound_play_buf[i*160],sound_buf,160);
buff="The server has played";
client_addr.sin_port = htons(7860);
printf("服务器发送到%s:%d的消息\n:%s\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port),buff);
sendto(send_sock,buff,strlen(buff),0,(struct sockaddr *) &client_addr, addr_len);
}
printf("Client said:\n");
//sound_fd=get_sound();
state = write(sound_fd, sound_play_buf, sizeof(sound_play_buf)); /* 回放 */
if (state != sizeof(sound_play_buf))
perror("wrote wrong number of bytes");
#if 0
/* 在继续录音前等待回放结束 */
state = ioctl(sound_fd, SOUND_PCM_SYNC, 0);
if (state == -1)
perror("SOUND_PCM_SYNC ioctl failed");
//close(sound_fd);
#endif
}
close(recv_sock);
close(send_sock);
return 0;
}
int get_sound(void)
{
int fd; /* 声音设备的文件描述符 */
int arg; /* 用于ioctl调用的参数 */
int status; /* 系统调用的返回值 */
/* 打开声音设备 */
fd = open("/dev/dsp", O_RDWR);
if (fd < 0) {
perror("open of /dev/dsp failed");
exit(1);
}
/* 设置采样时的量化位数 */
arg = SIZE;
status = ioctl(fd, 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;
status = ioctl(fd, 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;
status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
return(fd);
}