[原创] 【TI原创】基于LM3S8962的网络收音机(五) --- PC端发送程序

chenzhufly   2011-10-13 17:23 楼主

为了配合LM3S8962+VS1053的调试,从网上下载并修改了一个发送文件的程序,用来完成:

 

1.  网络下载存储播放;

2.  网络实时播放

 

为进一步实现网络收音机的功能打下结实的基础。

 

代码功能简单,如下所示:

  1. #include<winsock2.h>                                                                                              
    #include<stdio.h> 
    #include <sys/stat.h>

  2. char aa[100000]={0};
                                                                                                                    
    void main()                                                                                                       
    {

  3. FILE *file1;                                                                                               
    struct stat statBuf;
    unsigned long file_size,num;
     
    WORD wVersionRequested;                                                                                           
    WSADATA wsaData;
    SOCKET sockSrv,sockConn;
    SOCKADDR_IN addSrv;
    SOCKADDR_IN addClient;                                                                                                  
    int err,len,i,j,k; 
    char recvBuf[100];                                                                                                         
    unsigned char sendBuf[512];// = {'1','2','3','0'};
    unsigned char *p = sendBuf;
    char *pp="wait for request!";  


  4. file1=fopen("C://Windows.wav","rb");
    fstat(fileno(file1),&statBuf);
    file_size = statBuf.st_size;
    printf("%ld",file_size);

  5.                                                                                                               
    wVersionRequested = MAKEWORD( 2, 2 );                                                                                                                                                                                             
    //加载套接字DLL库                                                                                                 
    err = WSAStartup( wVersionRequested, &wsaData );                                                                  
    if ( err != 0 )                                                                                                   
    {                                                                                                                 
     return;                                                                                                          
    }                                                                                                                 
                                                                                                                      
    if ( LOBYTE( wsaData.wVersion ) != 2 ||                                                                           
     HIBYTE( wsaData.wVersion ) != 2 )                                                                                
    {                                                                                                                                                                                                 
     WSACleanup( );                                                                                                   
     return;                                                                                                          
    }                                                                                                                 
                                                                                                                      
     //1、创建套接字                                                                                                  
    sockSrv=socket(AF_INET,SOCK_STREAM,0);                                                                     
    /*第一个参数对于TCP/IP协议的套接字,只能是AF_INET或PF_INET,第二个参数对于                                         
     1.1版本的socket,只支持两种类型的套接字,SOCK_STREAM指定产生流式套接字,                                          
     SOCK_DGRAM产生数据报套接字,第三个参数为零时自动选择合适的协议。*/                                               
                                                                                                                      
    //2、绑定套接字到一个本地地址和端口上                                                                             
                                                                                                  
    //htonl是把u_long类型转换为网络字节序                                                                             
    //htons是把u_short类型转换为网络字节序                                                                            
    addSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);   //inet_addr("192.168.1.11");                                                                 
    addSrv.sin_family=AF_INET;                                                                                        
    //unsigned?? short?? sin_port;                                                                                    
    addSrv.sin_port=htons(4000);                                                                                      

  6. bind(sockSrv,(SOCKADDR*)&addSrv,sizeof(SOCKADDR));                                                              
                                                                                                                      
      //3、将套接字设为监听模式,准备接受客户请求                                                                     
    listen(sockSrv,5);                                                                                                
                                                                                                                      
                                                                                                                                                                                                           
    len=sizeof(SOCKADDR);                                                                                         

  7. sockConn=accept(sockSrv,(SOCKADDR*)&addClient,&len);                                                       
    //accept函数第三个参数必须赋初始值,初始值为结构体的长度。否则函数调用失败                                        
                                                                                                                      
                                                                                                                     
    num = 0;                                                                                                                  
      //4、用返回的套接字和客户端进行通信                                                                             
    while(1)                                                                                                          
    {                                                                                                

  8.  k = fread(sendBuf,1,128,file1);
     if(k==-1)
     {
             fclose(file1);printf("read error!\n"); break;
     }
      num = num + k;
     
     send(sockConn,sendBuf,128,0);  //发送一个网络数据包

  9.  if(num > file_size - 128)
        {

  10.    printf("data over!");
       while(1)
       {
       
       }
     }
     printf("wait another packet!");

  11.  k = recv(sockConn,recvBuf,100,0); //等待回馈
                                                                                                                                                                                                                                                                                                          
    }
     closesocket(sockConn);
     fclose(file1);
    }  

 

代码下载:

PC程序.rar (1.72 MB)
(下载次数: 183, 2011-10-13 17:23 上传)

[ 本帖最后由 chenzhufly 于 2011-10-13 17:24 编辑 ]
生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙 =================================== 做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰

回复评论 (6)

回复 楼主 chenzhufly 的帖子

楼主的速度好快,都跟不上了。
谢谢!
点赞  2011-10-13 23:12

不是一个人在战斗 是我师弟很给力 哈哈

 

我文档发都来不及

 

基本上是一步一步 点点滴滴了

生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙 =================================== 做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰
点赞  2011-10-14 10:23

引用: 原帖由 chenzhufly 于 2011-10-14 10:23 发表 不是一个人在战斗 是我师弟很给力 哈哈   我文档发都来不及   基本上是一步一步 点点滴滴了

 

厉害,顶顶。

点赞  2011-10-14 12:28

太厉害了, 支持一个

点赞  2011-10-14 12:57
哈哈 期待大家的加入和指导
生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙 =================================== 做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰
点赞  2011-10-14 21:36
看一下
店铺:http://mcuc.taobao.com;单片机视频教程资料
点赞  2011-11-6 09:52
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复