程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Linux收集編程之socket文件傳輸示例

Linux收集編程之socket文件傳輸示例

編輯:關於C++

Linux收集編程之socket文件傳輸示例。本站提示廣大學習愛好者:(Linux收集編程之socket文件傳輸示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Linux收集編程之socket文件傳輸示例正文


本文所述示例法式是基於Linux平台的socket收集編程,完成文件傳輸功效。該示例是基於TCP流協定完成的socket收集文件傳輸法式。采取C說話編寫。終究可以或許完成傳輸任何格局文件的文件傳輸法式。

詳細完成代碼以下:

Server端代碼以下:

/************************************************************************* 
  > File Name: Server.c 
  > Author: SongLee 
 ************************************************************************/ 
 
#include<netinet/in.h> // sockaddr_in 
#include<sys/types.h>  // socket 
#include<sys/socket.h> // socket 
#include<stdio.h>    // printf 
#include<stdlib.h>   // exit 
#include<string.h>   // bzero 
 
#define SERVER_PORT 8000 
#define LENGTH_OF_LISTEN_QUEUE 20 
#define BUFFER_SIZE 1024 
#define FILE_NAME_MAX_SIZE 512 
 
int main(void) 
{ 
  // 聲明並初始化一個辦事器真個socket地址構造 
  struct sockaddr_in server_addr; 
  bzero(&server_addr, sizeof(server_addr)); 
  server_addr.sin_family = AF_INET; 
  server_addr.sin_addr.s_addr = htons(INADDR_ANY); 
  server_addr.sin_port = htons(SERVER_PORT); 
 
  // 創立socket,若勝利,前往socket描寫符 
  int server_socket_fd = socket(PF_INET, SOCK_STREAM, 0); 
  if(server_socket_fd < 0) 
  { 
    perror("Create Socket Failed:"); 
    exit(1); 
  } 
  int opt = 1; 
  setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 
 
  // 綁定socket和socket地址構造 
  if(-1 == (bind(server_socket_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)))) 
  { 
    perror("Server Bind Failed:"); 
    exit(1); 
  } 
   
  // socket監聽 
  if(-1 == (listen(server_socket_fd, LENGTH_OF_LISTEN_QUEUE))) 
  { 
    perror("Server Listen Failed:"); 
    exit(1); 
  } 
 
  while(1) 
  { 
    // 界說客戶真個socket地址構造 
    struct sockaddr_in client_addr; 
    socklen_t client_addr_length = sizeof(client_addr); 
 
    // 接收銜接要求,前往一個新的socket(描寫符),這個新socket用於同銜接的客戶端通訊 
    // accept函數會把銜接到的客戶端信息寫到client_addr中 
    int new_server_socket_fd = accept(server_socket_fd, (struct sockaddr*)&client_addr, &client_addr_length); 
    if(new_server_socket_fd < 0) 
    { 
      perror("Server Accept Failed:"); 
      break; 
    } 
 
    // recv函數吸收數據到緩沖區buffer中 
    char buffer[BUFFER_SIZE]; 
    bzero(buffer, BUFFER_SIZE); 
    if(recv(new_server_socket_fd, buffer, BUFFER_SIZE, 0) < 0) 
    { 
      perror("Server Recieve Data Failed:"); 
      break; 
    } 
 
    // 然後從buffer(緩沖區)拷貝到file_name中 
    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)); 
    printf("%s\n", file_name); 
 
    // 翻開文件並讀取文件數據 
    FILE *fp = fopen(file_name, "r"); 
    if(NULL == fp) 
    { 
      printf("File:%s Not Found\n", file_name); 
    } 
    else 
    { 
      bzero(buffer, BUFFER_SIZE); 
      int length = 0; 
      // 每讀取一段數據,便將其發送給客戶端,輪回直到文件讀完為止 
      while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) 
      { 
        if(send(new_server_socket_fd, buffer, length, 0) < 0) 
        { 
          printf("Send File:%s Failed./n", file_name); 
          break; 
        } 
        bzero(buffer, BUFFER_SIZE); 
      } 
 
      // 封閉文件 
      fclose(fp); 
      printf("File:%s Transfer Successful!\n", file_name); 
    } 
    // 封閉與客戶真個銜接 
    close(new_server_socket_fd); 
  } 
  // 封閉監聽用的socket 
  close(server_socket_fd); 
  return 0; 
} 

Client端代碼以下:

/************************************************************************* 
  > File Name: Client.c 
  > Author: SongLee 
 ************************************************************************/ 
 
#include<netinet/in.h>  // sockaddr_in 
#include<sys/types.h>  // socket 
#include<sys/socket.h>  // socket 
#include<stdio.h>    // printf 
#include<stdlib.h>    // exit 
#include<string.h>    // bzero 
 
#define SERVER_PORT 8000 
#define BUFFER_SIZE 1024 
#define FILE_NAME_MAX_SIZE 512 
 
int main() 
{ 
  // 聲明並初始化一個客戶真個socket地址構造 
  struct sockaddr_in client_addr; 
  bzero(&client_addr, sizeof(client_addr)); 
  client_addr.sin_family = AF_INET; 
  client_addr.sin_addr.s_addr = htons(INADDR_ANY); 
  client_addr.sin_port = htons(0); 
 
  // 創立socket,若勝利,前往socket描寫符 
  int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0); 
  if(client_socket_fd < 0) 
  { 
    perror("Create Socket Failed:"); 
    exit(1); 
  } 
 
  // 綁定客戶真個socket和客戶真個socket地址構造 非必須 
  if(-1 == (bind(client_socket_fd, (struct sockaddr*)&client_addr, sizeof(client_addr)))) 
  { 
    perror("Client Bind Failed:"); 
    exit(1); 
  } 
 
  // 聲明一個辦事器真個socket地址構造,並用辦事器那裡的IP地址及端口對其停止初始化,用於前面的銜接 
  struct sockaddr_in server_addr; 
  bzero(&server_addr, sizeof(server_addr)); 
  server_addr.sin_family = AF_INET; 
  if(inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) == 0) 
  { 
    perror("Server IP Address Error:"); 
    exit(1); 
  } 
  server_addr.sin_port = htons(SERVER_PORT); 
  socklen_t server_addr_length = sizeof(server_addr); 
 
  // 向辦事器提議銜接,銜接勝利後client_socket_fd代表了客戶端和辦事器的一個socket銜接 
  if(connect(client_socket_fd, (struct sockaddr*)&server_addr, server_addr_length) < 0) 
  { 
    perror("Can Not Connect To Server IP:"); 
    exit(0); 
  } 
 
  // 輸出文件名 並放到緩沖區buffer中期待發送 
  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中的數據 
  if(send(client_socket_fd, buffer, BUFFER_SIZE, 0) < 0) 
  { 
    perror("Send File Name Failed:"); 
    exit(1); 
  } 
 
  // 翻開文件,預備寫入 
  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_fd, buffer, BUFFER_SIZE, 0)) > 0) 
  { 
    if(fwrite(buffer, sizeof(char), length, fp) < length) 
    { 
      printf("File:\t%s Write Failed\n", file_name); 
      break; 
    } 
    bzero(buffer, BUFFER_SIZE); 
  } 
 
  // 吸收勝利後,封閉文件,封閉socket 
  printf("Receive File:\t%s From Server IP Successful!\n", file_name); 
  close(fp); 
  close(client_socket_fd); 
  return 0; 
} 

該法式備有較為詳實的正文,信任不難懂得。感興致的同伙可以在此基本上測驗考試一些功效的擴大,使其功效加倍壯大。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved