[text] file transfer server

Viewer

copydownloadembedprintName: file transfer server
  1. #include<stdio.h>
  2. #include<arpa/inet.h>
  3. #include<sys/types.h>
  4. #include<sys/socket.h>
  5. #include<netinet/in.h>
  6. #include<netdb.h>
  7. #include<stdlib.h>
  8. #include<string.h>
  9. #include<unistd.h>
  10. #define SERV_TCP_PORT 5035
  11. #define MAX 60
  12. int i, j, tem;
  13. char buff[4096], t;
  14. FILE *f1;
  15. int main(int afg, char *argv)
  16. {
  17.        int sockfd, newsockfd, clength;
  18.        struct sockaddr_in serv_addr,cli_addr;
  19.        char t[MAX], str[MAX];
  20.        strcpy(t,"exit");
  21.        sockfd=socket(AF_INET, SOCK_STREAM,0);
  22.        serv_addr.sin_family=AF_INET;
  23.        serv_addr.sin_addr.s_addr=INADDR_ANY;
  24.        serv_addr.sin_port=htons(SERV_TCP_PORT);
  25.        printf("\nBinded");
  26.        bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
  27.        printf("\nListening...");
  28.        listen(sockfd, 5);
  29.        clength=sizeof(cli_addr);
  30.        newsockfd=accept(sockfd,(struct sockaddr*) &cli_addr,&clength);
  31.        close(sockfd);
  32.        read(newsockfd, &str, MAX);
  33.        printf("\nClient message\n File Name : %s\n", str);
  34.        f1=fopen(str, "r");
  35.        while(fgets(buff, 4096, f1)!=NULL)
  36.        {
  37.             write(newsockfd, buff,MAX);
  38.             printf("\n");
  39.        }
  40.        fclose(f1);
  41.        printf("\nFile Transferred\n");
  42.        return 0;
  43. }

Editor

You can edit this paste and save as new:


File Description
  • file transfer server
  • Paste Code
  • 22 Apr-2024
  • 1.23 Kb
You can Share it: