cli.c
----------------
#include <stdio.h>
#include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <pthread.h>
#include "fget.h"
Counter counter; int fd; void *client_thread(void * arg) { Packet packet;
Packet *ppkt = &packet; ssize_t bytes = 0; int ser_fd = (int)arg;
while (1)
{ bzero(&packet, sizeof(Packet)); /*
// Increase the count number; pthread_mutex_lock(&cnt_lock); // count == -1 indicate that the file has read to the end. if (-1 == count) { pthread_mutex_unlock(&cnt_lock); pthread_exit((void *)0); } packet.seq = count++; pthread_mutex_unlock(&cnt_lock); */ bytes = recv(ser_fd, ppkt, sizeof(Packet), 0);
//bytes = pread(fd, ppkt->data, (ppkt->seq * BUFSIZE), BUFSIZE);
if (-1 == bytes) { continue; } else if (0 == bytes) { break; }
bytes = pwrite(fd, (char *)ppkt->data, bytes, (ppkt->seq) * BUFSIZE);
} return ((void *)0);
} int main(int argc, char *argv[]) { int sockfd; struct hostent *host; struct sockaddr_in serv_addr; /* local machine information */ socklen_t sin_size; pthread_t pt[MAX_THREAD_NUM];
void *pret; int i = 0; // count of thread
if (argc < 3) {
fprintf(stderr,"Usage: %s ServerIP FileName\n", argv[0]); return (-1); } /*
//if((struct hostent *)NULL == (host = gethostbyname(argv[1]))) if(NULL == (host = gethostbyname(argv[1]))) { perror("gethostbyname error! \n"); return (-2); } */ bzero(&counter, sizeof(Counter)); if (pthread_mutex_init(&(counter.cnt_lock), NULL) != 0)
{ perror("Initialize cnt_lock mutex error!\n"); return (-1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket create error "); return(-3); }
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT); //serv_addr.sin_addr = *((struct in_addr *)(host->h_addr)); //memcpy(&(serv_addr.sin_addr), host->, host->h_length); inet_pton(AF_INET, argv[1], &serv_addr.sin_addr); bzero(&(serv_addr.sin_zero),8); if (-1 == (fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))
{ perror("File open error. \n"); // TODO: add exit functions } if (-1 == connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
perror("listen error "); return (-4); } for (i = 0; i < MAX_THREAD_NUM + 5; i++)
{ if (0 != pthread_create(&pt[i], NULL, client_thread, (void *)sockfd))
{ perror("Create thread error\n"); //exit(-1); continue; } } for (i = 0; i < MAX_THREAD_NUM + 5; i++)
{ if (0 != pthread_join(pt[i], &pret)) { fprintf(stderr, "can‘t join with thread %d\n", i); } //if () } close(fd);
pthread_mutex_destroy(&counter.cnt_lock);
return 0;
} -------------------------------------- #include <stdio.h>
#include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <pthread.h>
#include "fget.h"
pthread_mutex_t cnt_lock;
long count = 0; int fd;
void *server_thread(void * arg) { Packet packet;
Packet *ppkt = &packet; ssize_t bytes = 0; int cli_fd = (int)arg;
while (1)
{ bzero(&packet, sizeof(Packet)); // Increase the count number;
pthread_mutex_lock(&cnt_lock); // count == -1 indicate that the file has read to the end. if (-1 == count) { pthread_mutex_unlock(&cnt_lock); pthread_exit((void *)0); } packet.seq = count++; pthread_mutex_unlock(&cnt_lock); bytes = pread(fd, ppkt->data, BUFSIZE, (ppkt->seq * BUFSIZE));
if (-1 == bytes) { continue; } else if (BUFSIZE != bytes) { // We have read to the end of file, so we should set count to 1 to indicate others
//that we have read all the data of the file and need to go out. pthread_mutex_lock(&cnt_lock); count = -1; pthread_mutex_unlock(&cnt_lock); } send(cli_fd, (char *)ppkt, bytes, 0);
} return ((void *)0);
} int main(int argc, char *argv[]) { int sockfd,client_fd; /*sock_fd Monitor socket; client_fd Data transfer socket */ struct sockaddr_in my_addr; /* local machine information */ struct sockaddr_in remote_addr; /* client address information */ socklen_t sin_size; int i = 0;
pthread_t pt[MAX_THREAD_NUM + 10];
void * pret; if (pthread_mutex_init(&cnt_lock, NULL) != 0)
{ perror("Initialize cnt_lock mutex error!\n"); return (-1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket create error "); exit(1); } my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVPORT); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind error "); exit(1); } if (-1 == (fd = open(argv[1], O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { perror("File open error. \n"); // TODO: add exit functions } if (listen(sockfd, BACKLOG) == -1) {
perror("listen error "); exit(1); } while (1) { sin_size = sizeof(struct sockaddr_in); if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) { perror("accept error"); continue; } printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr)); if (pthread_create(&pt[i++], NULL, server_thread, (void *)client_fd) != 0)
{ perror("Create thread error\n"); //exit(-1); break; } printf("Create thread %d ok\n", client_fd); } for (; i--; )
{ if (0 != pthread_join(pt[i], &pret)) { fprintf(stderr, "can‘t join with thread %d\n", i); } } // TODO: Add pthread_join to wait for all threads finished data transfer. close(fd); close(client_fd); close(sockfd); pthread_mutex_destroy(&cnt_lock);
return 0;
} |
|