#include <rtthread.h>
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include <ff.h>
#include <diskio.h>
#include "vs1053.h"
void play2(char *wavFile);
void play(int bytes_received);
static const char send_data[] = "need!"; /* 发送用到的数据 */
void play(int bytes_received)//播放接收到的网络数据
{
int i;
for(i=0;i<bytes_received;i=i+32)
{
while(VS_DREQ_READ()==0);
VS_DCn_LOW();
VS_CSn_HIGH();
for(k=0;k<32;k++)
{
_ssi_readwrite(*(recv_data + k));
}
VS_DCn_HIGH();
VS_CSn_LOW();
}
}
void tcpclient(const char* url, int port)//应用程序
{
/////////////////////////////////////////
// FIL file;
// FRESULT res;
int br,num;
//////////////////////////////////////////
char *recv_data,k;
struct hostent *host;
int sock, bytes_received;
struct sockaddr_in server_addr;
//准备接收网络数据
host = gethostbyname(url);
recv_data = rt_malloc(128);//(512);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
rt_free(recv_data);
return;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
rt_kprintf("Connect error\n");
rt_free(recv_data);
return;
}
while(1)
{
//收到网络数据包
bytes_received = recv(sock, recv_data, 128, 0);
if (bytes_received < 0)
{
lwip_close(sock);
rt_free(recv_data);
break;
}
play(bytes_received); //开始播放
//告诉PC端发送下一包网络数据
send(sock,send_data,strlen(send_data), 0);
}
return;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出tcpclient函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
#endif
|