Question
I am working on a program that transfers image file from client to server. I have a working code. But my code only transfers .png
I am working on a program that transfers image file from client to server. I have a working code. But my code only transfers .png to .png even if I try and send any other file extension and receive in the same extension. the code below has .png in it but I've tried doing .jpg on both client and server side and send the .jpg file but it doesn't work.
Here is my code.
####Server###
from socket import * import select import time
buffer_size = 4096 connected_clients_sockets = [] HOST = '192.168.0.5' imgcounter = 1 basename = "image%d.png"
def create_socket(server_port): server_socket_conn = socket(AF_INET, SOCK_STREAM) server_socket_conn.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server_socket_conn.bind((HOST, server_port)) server_socket_conn.listen(10) connected_clients_sockets.append(server_socket_conn) return server_socket_conn
server_port = input("Please, enter TCP connection port number: ") server_socket = create_socket(int(server_port))
while (True): read_sockets, write_sockets, error_sockets = select.select(connected_clients_sockets, [], []) for sock in read_sockets:
if sock == server_socket: sockfd, client_address = server_socket.accept() connected_clients_sockets.append(sockfd) else: try: print("Buffer size is %d" % buffer_size)
data = sock.recv(buffer_size) print("------- Received Data ----------") print(data) print("---------------------------------") if data.startswith(b"Size"): tmp = data.decode("utf-8").split() size = int(tmp[1]) print(f"Got size {size}") sock.sendall(b"Got size %d data" % size) buffer_size = 40960000 elif data.startswith(b"Bye"): print("Bye") sock.shutdown() elif data: file = open(basename % imgcounter, 'wb') file.write(data) file.close() print("Got Image") sock.sendall(b"Got Image") buffer_size = 4096 sock.shutdown() file.write(data) file.close() sock.close() except Exception as exc: sock.close() connected_clients_sockets.remove(sock) continue imgcounter += 1 server_socket.close() ##########client#####
from socket import * import select
HOST = '192.168.0.5'
def establish_connection(server_port): client_socket = socket(AF_INET, SOCK_STREAM) client_socket.connect((HOST, server_port)) return client_socket
def send_recv_message(client_socket, message): file = open(filename,'rb') file_data = file.read() size = len(file_data)
client_socket.sendall(b"Size %d" % size)
response = client_socket.recv(4096) print(f"Response: {response}")
if response.startswith(b"Got size"):
client_socket.sendall(file_data) print("File transfer started...")
response = client_socket.recv(4096) print(f"Response: {response}") if response.decode("utf-8") == "Got Image": client_socket.sendall(b"Bye Bye") print("Image successfully transfered")
server_port = input("Please, enter server TCP connection port number: ") client_socket = establish_connection(int(server_port)) print("Connection established ")
filename = "image.png" # input("Enter filename to be sent: ")
send_recv_message(client_socket, filename)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started