In Linux systems, C/C++ can be used for socket programming to send and receive files via TCP or UDP protocols. Below, I'll demonstrate a simple example of using the TCP protocol (which provides reliable, connection-oriented data transmission) for file transfer.
1. Server-side Program
The server-side task is to listen on a port, accept client connections, read the file data sent by the client, and write it to local storage. Below is a simple server-side example code:
c#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #define PORT 8080 #define BUFFER_SIZE 1024 int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); char buffer[BUFFER_SIZE] = {0}; char *filename = "received_file.txt"; // Create socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Bind socket to localhost's 8080 port address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen on this port if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } // Accept an incoming connection if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept"); exit(EXIT_FAILURE); } // Read data and write to file FILE *file = fopen(filename, "wb"); int bytes_read; while ((bytes_read = read(new_socket, buffer, BUFFER_SIZE)) > 0) { fwrite(buffer, 1, bytes_read, file); } fclose(file); close(new_socket); close(server_fd); return 0; }
2. Client-side Program
The client-side task is to connect to the server, read local file data, and send the data to the server. Below is the corresponding client-side example code:
c#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #define PORT 8080 #define BUFFER_SIZE 1024 int main() { int sock = 0; struct sockaddr_in serv_addr; char *filename = "file_to_send.txt"; char buffer[BUFFER_SIZE] = {0}; // Create socket file descriptor if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("\n Socket creation error \n"); return -1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 address from text to binary form if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { printf("\nInvalid address/ Address not supported \n"); return -1; } // Connect to server if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("\nConnection Failed \n"); return -1; } // Read and send file content FILE *file = fopen(filename, "rb"); int bytes_read; while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, file)) > 0) { write(sock, buffer, bytes_read); } fclose(file); close(sock); return 0; }
Explanation
- Server-side uses the
socket(),bind(),listen(), andaccept()functions to set up the TCP server and accept client connections. - Client-side uses the
socket()andconnect()functions to establish a connection to the server. - File data is transmitted through sockets in binary format.
- The
read()andwrite()functions facilitate data transfer between sockets and files.
This is a basic example; in practical applications, you may need to handle errors, enhance security, or improve performance.