├── file.txt ├── file2.txt ├── client ├── server ├── README.md ├── client.c └── server.c /file.txt: -------------------------------------------------------------------------------- 1 | Hi This is Shashi Preetham known as PSP316R -------------------------------------------------------------------------------- /file2.txt: -------------------------------------------------------------------------------- 1 | Hi This is Shashi Preetham known as PSP316R -------------------------------------------------------------------------------- /client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pshashipreetham/File-Transfer-Using-TCP-Socket-in-C-Socket-Programming/HEAD/client -------------------------------------------------------------------------------- /server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pshashipreetham/File-Transfer-Using-TCP-Socket-in-C-Socket-Programming/HEAD/server -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File Transfer Using TCP Socket in C | Socket Programming 2 | 3 | **OS:** Ubuntu 20.04 LTS
4 | **IDE:** Visual Studio Code
5 | 6 | ## Commands to be followed 7 | ### Server: 8 | Compile Server and execute: 9 | ```shell 10 | gcc -o server server.c 11 | ./server 12 | ``` 13 | ### Client: 14 | Compile Client and execute: 15 | ```shell 16 | gcc -o client client.c 17 | ./client 18 | ``` 19 | 20 | Output on **Client side** console/terminal: 21 | ```shell 22 | [+]Server socket created. 23 | [+]Connected to server. 24 | [+] File data send successfully. 25 | [+]Disconnected from the server. 26 | ``` 27 | 28 | Output on **Server side** console/terminal: 29 | ```shell 30 | [+]Server socket created. 31 | [+]Binding Successfull. 32 | [+]Listening... 33 | [+]Data written in the text file 34 | ``` 35 | 36 | **Note:**
37 | **1.** You can see the changes in the **file.txt** and **file2.txt** which are present in the repo, since that data get transferred from one file to another !!
38 | **2.** The Libraries **#include** are available only for the Linux Distros not for the windows, for windows we need to use **#include**
39 | **3.** Kindly Raise Issues if there is any problem or you can mail me at **psp316r@gmail.com** !! 40 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIZE 1024 8 | 9 | void send_file(FILE *fp, int sockfd) 10 | { 11 | char data[SIZE] = {0}; 12 | 13 | while(fgets(data, SIZE, fp)!=NULL) 14 | { 15 | if(send(sockfd, data, sizeof(data), 0)== -1) 16 | { 17 | perror("[-] Error in sendung data"); 18 | exit(1); 19 | } 20 | bzero(data, SIZE); 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | char *ip = "127.0.0.1"; 27 | int port = 8080; 28 | int e; 29 | 30 | int sockfd; 31 | struct sockaddr_in server_addr; 32 | FILE *fp; 33 | char *filename = "file.txt"; 34 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 35 | if(sockfd<0) 36 | { 37 | perror("[-]Error in socket"); 38 | exit(1); 39 | } 40 | printf("[+]Server socket created. \n"); 41 | 42 | server_addr.sin_family = AF_INET; 43 | server_addr.sin_port = port; 44 | server_addr.sin_addr.s_addr = inet_addr(ip); 45 | 46 | e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); 47 | if(e == -1) 48 | { 49 | perror("[-]Error in Connecting"); 50 | exit(1); 51 | } 52 | printf("[+]Connected to server.\n"); 53 | fp = fopen(filename, "r"); 54 | if(fp == NULL) 55 | { 56 | perror("[-]Error in reading file."); 57 | exit(1); 58 | } 59 | send_file(fp,sockfd); 60 | printf("[+] File data send successfully. \n"); 61 | close(sockfd); 62 | printf("[+]Disconnected from the server. \n"); 63 | return 0; 64 | 65 | } -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define SIZE 1024 7 | 8 | void write_file(int sockfd) 9 | { 10 | int n; 11 | FILE *fp; 12 | char *filename = "file2.txt"; 13 | char buffer[SIZE]; 14 | 15 | fp = fopen(filename, "w"); 16 | if(fp==NULL) 17 | { 18 | perror("[-]Error in creating file."); 19 | exit(1); 20 | } 21 | while(1) 22 | { 23 | n = recv(sockfd, buffer, SIZE, 0); 24 | if(n<=0) 25 | { 26 | break; 27 | return; 28 | } 29 | fprintf(fp, "%s", buffer); 30 | bzero(buffer, SIZE); 31 | } 32 | return; 33 | 34 | } 35 | 36 | int main () 37 | { 38 | char *ip = "127.0.0.1"; 39 | int port = 8080; 40 | int e; 41 | 42 | int sockfd, new_sock; 43 | struct sockaddr_in server_addr, new_addr; 44 | socklen_t addr_size; 45 | char buffer[SIZE]; 46 | 47 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 48 | if(sockfd<0) 49 | { 50 | perror("[-]Error in socket"); 51 | exit(1); 52 | } 53 | printf("[+]Server socket created. \n"); 54 | 55 | server_addr.sin_family = AF_INET; 56 | server_addr.sin_port = port; 57 | server_addr.sin_addr.s_addr = inet_addr(ip); 58 | 59 | e = bind(sockfd,(struct sockaddr*)&server_addr, sizeof(server_addr)); 60 | if(e<0) 61 | { 62 | perror("[-]Error in Binding"); 63 | exit(1); 64 | } 65 | printf("[+]Binding Successfull.\n"); 66 | 67 | e = listen(sockfd, 10); 68 | if(e==0) 69 | { 70 | printf("[+]Listening...\n"); 71 | } 72 | else 73 | { 74 | perror("[-]Error in Binding"); 75 | exit(1); 76 | } 77 | addr_size = sizeof(new_addr); 78 | new_sock = accept(sockfd,(struct sockaddr*)&new_addr, &addr_size); 79 | 80 | write_file(new_sock); 81 | printf("[+]Data written in the text file "); 82 | } --------------------------------------------------------------------------------