简体   繁体   中英

Socket TCP/IP in C passing files and using Caesar cipher

I am doing a project in which I need to pass files through a TCP socket at the same time I have to encrypt the text of the file using Caesar Cypher, but I am having an error like you can see in the image above, the error is "assigment to expression with array type" but I have a several warnings that I think are due to the error I have can you help me to solve this?

I have the int main complete and working because I can send the file without a problem, but I couldn't put the whole code here because I was giving an error in creating the question, so the int main is as an image

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024

 void send_file(FILE *fp, int sockfd){
      int n;

      char data[BUF_SIZE] = {0};

      while(fgets(data, BUF_SIZE, fp) != NULL) {
        if (send(sockfd, data, sizeof(data), 0) == -1) {
          perror("Error in sending file.");
          exit(1);
        }
        bzero(data, BUF_SIZE);
      }
    }
    void CaeserCypher(FILE *fp2, int key){
    int i =0;
    int cypherValue;
    char cypher[BUF_SIZE]={0};

    while(fgets(cypher, BUF_SIZE, fp2) != NULL){
        cypherValue =((int)cypher[i]- 97 + key)%26 + 97;
        cypher = cypherValue;

        fprintf("%c", cypher);
        i++;
    }
    bzero(cypher, BUF_SIZE);
}


int main(){
  char *ip = "127.0.0.1";
  int port = 9000;
  int e;

  int sockfd;
  struct sockaddr_in server_addr;
  FILE *fp, *fp2;
  char *filename = "exemplo.txt";
  int key=1;

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if(sockfd < 0) {
    perror("Error in socket");
    exit(1);
  }
  printf("Server socket created successfully.\n");

  server_addr.sin_family = AF_INET;
  server_addr.sin_port = port;
  server_addr.sin_addr.s_addr = inet_addr(ip);

  e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  if(e == -1) {
    perror("Error in socket");
    exit(1);
  }
    printf("Connected to Server.\n");


  fp2 = fopen(filename, "w");
  if (fp == NULL) {
    perror("Error in reading file.");
    exit(1);
  }
  void CaeserCypher(fp2, key);
  fclose(fp2);

  fp = fopen(filename, "r");
  send_file(fp, sockfd);
  printf("File data sent successfully.\n");

    printf("Closing the connection.\n");
  close(sockfd);

  return 0;
}

int main error in code `

When you declared cypher you declared it as a char array with a size of 1024. This is different than just a char. You can't just assign a value (like cypherValue) to an array. You need to assign it to a member of that array using an index. You would do that with something like "cypher[index] = cypherValue;".

Also, the fgets function reads an array of chars from the file and puts them into the buffer that's passed to it (cypher). Your code in its current state would only encrypt a single char in that array before (attempting) to write it back to the file.

The reason why I say attempting to write it back to the file is because your call to fprintf would not work. Unlike printf, you need to pass a FILE* as the first argument before passing the format string. Since cyppher is an array of chars and not just a single char you need to use "%s" as the format string.

You can read more about the fprintf function here: https://www.tutorialspoint.com/c_standard_library/c_function_fprintf.htm

There's also a few errors with your main function. On line 65 you open fp2 in write mode using "w". This will delete the contents of the file and won't let you read the file for encrypting. Here is a reference for the fopen function: https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm

Finally, on line 66 fp should be fp2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM