Linux C实现文件传输
副标题#e#
file_server.c 文件传输顺序处事器示例
//////////////////////////////////////////////////////////////////////////////////////< br>// file_server.c 文件传输顺序处事器示例
//////////////////////////////////////////////////////////////////////////////////////
// 本文件是处事器的代码
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
*/
#define HELLO_WORLD_SERVER_PORT 6666
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
int main(int argc, char **argv)
{
//配置一个socket地点布局server_addr,代表处事器internet地 址, 端口
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof (server_addr)); //把一段内存区的内容全部配置为0
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons (HELLO_WORLD_SERVER_PORT);
//建设用于internet的流协议(TCP)socket,用server_socket代表处事 器socket
int server_socket = socket(PF_INET,SOCK_STREAM,0);
if( server_socket < 0)
{
printf("Create Socket Failed!");
exit(1);
}
//把socket和socket地点布局接洽起来
if( bind(server_socket,(struct sockaddr*) &server_addr,sizeof(server_addr)))
{
printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT);
exit(1);
}
//server_socket用 于监听
if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) )
{
printf ("Server Listen Failed!");
exit(1);
}
while (1) //处事器端要 一直运行
{
//界说客户端的socket地点布局client_addr
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
//接管一 个到server_socket代表的socket的一个毗连
//假如没有毗连请求,就期待到有毗连请求--这是 accept函数的特性
//accept函数返回一个新的socket,这个socket(new_server_socket)用于同连 接到的客户的通信
//new_server_socket代表了处事器和客户端之间的一个通信通道
//accept函数把毗连到的客户端信息填写到客户端的socket地点布局client_addr中
int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
if ( new_server_socket < 0)
{
printf("Server Accept Failed!\n");
break;
}
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
length = recv (new_server_socket,buffer,BUFFER_SIZE,0);//这里先吸收客户端发来的要获取的文件名
if (length < 0)
{
printf("Server Recieve Data Failed! \n");
break;
}
char file_name [FILE_NAME_MAX_SIZE+1];
bzero(file_name, FILE_NAME_MAX_SIZE+1);
strncpy (file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen (buffer));
// int fp = open(file_name, O_RDONLY);
// if( fp < 0 )
FILE * fp = fopen(file_name,"r");
if(NULL == fp )
{
printf("File:\t%s Not Found\n", file_name);
}
else
{
bzero(buffer, BUFFER_SIZE);
int file_block_length = 0;
// while( (file_block_length = read(fp,buffer,BUFFER_SIZE))>0)
while( (file_block_length = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
{
printf("file_block_length = %d\n",file_block_length);
//发 送buffer中的字符串到new_server_socket,实际是给客户端
if(send (new_server_socket,buffer,file_block_length,0)<0)
{
printf("Send File:\t%s Failed\n", file_name);
break;
}
bzero(buffer, BUFFER_SIZE);
} //这段代码是轮回读取文件的一段数据,在轮回挪用send,发 送到客户端,这里强调一点的TCP每次接管最多是1024字节,多了就会分片,因此每次发送时只管不要高出 1024字节。
// close(fp);
fclose(fp);
printf ("File:\t%s Transfer Finished\n",file_name);
}
//封锁与客户端的 毗连
close(new_server_socket);
}
//封锁监听用的socket
close (server_socket);
return 0;
}
#p#副标题#e#
#p#分页标题#e#
file_client.c 文件传输客户端措施示例
//////////////////////////////////////////////////////////////////////////////////////< br>// file_client.c 文件传输客户端措施示例
//////////////////////////////////////////////////////////////////////////////////////
// 本文件是客户机的代码
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
*/
#define HELLO_WORLD_SERVER_PORT 6666
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
//配置一个socket地点布局client_addr,代表客户机internet地点, 端口
struct sockaddr_in client_addr;
bzero(&client_addr,sizeof(client_addr)); //把一段内存 区的内容全部配置为0
client_addr.sin_family = AF_INET; //internet协议族
client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY暗示自动获取本机地点
client_addr.sin_port = htons(0); //0暗示让系统自动分派一个空闲端口
//建设用于internet 的流协议(TCP)socket,用client_socket代表客户机socket
int client_socket = socket (AF_INET,SOCK_STREAM,0);
if( client_socket < 0)
{
printf ("Create Socket Failed!\n");
exit(1);
}
//把客户机的socket和 客户机的socket地点布局接洽起来
if( bind(client_socket,(struct sockaddr*) &client_addr,sizeof(client_addr)))
{
printf("Client Bind Port Failed!\n");
exit(1);
}
//配置一个socket地点布局server_addr,代表 处事器的internet地点, 端口
struct sockaddr_in server_addr;
bzero (&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
if (inet_aton(argv[1],&server_addr.sin_addr) == 0) //处事器的IP地点来自措施的参数
{
printf("Server IP Address Error!\n");
exit(1);
}
server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
socklen_t server_addr_length = sizeof(server_addr);
//向处事器提倡毗连,毗连乐成后client_socket代表了客户机和处事器的一个 socket毗连
if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printf("Can Not Connect To %s! \n",argv[1]);
exit(1);
}
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name, FILE_NAME_MAX_SIZE+1);
printf("Please Input File Name On Server:\t");
scanf("%s", file_name);
char buffer[BUFFER_SIZE];
bzero(buffer,BUFFER_SIZE);
strncpy(buffer, file_name, strlen(file_name) >BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));
//向处事器发送buffer中的数据
send (client_socket,buffer,BUFFER_SIZE,0);
// int fp = open(file_name, O_WRONLY|O_CREAT);
// if( fp < 0 )
FILE * fp = fopen(file_name,"w");
if(NULL == fp )
{
printf("File:\t%s Can Not Open To Write\n", file_name);
exit(1);
}
//从处事器吸收数据到buffer中
bzero(buffer,BUFFER_SIZE);
int length = 0;
while( length = recv(client_socket,buffer,BUFFER_SIZE,0)) //轮回接 收,再写到文件
{
if(length < 0)
{
printf ("Recieve Data From Server %s Failed!\n", argv[1]);
break;
}
// int write_length = write(fp, buffer,length);
int write_length = fwrite(buffer,sizeof(char),length,fp);
if (write_length<length)
{
printf("File:\t%s Write Failed\n", file_name);
break;
}
bzero(buffer,BUFFER_SIZE);
}
printf("Recieve File:\t %s From Server[%s] Finished\n",file_name, argv[1]);
close(fp);
//封锁socket
close(client_socket);
return 0;
}