首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

关于网络编程的有关问题-帮忙看上程序(新手)

2012-08-17 
关于网络编程的问题--帮忙看下程序(新手)实现功能,从服务端下载有mp3歌名的文件列表并读取里面的歌名,通过

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


[解决办法]
默认socket的read和write两个函数都是阻塞的。
可将客户端和服务器的socket设置成非阻塞。
int flag;
flag = fcntl(sock_fd, F_GETFL, 0);
flag |= O_NONBLOCK;
fcntl(sock_fd, F_SETFL, flag);

热点排行