当前位置: 技术问答>linux和unix
关于网络编程的问题--帮忙看下程序(新手)
来源: 互联网 发布时间:2017-01-28
本文导语: 实现功能,从服务端下载有mp3歌名的文件列表并读取里面的歌名,通过歌名再从服务端下载歌曲。 出现的问题:在读取完文件列表后被卡住不能再往下执行,如果把读取文件列表的函数(send_list,download_list)里的while...
实现功能,从服务端下载有mp3歌名的文件列表并读取里面的歌名,通过歌名再从服务端下载歌曲。
出现的问题:在读取完文件列表后被卡住不能再往下执行,如果把读取文件列表的函数(send_list,download_list)里的while循环取消,则能成功下载列表文件,但是还是不能再执行下去,因为发送或下载歌曲的函数里的while循环始终不能跳出来,请问改如何去修改?
=====================================================================================================
服务端程序如下:
#include
#include
#include
#include
#include
#include
#include
#include "net.h"
#include "file.h"
#define ASSERT(var, type) if(var 0)
{
printf("nRead is %d; ", nRead);
nWrite = write(cfd, buf, nRead);
printf(" nWite is %dn", nWrite);
count ++;
}
printf("had sent %d bytes datan", count);
close(fd);
}
//**************************************************************//
//
//Function: send mp3 to client
//
//**************************************************************//
void send_mp3(int sfd, int cfd)
{
char buf[512] = {0};
int count = 0;
//received filename from client
int nRead = read(cfd, buf, sizeof(buf));
// open file
int fd = open(buf, O_RDONLY);
ASSERT(fd, open);
bzero(buf, sizeof(buf));
//sent filename to client
while((nRead = read(fd, buf, sizeof(buf))) > 0)
{
printf("nRead is %d; ", nRead);
int nWrite = write(sfd, buf, nRead);
printf("nWite is %dn", nWrite);
count ++;
}
printf("had sent %d bytes datan", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc 0)
{
printf("nRead is %d; ", nRead);
ASSERT(nWrite = write(fd, buf, nRead), write);
printf("nWrite is %dn", nWrite);
count ++;
}
printf("had written %d bytes datan", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc
出现的问题:在读取完文件列表后被卡住不能再往下执行,如果把读取文件列表的函数(send_list,download_list)里的while循环取消,则能成功下载列表文件,但是还是不能再执行下去,因为发送或下载歌曲的函数里的while循环始终不能跳出来,请问改如何去修改?
=====================================================================================================
服务端程序如下:
#include
#include
#include
#include
#include
#include
#include
#include "net.h"
#include "file.h"
#define ASSERT(var, type) if(var 0)
{
printf("nRead is %d; ", nRead);
nWrite = write(cfd, buf, nRead);
printf(" nWite is %dn", nWrite);
count ++;
}
printf("had sent %d bytes datan", count);
close(fd);
}
//**************************************************************//
//
//Function: send mp3 to client
//
//**************************************************************//
void send_mp3(int sfd, int cfd)
{
char buf[512] = {0};
int count = 0;
//received filename from client
int nRead = read(cfd, buf, sizeof(buf));
// open file
int fd = open(buf, O_RDONLY);
ASSERT(fd, open);
bzero(buf, sizeof(buf));
//sent filename to client
while((nRead = read(fd, buf, sizeof(buf))) > 0)
{
printf("nRead is %d; ", nRead);
int nWrite = write(sfd, buf, nRead);
printf("nWite is %dn", nWrite);
count ++;
}
printf("had sent %d bytes datan", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc 0)
{
printf("nRead is %d; ", nRead);
ASSERT(nWrite = write(fd, buf, nRead), write);
printf("nWrite is %dn", nWrite);
count ++;
}
printf("had written %d bytes datan", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc