├── img ├── README.md └── File Transfer TCP.png ├── send.txt ├── Makefile ├── README.md ├── client.c └── server.c /img/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /send.txt: -------------------------------------------------------------------------------- 1 | This is a sample text file. 2 | Lets add one more line to it. 3 | -------------------------------------------------------------------------------- /img/File Transfer TCP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikhilroxtomar/File-Transfer-using-TCP-Socket-in-C/HEAD/img/File Transfer TCP.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | server: 2 | gcc server.c -o server 3 | 4 | client: 5 | gcc client.c -o client 6 | 7 | clean: 8 | rm server 9 | rm client 10 | 11 | all: server client 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File-Transfer-using-TCP-Socket-in-C 2 | A simple TCP client-server program written in C. In this program the client read a file and send its data to server. The server then receives the data and write it in a text file. 3 |

4 | Blog post: https://idiotdeveloper.com/file-transfer-using-tcp-socket-in-c/ 5 |
6 | 7 | 8 | 9 | Use the Makefile to compile the code. 10 | 15 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define SIZE 1024 7 | 8 | void send_file(FILE *fp, int sockfd){ 9 | int n; 10 | char data[SIZE] = {0}; 11 | 12 | while(fgets(data, SIZE, fp) != NULL) { 13 | if (send(sockfd, data, sizeof(data), 0) == -1) { 14 | perror("[-]Error in sending file."); 15 | exit(1); 16 | } 17 | bzero(data, SIZE); 18 | } 19 | } 20 | 21 | int main(){ 22 | char *ip = "127.0.0.1"; 23 | int port = 8080; 24 | int e; 25 | 26 | int sockfd; 27 | struct sockaddr_in server_addr; 28 | FILE *fp; 29 | char *filename = "send.txt"; 30 | 31 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 32 | if(sockfd < 0) { 33 | perror("[-]Error in socket"); 34 | exit(1); 35 | } 36 | printf("[+]Server socket created successfully.\n"); 37 | 38 | server_addr.sin_family = AF_INET; 39 | server_addr.sin_port = port; 40 | server_addr.sin_addr.s_addr = inet_addr(ip); 41 | 42 | e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); 43 | if(e == -1) { 44 | perror("[-]Error in socket"); 45 | exit(1); 46 | } 47 | printf("[+]Connected to Server.\n"); 48 | 49 | fp = fopen(filename, "r"); 50 | if (fp == NULL) { 51 | perror("[-]Error in reading file."); 52 | exit(1); 53 | } 54 | 55 | send_file(fp, sockfd); 56 | printf("[+]File data sent successfully.\n"); 57 | 58 | printf("[+]Closing the connection.\n"); 59 | close(sockfd); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define SIZE 1024 6 | 7 | void write_file(int sockfd){ 8 | int n; 9 | FILE *fp; 10 | char *filename = "recv.txt"; 11 | char buffer[SIZE]; 12 | 13 | fp = fopen(filename, "w"); 14 | while (1) { 15 | n = recv(sockfd, buffer, SIZE, 0); 16 | if (n <= 0){ 17 | break; 18 | return; 19 | } 20 | fprintf(fp, "%s", buffer); 21 | bzero(buffer, SIZE); 22 | } 23 | return; 24 | } 25 | 26 | int main(){ 27 | char *ip = "127.0.0.1"; 28 | int port = 8080; 29 | int e; 30 | 31 | int sockfd, new_sock; 32 | struct sockaddr_in server_addr, new_addr; 33 | socklen_t addr_size; 34 | char buffer[SIZE]; 35 | 36 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 37 | if(sockfd < 0) { 38 | perror("[-]Error in socket"); 39 | exit(1); 40 | } 41 | printf("[+]Server socket created successfully.\n"); 42 | 43 | server_addr.sin_family = AF_INET; 44 | server_addr.sin_port = port; 45 | server_addr.sin_addr.s_addr = inet_addr(ip); 46 | 47 | e = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); 48 | if(e < 0) { 49 | perror("[-]Error in bind"); 50 | exit(1); 51 | } 52 | printf("[+]Binding successfull.\n"); 53 | 54 | if(listen(sockfd, 10) == 0){ 55 | printf("[+]Listening....\n"); 56 | }else{ 57 | perror("[-]Error in listening"); 58 | exit(1); 59 | } 60 | 61 | addr_size = sizeof(new_addr); 62 | new_sock = accept(sockfd, (struct sockaddr*)&new_addr, &addr_size); 63 | write_file(new_sock); 64 | printf("[+]Data written in the file successfully.\n"); 65 | 66 | return 0; 67 | } 68 | --------------------------------------------------------------------------------