文件传输(可以传输给多个客户)

/*************************************************************************
	> File Name: server_trans.c
	> Author: qinf
	> Mail: 
	> Created Time: Fri 04 Apr 2014 03:14:29 PM CST
 ************************************************************************/

#include "tcp.h"
#define SIZE 1024*1024
char filename[256];
void *send_file(void *arg) {

	//send
	int client_fd = *(int *)arg;
	printf("client_fd: %d\n", client_fd);
	int iret;
	char send_buf[SIZE];
	int count = 0, file_fd;
	memset(send_buf, 0, SIZE);
	file_fd = open(filename, O_RDONLY);
	if (file_fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}
	printf("===============\n");
	while ((iret = read(file_fd, send_buf, SIZE)) > 0) {
		++count;
		printf("--%4d--read %d bytes from file\n", count, iret);
		iret = send(client_fd, send_buf, iret, 0);
		printf("send %d bytes to file\n", iret);
		memset(send_buf, 0, SIZE);
	}
	printf("over \n");
	close(file_fd);
	close(client_fd);
}
int main(int argc, char *argv[])
{
	int server_fd, client_fd;
	struct sockaddr_in server_addr, client_addr;
	int iret;
	memset(filename, 0, 256);
	strcpy(filename, argv[3]);
	pthread_t a_thread;
	//socket
	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == server_fd) {
		perror("socket");
		exit(EXIT_FAILURE);
	}
	//bind
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	iret = bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
	if (iret == -1) {
		perror("bind");
		exit(EXIT_FAILURE);
	}
	//listen
	iret = listen(server_fd, 5);
	if (-1 == iret) {
		perror("listen");
		exit(EXIT_FAILURE);
	}
	//accept
	while (1) {
		client_fd = accept(server_fd, NULL, NULL);
		if (-1 == client_fd) {
			perror("accept");
			close(client_fd);
			exit(EXIT_FAILURE);
		}
		pthread_create(&a_thread, NULL, send_file, (void *)&client_fd);
		printf("a client connect!\n");

	}
	//close
	close(server_fd);

	return 0;
}


/*************************************************************************
	> File Name: client_trans.c
	> Author: qinf
	> Mail: 
	> Created Time: Fri 04 Apr 2014 03:39:46 PM CST
 ************************************************************************/

#include "tcp.h"
#define SIZE 10*1024*1024
int main(int argc, char *argv[])
{
	int client_fd, file_fd;
	struct sockaddr_in server_addr;
	int iret;
	char recv_buf[SIZE];
	file_fd = open(argv[3], O_WRONLY| O_CREAT, 0664);
	if (file_fd == -1) {
		perror("open file");
		exit(EXIT_FAILURE);
	}
	//socket
	client_fd = socket(AF_INET, SOCK_STREAM, 0);
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	//connect
	iret = connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
	if (-1 == iret) {
		perror("connet");
		exit(EXIT_FAILURE);
	}
	//receive
	memset(recv_buf, 0, SIZE);
	int count = 0;
	while ((iret = recv(client_fd, recv_buf, SIZE, 0)) > 0) {
		++count;
		printf("--%4d--,recv %d bytes", count, iret);
		iret = write(file_fd, recv_buf, iret);
		printf("write %d bytes to file\n", iret);
		memset(recv_buf, 0, SIZE);
	}
	//close
	close(file_fd);
	close(client_fd);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值