[clientvod.c]#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include #define PORT 59277
#define SIZE 4 * 1024int play_list(int ssd)
{
int i;
struct dirent *file; file = malloc(sizeof(struct dirent *));
i = 1;
while (i != 14)
{
read(ssd, file, sizeof(struct dirent));
printf("[%d] %s\n", i, file->d_name);
++i;
}
printf("which do you want to play?:(1 - %d) ", i - 1);
scanf("%d", &i);
write(ssd, &i, sizeof(i)); return 0;
}void play()
{
execlp("mplayer", "mplayer", "-slave", "-quiet", "/tmp/cache", NULL);
}int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "usage: %s IP\n", argv[0]);
return -1;
} int i, ssd, cad, len = 1;
char buffer[SIZE], ctrl[100];
struct sockaddr_in s_addr; mkfifo("/tmp/cache", O_CREAT | O_RDWR);
ssd = socket(PF_INET, SOCK_STREAM, 0);
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(PORT);
inet_pton(AF_INET, argv[1], &s_addr.sin_addr); connect(ssd, (struct sockaddr *)&s_addr, sizeof(s_addr));
play_list(ssd);
if (fork() == 0)
{
play();
exit(0);
}
cad = open("/tmp/cache", O_RDWR);
while (len)
{
len = read(ssd, buffer, sizeof(buffer));
write(cad, buffer, len);
}
close(cad);
system("rm -rf /tmp/cache"); return 0;
}
--------------------------------------------------------------------------------
[servervod.c]#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include #define PORT 59277
#define SIZE 4 * 1024
#define MOVIE "/resource/movie/"char fn[100][500]={};int play_list(int csd)
{
int i;
DIR *dir;
struct dirent *file; dir = opendir(MOVIE);
i = 0;
while (1)
{
file = readdir(dir);
if (file == NULL)
break;
memcpy(fn[i], MOVIE, sizeof(MOVIE));
memcpy(fn[i] + sizeof(MOVIE) - 1, file->d_name,
sizeof(file->d_name));
write(csd, file, sizeof(struct dirent));
++i;
}
closedir(dir); return 0;
}int send_data(int csd, int *num)
{
int fd, len = 1;
char buffer[SIZE]; read(csd, num, sizeof(*num));
fd = open(fn[*num - 1], O_RDONLY);
while (len)
{
len = read(fd, buffer, sizeof(buffer));
write(csd, buffer, len);
}
close(fd); return 0;
}int main()
{
int i, num, ssd, csd, addrlen;
struct sockaddr_in s_addr, c_addr; ssd = socket(PF_INET, SOCK_STREAM, 0);
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "0.0.0.0", &s_addr.sin_addr); i = 1;
setsockopt(ssd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
bind(ssd, (struct sockaddr *)&s_addr, sizeof(s_addr));
printf("waiting......\n");
listen(ssd, 10);
addrlen = sizeof(c_addr);
while(1)
{
csd = accept(ssd, (struct sockaddr *)&c_addr, &addrlen);
if (csd == -1)
continue;
printf("connect success\n");
play_list(csd);
if (fork() == 0)
{
send_data(csd, &num);
exit(0);
}
} return 0;
}
阅读(627) | 评论(0) | 转发(0) |