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

来源:互联网 发布:苹果电脑软件少 编辑:程序博客网 时间:2024/06/14 23:07

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

Client端的代码

/* ************************************************* *Name : send_file.c                       * *Date : 2015-04-15                        * *Author : Sniper                          * *Aim : Client send the file to the Server.* ************************************************* */#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdlib.h>#include <memory.h>#include <string.h>#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;}elseprintf("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 <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdlib.h>#include <memory.h>#include <string.h>#include <sys/socket.h>#include <pthread.h>int socketfd_connect = 0;pthread_t ntid;int len = 0;#define BUFFER_SIZE 1024#define FILE_NAME_MAX_SIZE 512void *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<length){    printf("File:test Write Failed\n");    break;}bzero(buffer,BUFFER_SIZE);    }printf("Recieve File: test  From Client Finished\n");fclose(fp); /******************************************************************/pthread_exit(0);}int main(int argc,char *argv[]){int socketfd_listen;struct sockaddr_in server_addr,client_addr;unsigned short portnum = 0x22B8;int err;/* *Create the socket */if((socketfd_listen=socket(AF_INET,SOCK_STREAM,0))<0){printf("Socket create error! \n");exit(1);}/* *set the address format */bzero(&server_addr,sizeof(struct sockaddr_in));server_addr.sin_family=AF_INET;server_addr.sin_addr.s_addr=inet_addr("192.168.81.122");server_addr.sin_port=htons(portnum);if(bind(socketfd_listen,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))<0){printf("bind failure!\n");return -1;}printf("bind Success!\n");if(listen(socketfd_listen,5)<0){printf("Listen failure!\n");return -1;}len=sizeof(struct sockaddr);/* *Using the loop to send and receive the message. */while(1){socketfd_connect = accept(socketfd_listen, (struct sockaddr *)(&client_addr), &len);if(socketfd_connect<0){printf("accept fail !\n");return -1;}err = pthread_create(&ntid,NULL,print_message,NULL);if(err!=0)printf("can't create pthread!\n");}close(socketfd_listen);close(socketfd_connect);return 0;}
编译两者即可,我用了一个test为例来测试,文件内容随意自己输入。大家可以自己尝试。


上图是演示结果!


0 0
原创粉丝点击