简体   繁体   English

从客户端C发送套接字,服务器Python问题

[英]Socket Send from Client C, Server Python Issue

My current application involves a C client sending a file to a Python server using TCP. 我当前的应用程序涉及一个C客户端,该客户端使用TCP将文件发送到Python服务器。 It will produce a hash of the file and send the information back. 它将产生文件的哈希并将信息发送回去。 I can get it to work with a python client but I am having issues migrating the client into C. The python server is still unfinished (it needs to convert the file size string to int, error checking, etc). 我可以将其与python客户端一起使用,但是在将客户端迁移到C时遇到问题。python服务器仍未完成(它需要将文件大小字符串转换为int,错误检查等)。

My biggest issue right now is after the server calls hash_type = connbuff.get_utf8(), it gives me the first user input of the hash type, then closes the connection. 我现在最大的问题是服务器调用hash_type = connbuff.get_utf8()之后,它为我提供了哈希类型的第一个用户输入,然后关闭了连接。 I know it is an issue with get_utf8(), but I am stumped on how to fix this. 我知道这是get_utf8()的问题,但我对如何解决此问题感到困惑。 Should I just send arbitrary amounts of data from the client every time? 我是否应该每次都仅从客户端发送任意数量的数据? Please help me learn from the error of my ways! 请帮助我从我的错误中学习! Any advice/suggestions are much appreciated! 任何建议/建议,不胜感激! Thanks =) 谢谢=)

Server.py Server.py

import socket
import os
import hashlib
import buffer

HOST = '127.0.0.1'
PORT = 2345

def getHash(fileName, hashType):
    ... hash algorithms ....

try:
    os.mkdir('uploads')
except FileExistsError:
    pass

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
print("Waiting for a connection.....")

while True:
    conn, addr = s.accept()
    print("Got a connection from ", addr)
    connbuf = buffer.Buffer(conn)

while True:
    hash_type = connbuf.get_utf8()
        if not hash_type:
            break
        print('Hash Type: ', hash_type)

        file_name = connbuf.get_utf8()
        if not file_name:
            break
        print('File Name: ', file_name)
        file_name = os.path.join('uploads', file_name)

        file_size = connbuf.get_utf8()
        file_size = int(file_size)
        print('File Size: ', file_size)

        with open(file_name, 'wb') as f:
            remaining = file_size
            while remaining:
                if remaining >= 4096:
                    chunk_size = 4096
                else:
                    chunk_size = remaining
                chunk = connbuf.get_bytes(chunk_size)
                if not chunk:
                    break
                f.write(chunk)
                remaining -= len(chunk)

            if remaining:
                print('File incomplete: MISSING', remaining, 'BYTES.')
            else:
                print('File received successfully.')

        file_hash = getHash(file_name, hash_type)
        response = file_name + ' ' + file_hash
        connbuf.put_utf8(response)

print('Connection closed.')
conn.close()

My Buffer class get_utf8() and get_bytes() look like this... 我的Buffer类get_utf8()和get_bytes()看起来像这样...

def __init__(self,s):
    self.sock = s
    self.buffer = b''

def get_bytes(self,n):
    while len(self.buffer) < n:
        data = self.sock.recv(1024)
        if not data:
            data = self.buffer
            self.buffer = b''
            return data
        self.buffer += data

    data,self.buffer = self.buffer[:n],self.buffer[n:]
    return data

def get_utf8(self):
    while b'\x00' not in self.buffer:
        data = self.sock.recv(1024)
        if not data:
            return ''
        self.buffer += data

    data,_,self.buffer = self.buffer.partition(b'\x00')
    return data.decode()

Client.c Client.c

#include <sys/socket.h>
  ... more includes ...

#define PORT_NUMBER 2345
#define SERVER_ADDRESS "127.0.0.1"

char *inputString(FILE* fp, size_t size){
    ... string input code ...
}

int main () {
    int server_socket, connection_status;
    struct sockaddr_in serverAddress;
    char *hashType;
    char *fileName;
    char send_buffer[4000];
    FILE * file_to_send;
    int file_size;

/* Connect to Server */
    ... connect to server code ...

/* Get Hash an File User Input */
printf("Enter hash type: ");
hashType = inputString(stdin, 10);
printf("Hash type: %s\n", hashType);

printf("Enter file name: ");
fileName = inputString(stdin, 10);
printf("File Name: %s\n");

/* Send User Input */
send(server_socket, hashType, sizeof(hashType), 0);
send(server_socket, fileName, sizeof(fileName), 0);

/* Open File, Get Size, Convert to String */
file_to_send = fopen(fileName, "rb");
fseek(file_to_send, 0, SEEK_END);
file_size = ftell(file_to_send);
fseek(file_to_send, 0, SEEK_SET);

int l = snprintf(NULL, 0, "%d", file_size);
char *str_file_size;
asprintf(&str_file_size, "%i", file_size);
printf("%s\n", str_file_size);

/* Send File Size and File */
send(server_socket, str_file_size, sizeof(str_file_size), 0);

while(!feof(file_to_send)) {
    fread(send_buffer, 1, sizeof(send_buffer) - 1, file_to_send);
}
send(server_socket, send_buffer, sizeof(send_buffer), 0);

return 0;

} }

get_utf8 expects to read a null-terminated UTF-8-encoded string from the socket. get_utf8希望从套接字读取以null终止的UTF-8编码的字符串。 In the C code, you send sizeof(hashType) . 在C代码中,您发送sizeof(hashType) hashType is a pointer, so you are sending exactly 4 or 8 bytes (depending on 32- or 64-bit architecture). hashType是一个指针,因此您恰好要发送4或8个字节(取决于32位或64位体系结构)。 You probably need strlen(hashType)+1 (+1 for the NULL). 您可能需要strlen(hashType)+1 (NULL为+1)。 Ditto for filename. 与文件名相同。

get_utf8 also reads until it sees a null. get_utf8也会读取直到看到空值。 If it never sees one, it returns empty string, which then causes the receive code to break and closes the connection. 如果从未看到,则返回空字符串,这将导致接收代码中断并关闭连接。

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

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