|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
实现功能,从服务端下载有mp3歌名的文件列表并读取里面的歌名,通过歌名再从服务端下载歌曲。
出现的问题:在读取完文件列表后被卡住不能再往下执行,如果把读取文件列表的函数(send_list,download_list)里的while循环取消,则能成功下载列表文件,但是还是不能再执行下去,因为发送或下载歌曲的函数里的while循环始终不能跳出来,请问改如何去修改?
=====================================================================================================
服务端程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "net.h"
#include "file.h"
#define ASSERT(var, type) if(var < 0) {perror("type"); exit(0);}
#define FILE_NAME "list.txt"
//****************************************************************//
//
//Function: send list to cilent
//
//***************************************************************//
void send_list(int sfd, int cfd, char filename[])
{
char buf[512] = {0};
int count = 0;
// sent filename to cilent
strcpy(buf, filename);
int nWrite = write(cfd, buf, strlen(buf));
int nRead = read(cfd, buf, sizeof(buf));
puts(buf);
bzero(buf, sizeof(buf));
//sent content to cilent
int fd = open(filename, O_RDONLY);
ASSERT(fd, open);
while((nRead = read(fd, buf, sizeof(buf))) > 0)
{
printf("nRead is %d; ", nRead);
nWrite = write(cfd, buf, nRead);
printf(" nWite is %d\n", nWrite);
count ++;
}
printf("had sent %d bytes data\n", 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 %d\n", nWrite);
count ++;
}
printf("had sent %d bytes data\n", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("usage: server IP PORT");
exit(0);
}
int sfd = tcp_init(argv[1], atoi(argv[2]));// initialize sfd
while(1)
{
char filename[512] = {0};
strcpy(filename, FILE_NAME);
int cfd = tcp_accept(sfd);
printf("starting to send list to client......\n");
send_list(sfd, cfd, filename);
printf("sent list to client success......\n");
printf("starting to send mp3 to client......\n");
send_mp3(sfd, cfd);
printf("sent mp3 to client success......\n");
close(cfd);
}
close(sfd);
exit(0);
}
============================================================================================
客户端程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "net.h"
#include "file.h"
#define ASSERT(var, type) if(var < 0){perror("type"); exit(0);}
static char filename[] = {0};
//*********************************************************************//
//
// Function: download list from server
//
//********************************************************************//
void download_list(int sfd)
{
char buf[512] = {0};
int count = 0;
//received filename from server
int nRead = read(sfd, buf, sizeof(buf));
ASSERT(nRead, read);
strcpy(filename, buf);
int nWrite = write(sfd, "received success\n", 18);
ASSERT(nWrite, write);
int fd = open(buf, O_CREAT|O_WRONLY);//create list file
ASSERT(fd, open);
bzero(buf, sizeof(buf));
// received content from server
while((nRead = read(sfd, buf, sizeof(buf))) > 0)
{
printf("nRead is %d; ", nRead);
nWrite = write(fd, buf, nRead);
printf("nWrite is %d\n", nWrite);
count ++;
}
printf("had written %d bytes data\n", count);
close(fd);
}
//*********************************************************************//
//
// Function: download mp3 from server
//
//********************************************************************//
void download_mp3(int sfd, char filename[])
{
// get mp3_name in file
FILE *pf = fopen(filename, "r");
if(NULL == pf)
{
perror("fopen");
exit(0);
}
char mp3_name[512] = {0};
char buf[512] = {0};
fgets(mp3_name, sizeof(mp3_name), pf);
// if(fgets(file_mp3, sizeof(file_mp3), pf) == NULL)
// {
// printf("fgets error");
// exit(0);
// }
puts(mp3_name);
fclose(pf);
//sent mp3_name to server
int nWrite = write(sfd, mp3_name, strlen(mp3_name));
ASSERT(nWrite, write);
int fd = open(mp3_name, O_CREAT | O_WRONLY);
ASSERT(fd, open);
int count = 0, nRead;
while((nRead = read(sfd, buf, sizeof(buf))) > 0)
{
printf("nRead is %d; ", nRead);
ASSERT(nWrite = write(fd, buf, nRead), write);
printf("nWrite is %d\n", nWrite);
count ++;
}
printf("had written %d bytes data\n", count);
close(fd);
}
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("usage: client IP PORT\n");
exit(0);
}
int sfd = tcp_connect(argv[1],atoi(argv[2]));//connect to server
printf("starting to download list from server......\n");
download_list(sfd);
printf("download list success......\n");
printf("starting to download mp3 from server......\n");
download_mp3(sfd, filename);
printf("download mp3 success......\n");
close(sfd);
exit(0);
} |
|