资源简介
LINUX下的一个多线程的服务器和客户端的网络传输程序,初学者可以作为参考。
LINUX下的一个多线程的服务器和客户端的网络传输程序,初学者可以作为参考

代码片段和文件信息
/*
Very simple file transferring client.
Copyright (C) 2004-2006 Li Suke School of Software and Microelectronics
Peking University
This is free software; you can redistribute it and/or
modify it freely.
This software is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include “rw.h“
int main(int argc char *argv[]){
int sockfd;
int conn_ret;
struct sockaddr_in servaddr;
char cmd_buf[MAX_LEN];
char recvbuf[MAX_LEN];
int error_flag;
int len = MAX_LEN;
int file_fd;
int dot_number;
int total_len = 0;
if(argc != 5){
printf(“Usage: fileclient \n“);
return 0;
}
if((sockfd = socket(AF_INETSOCK_STREAM0)) == -1){
perror(“sock“);
exit(1);
}
bzero(&servaddrsizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INETargv[1]&servaddr.sin_addr);
conn_ret = connect(sockfd(struct sockaddr *)&servaddrsizeof(servaddr));
if(conn_ret == -1){
perror(“connect“);
}
file_fd = open(argv[4]O_CREAT | O_WRONLY);
if(file_fd == -1){
perror(“open“);
exit(1);
}
len = strlen(argv[3])+3;
snprintf(cmd_buflen“%s\r\n“argv[3]);
if((error_flag = writeall(sockfdcmd_buf&len))==-1){
exit(1);
}
len = MAX_LEN;
printf(“\nfile is transferring:\n“);
while ((error_flag = readall(sockfdrecvbuf&len))==0){
if(len == 0){
printf(“\nClient has received file!\n“);
break;
}
printf(“.“);
printf(“read length is %d\n“len);
dot_number++;
if((dot_number % DOT_PERIOD) ==0){
printf(“\n“);
dot_number = 0;
}
total_len+=len;
if(writeall(file_fdrecvbuf&len) == -1){
printf(“\nclient has some error when receive the file!\n“);
break;
}
len = MAX_LEN;
}
printf(“\nRecevied %d bytes\n“total_len);
close(file_fd);
close(sockfd);
return;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3757 2006-06-16 10:03 pthreadfile\rw.c
文件 288 2006-06-16 10:03 pthreadfile\rw.h
文件 252 2006-06-16 10:03 pthreadfile\Makefile
文件 2415 2006-06-16 10:03 pthreadfile\fileclient.c
文件 2575 2006-06-16 10:03 pthreadfile\fileserver.c
目录 0 2006-06-16 10:03 pthreadfile
----------- --------- ---------- ----- ----
9505 7
- 上一篇:课程设计_高速公路收费系统
- 下一篇:进度条显示导出文件进度
相关资源
- Learning Linux Binary Analysis
- 蓝牙源代码应用于LINUX
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- 编写简单的RMI程序 多线程web 服务器
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- 多线程生产者消费者模式
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- 多线程实例:桌面智能弹球小游戏
评论
共有 条评论