关于网络编程的问题--帮忙看下程序(新手)
实现功能,从服务端下载有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<