- 论坛徽章:
- 0
|
在调试这个程序时,客户端只发送第一次记录的语音信息,而服务端也不断的播放第一次收到的语音。
服务器代码:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* 存储秒数 */
#define RATE 8000 /* 采样频率 */
#define SIZE 8 /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */
// 用于保存数字音频数据的内存缓冲区 8000bytes
//unsigned char sound_buf[1024];
unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/8];
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(783;
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);
addr_len = sizeof(client_addr);
while (1) {
len = recvfrom(recv_sock, sound_buf, sizeof(sound_buf) - 1, 0, (struct sockaddr *) &client_addr, &addr_len);
if (len < 0) {
perror("recvfrom"
exit(errno);
}
#if 1
printf("收到来自%s:%d的消息\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
printf("Client said:\n"
sound_fd=get_sound();
state = write(sound_fd, sound_buf, sizeof(sound_buf)); /* 回放 */
if (state != sizeof(sound_buf))
perror("wrote wrong number of bytes");
/* 在继续录音前等待回放结束 */
state = ioctl(sound_fd, SOUND_PCM_SYNC, 0);
if (state == -1)
perror("SOUND_PCM_SYNC ioctl failed");
close(sound_fd);
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);
#else
buff[len] = '\0';
printf("收到来自%s:%d的消息\n\r",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
printf("The received data are as bellow:\n");
fputs(buff,stdout);
for(i=0;i<len;i++)
if((buff<='Z')&&(buff>='A'))
buff+=0x20;
printf("The received data after dealing with are:\n");
fputs(buff,stdout);
printf("The received data after dealing with are sending...\n");
client_addr.sin_port = htons(7860);
printf("服务器发送到%s:%d的消息\n\r",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
sendto(send_sock,buff,strlen(buff),0,(struct sockaddr *) &client_addr, addr_len);
#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);
}
客户端代码:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* 存储秒数 */
#define RATE 8000 /* 采样频率 */
#define SIZE 8 /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */
// 用于保存数字音频数据的内存缓冲区 8000bytes
unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/8];
//unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/8];
#define DEST_IP "192.168.0.41"
#define MAXSIZE 128
void str_client(FILE* fp,int send_sock,int recv_sock);
int get_sound(void);
int main(void)
{
struct sockaddr_in send_addr;
struct sockaddr_in recv_addr;
int send_sock,recv_sock;
int addr_len;
int len;
//char buff[128];
if ((send_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(errno);
}else
printf("create socket.\n\r");
if ((recv_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket_r");
exit(errno);
} else
printf("create client receving socket.\n\r");
memset(&send_addr, 0, sizeof(struct sockaddr_in));
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons(7830);
send_addr.sin_addr.s_addr = inet_addr(DEST_IP);
if ((bind(send_sock, (struct sockaddr *)&send_addr, sizeof(send_addr))) == -1) {
perror("bind_client_send");
exit(errno);
} else
printf("bind address to client send socket.\n");
memset(&recv_addr, 0, sizeof(struct sockaddr_in));
recv_addr.sin_family = AF_INET;
recv_addr.sin_port = htons(7860);
recv_addr.sin_addr.s_addr = inet_addr(DEST_IP);
if ((bind(recv_sock, (struct sockaddr *)&recv_addr, sizeof(recv_addr))) == -1) {
perror("bind_client_recv");
exit(errno);
} else
printf("bind address to client receive socket.\n");
str_client(stdin,send_sock,recv_sock);
close(send_sock);
close(recv_sock);
return 0;
}
void str_client(FILE* fp,int send_sock,int recv_sock)
{
struct sockaddr_in fromaddr;
socklen_t fromlen;
struct sockaddr_in dest_addr;
char send_buf[MAXSIZE], recvs[MAXSIZE];
int dest_sock;
int n=0, i;
int sound_fd;
int state;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(7840);
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
if ((dest_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket_r");
exit(errno);
}else
printf("create dest receving socket.\n\r");
sound_fd=get_sound();
while(1){
printf("Say something:\n");
state = read(sound_fd, sound_buf, sizeof(sound_buf)); /* 录音 */
if (state != sizeof(sound_buf))
perror("read wrong number of bytes");
close(sound_fd);
#if 1
#if 1
sendto(send_sock,&sound_buf,sizeof(sound_buf),0,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr));
printf("发送到%s:%d的消息\n",inet_ntoa(dest_addr.sin_addr), ntohs(dest_addr.sin_port));
#else
for(i=0;i<(3*;i++){
sendto(send_sock,&send_buf[i*1024],1024,0,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr));
printf("发送到%s:%d的消息:%d*1024\n",inet_ntoa(dest_addr.sin_addr), ntohs(dest_addr.sin_port),i);
}
#endif
printf("waiting receive...\n");
fromlen = sizeof(fromaddr);
n = recvfrom(recv_sock, recvs, sizeof(recvs) - 1, 0, (struct sockaddr *)&fromaddr, &fromlen);
if (n < 0) {
perror("recvfrom");
exit(errno);
}else{
recvs[n]='\0';
printf("收到来自%s:%d的消息:%s\n",inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port),recvs);
}
#else
sendto(send_sock,send_buf,strlen(send_buf),0,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr));
printf("发送到%s:%d的消息:%s\n",inet_ntoa(dest_addr.sin_addr), ntohs(dest_addr.sin_port),send_buf);
printf("send success!\n");
printf("waiting receive...\n");
fromlen = sizeof(fromaddr);
n = recvfrom(recv_sock, recvs, sizeof(recvs) - 1, 0, (struct sockaddr *)&fromaddr, &fromlen);
if (n < 0) {
perror("recvfrom");
exit(errno);
}else{
recvs[n]='\0';
printf("收到来自%s:%d的消息:%s\n",inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port),recvs);
if(memcmp(&fromaddr,&dest_addr,fromlen-sizeof(fromaddr.sin_zero))!=0){
printf("Error recving data just for non-expected address, and data as bellow:\n");
}else
printf("The address and port when sending and receving are the same\n");
}
#endif
}
close(dest_sock);
}
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);
} |
|