简体   繁体   English

C 中的套接字 TCP/IP 传递文件并使用凯撒密码

[英]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?我正在做一个项目,其中我需要通过 TCP 套接字传递文件,同时我必须使用 Caesar Cypher 加密文件的文本,但是我遇到了一个错误,就像您在上图中看到的错误是“用数组类型分配表达式”,但我有几个警告,我认为是由于我遇到的错误,你能帮我解决这个问题吗?

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我有完整的 int main 并且正在工作,因为我可以毫无问题地发送文件,但是我不能把整个代码放在这里,因为我在创建问题时出错了,所以 int main 是一个图像

#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 `代码中的int 主要错误`

When you declared cypher you declared it as a char array with a size of 1024. This is different than just a char.当您声明 cypher 时,您将其声明为大小为 1024 的 char 数组。这与仅 char 不同。 You can't just assign a value (like cypherValue) to an array.您不能只为数组分配一个值(如 cypherValue)。 You need to assign it to a member of that array using an index.您需要使用索引将其分配给该数组的成员。 You would do that with something like "cypher[index] = cypherValue;".你可以用“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).此外,fgets function 从文件中读取一个字符数组并将它们放入传递给它的缓冲区(密码)。 Your code in its current state would only encrypt a single char in that array before (attempting) to write it back to the file.您当前 state 中的代码在(尝试)将其写回文件之前只会加密该数组中的单个字符。

The reason why I say attempting to write it back to the file is because your call to fprintf would not work.我说尝试将其写回文件的原因是因为您对 fprintf 的调用不起作用。 Unlike printf, you need to pass a FILE* as the first argument before passing the format string.与 printf 不同,您需要在传递格式字符串之前传递 FILE* 作为第一个参数。 Since cyppher is an array of chars and not just a single char you need to use "%s" as the format string.由于 cyppher 是一个字符数组,而不仅仅是一个字符,因此您需要使用 "%s" 作为格式字符串。

You can read more about the fprintf function here: https://www.tutorialspoint.com/c_standard_library/c_function_fprintf.htm您可以在此处阅读有关 fprintf function 的更多信息: https://www.tutorialspoint.com/c_standard_library/c_function_fprintf.htm

There's also a few errors with your main function.您的主要 function 也存在一些错误。 On line 65 you open fp2 in write mode using "w".在第 65 行,您使用“w”以写入模式打开 fp2。 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这是 fopen function 的参考: https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm

Finally, on line 66 fp should be fp2.最后,第 66 行 fp 应该是 fp2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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