客户端仅通过C

时间:2016-08-10 15:16:24

标签: c sockets tcp

全新的StackOverflow,绝对是全新的,以确定如何让套接字在C中工作。这就是我要做的事情:我需要创建两个程序,其中一个充当“守护进程”(实际上,你只是先启动它,它在后台运行)监听指定的端口,然后另一个程序试图通过该文件发送信息 - 通过该端口 - 以及“密钥”文件的名称应该用来加密它。守护程序连接到(让我们称之为客户端)程序,分离新进程,接受信息,相应地加密文本,然后将其发送回客户端程序以存储在文件中。

最后一部分是我遇到麻烦的地方。我已经能够成功地将所有数据从客户端传输到守护程序,甚至成功加密它;我尝试将信息存储到一个临时文件中,看看它是如何出现的,一切看起来都应该如此。但是当我尝试将信息发回时 - 值得注意的是,我正在尝试将其发送回块,因为守护程序正在加密它,因此可能仍在接收信息 - 客户端之前只接收一小部分数据连接似乎已关闭,流程已完成。

对于这篇冗长的帖子感到抱歉,只是想尽可能详细。我尝试搜索有关此主题的一些帖子,但我找到了this one,但问题并不完全相同(加上建议的修复程序是我已经尝试过的内容)。以下是我认为的相关代码。这只是我当前的设置,所以某些方面可能只是来自我尝试的不同方法的随机位,例如在客户端写入关闭是最近添加,似乎没有什么区别。在过去的一周里,我尝试了很多不同的工作来完成这项工作,所以如果我提出的任何建议都是我试过的,我会让大家知道。提前谢谢!

客户端:

//Create socket, check for success
//
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1){
        fprintf(stderr, "socket creation failed");
        exit(1);
    }
//Create address structure for socket to connect to

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(port);  
    server.sin_addr.s_addr = INADDR_ANY;

    int success;
    success = connect(sockfd, (struct sockaddr*) &server, sizeof(server));
    if(success == -1){
        fprintf(stderr, "Could not find port, failed to connect socket\n");
        exit(2);
    }

//IF all is well, open up the plaintext file for extracting data

    FILE* ptextSend = fopen(argv[1], "r");
    int totalNeeded;
    rewind(ptextSend);
    fseek(ptextSend, 0L, SEEK_END);
    totalNeeded = ftell(ptextSend);
    totalNeeded = totalNeeded - 1;
    rewind(ptextSend);
//First, send key path

    send(sockfd, keypath, sizeof(keypath), 0);

//Then, send contents of file
    int n = 0;
    char buffer[1000];
    while(n < totalNeeded){
    fgets(buffer, 1000, ptextSend);
    printf("right now buffer is %s\n", buffer);
    n = n + (send(sockfd, buffer, sizeof(buffer), 0));
    }
    shutdown(sockfd, 1);
//Attempt to receive encrypted info back    
    n=1;
    while(n != 0){
    char* newBuff[1000];
    n = recv(sockfd, newBuff, sizeof(newBuff) - 1, 0);
    if(n!=0)
    fprintf(stdout, "this is the client: %s\n", newBuff);
    }

守护程序/服务器代码:

//Create socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1){
        fprintf(stderr, "Server error: failure to create socket.\n");
        exit(1);
    }

//Create address for socket
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = INADDR_ANY;

//Bind socket to port using address struct
    int bindSuccess = bind(sockfd, (struct sockaddr*) &server, sizeof(server));
    if(bindSuccess == -1){
        fprintf(stderr, "Server error: bind call failed.\n");
        exit(1);
    }

//Set socket to listen
    int listenSuccess = listen(sockfd, 5);
    if(listenSuccess == -1){
        fprintf(stderr, "Server error: listen call failed.\n");
        exit(1);
    }

//Accept next client connection
    struct sockaddr_in client_addr;
    socklen_t client_length = sizeof(client_addr);
    int client_sockfd = accept(sockfd, (struct sockaddr*) &client_addr, &client_length);
    if(client_sockfd == -1){
        fprintf(stderr, "Server error: accept call failed.\n");
        exit(1);
    }
pid_t pid = fork();

if(pid < 0){
    fprintf(stderr, "Error forking process on connection.\n");
    exit(1);
}
if(pid == 0){
//First receive key path
    char keybuff[100];
    recv(client_sockfd, keybuff, sizeof(keybuff),0);
    FILE* theKey = fopen(keybuff, "r");

    char buffer[1000];
    int n = 1;

    while(n != 0){
    n = recv(client_sockfd, buffer, sizeof(buffer) - 1, 0);

    strtok(buffer, "\n");
    char* newString = encrypt(buffer, theKey);
    send(client_sockfd, newString, sizeof(newString) - 1, 0);

    }

    if(n == -1){
        fprintf(stderr, "Server error: failed to read from socket.\n");
        exit(1);
    }

    fclose(theKey);
    close(client_sockfd);
    close(sockfd); 

    return 0;
}else
    return 0;

}

我应该回来的是“现在缓冲区是:在午夜停止的RED GOOSE FLIES”之后是加密版本,因此有36个字符的乱码(偶尔会被“这是客户端”和换行符分解) 。相反,总输出是:

现在缓冲区是在午夜停止的RED GOOSE FLIES

这是客户:BPZDOSZ

...所以只需要七个字符的数据,然后退出(成功)。

0 个答案:

没有答案