从客户端C发送套接字,服务器Python问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从客户端C发送套接字,服务器Python问题相关的知识,希望对你有一定的参考价值。
我当前的应用程序涉及C客户端使用TCP将文件发送到Python服务器。它将生成文件的哈希值并将信息发回。我可以让它与python客户端一起使用,但我遇到了将客户端迁移到C的问题.python服务器仍未完成(它需要将文件大小字符串转换为int,错误检查等)。
我现在最大的问题是在服务器调用hash_type = connbuff.get_utf8()之后,它给了我第一个哈希类型的用户输入,然后关闭连接。我知道这是get_utf8()的一个问题,但我很难解决这个问题。我应该每次只从客户端发送任意数量的数据吗?请帮助我从错误的方式中学习!任何建议/建议都非常感谢!谢谢=)
server.朋友
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类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
#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
", hashType);
printf("Enter file name: ");
fileName = inputString(stdin, 10);
printf("File Name: %s
");
/* 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
", 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
希望从套接字读取一个以null结尾的UTF-8编码字符串。在C代码中,您发送sizeof(hashType)
。 hashType
是一个指针,因此您正在发送4或8个字节(取决于32位或64位架构)。您可能需要strlen(hashType)+1
(NULL为+1)。同上文件名。
get_utf8
也读,直到它看到null。如果它从未看到一个,它返回空字符串,然后导致接收代码中断并关闭连接。
以上是关于从客户端C发送套接字,服务器Python问题的主要内容,如果未能解决你的问题,请参考以下文章