Linux下通过socket通信实现客户端向服务器发送文件

通过socket通信实现客户端向服务器实现文件发送。整合好的代码如下:

Client端的代码

/*
 *************************************************
 *Name : send_file.c                       *
 *Date : 2015-04-15                        *
 *Author : Sniper                          *
 *Aim : Client send the file to the Server.*
 *************************************************
 */

#include 
#include 
#include 
#include 
#include 
#include 

#define BUFFER_SIZE                1024  
#define FILE_NAME_MAX_SIZE         512  

int main(int argc,char *argv[])
{
	int socketfd;
	struct sockaddr_in s_add,c_add;
	unsigned short portnum = 0x22B8;
	int len;
	char buffer[BUFFER_SIZE];  
	FILE *fp ;
	int file_block_length = 0;  
	
	
	
	/*
	 *Create the socket
	 */
	if((socketfd=socket(AF_INET,SOCK_STREAM,0))<0)
	{
		printf("Socket create error! \n");
		exit(1);
	}
	
	/*
	 *set the address format
	 */	
	bzero(&s_add,sizeof(struct sockaddr_in));
	s_add.sin_family = AF_INET;
	/*
	 *change the VM address
	 */
	s_add.sin_addr.s_addr = inet_addr("192.168.81.122");//change the string to 32-bit internet byte.
	s_add.sin_port=htons(portnum);
	
	if (connect(socketfd,(struct sockaddr *)(&s_add),sizeof(struct sockaddr))<0)
	{
		printf("Connect failure!\n");
		return -1;
	}
	else
		printf("Connect Success!\n");
	
	/*
	 *Send the file
	 */		
	fp = fopen("test", "r");  
	if (fp == NULL)  
	{  
		printf("File: test Not Found!\n");  
	}  
	else  
	{  
		bzero(buffer, BUFFER_SIZE);  		
		while( (file_block_length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)  
		{  
		    printf("file_block_length = %d\n", file_block_length);  

			/*
			 *Send the string to the buffer  
			 */
		    if (send(socketfd, buffer, file_block_length, 0) < 0)  
		    {  
		        printf("Send File:test Failed!\n");  
		        break;  
		    }  

		    bzero(buffer, sizeof(buffer));  
		}  
		fclose(fp);  
		printf("File:test Transfer Finished!\n");  
	}  
	close(socketfd);  
	
	return 0;
}

Server端的代码:

/*
 ************************************************
 *Name : receive_file.c                   *
 *Date : 2015-04-15                       *
 *Author : Sniper                         *
 *Aim : Service can receive more than one *
 *      Client that send the file.The main*
 *      target is to get the file.        *
 ************************************************
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 



int socketfd_connect = 0;
pthread_t ntid;
int len = 0;


#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512


void *print_message(void *arg)
{
	/*
	 *define the buffer to receive the file
	 */
	int pthread_socket_connect;
	int i=0;
	int write_length;
	FILE *fp;
	char buffer[BUFFER_SIZE];
	int length = 0;
		
	bzero(buffer,BUFFER_SIZE);
	pthread_socket_connect = socketfd_connect;	
	pthread_detach(ntid);
	/*
	 *Receive the file
	 */	
	fp = fopen("test","w");
	if(NULL == fp )
	{
		printf("File:\t Can Not Open To Write\n");
		exit(1);
	}	
	
	
	while( length = recv(pthread_socket_connect,buffer,BUFFER_SIZE,0))
	{
		if(length < 0)
		{
		    printf("Recieve Data From Client Failed!\n");
		    break;
		}
		
		/*
		 *write the buffer to the file
		 */
		write_length = fwrite(buffer,sizeof(char),length,fp);
		if (write_length
编译两者即可,我用了一个test为例来测试,文件内容随意自己输入。大家可以自己尝试。


上图是演示结果!


你可能感兴趣的:(Linux)