Question
I am writing a Socket program to transfer image files from a client to the server. Right now I have a working program using TCP.
I am writing a Socket program to transfer image files from a client to the server. Right now I have a working program using TCP. It transfers .txt files and also image files. But the thing with image file is it only gives me like 75% of the original image(cropped at the bottom) once received by the server, How can I get 100% the same image as I sent from the client side. Please look at my code and edit it where it needs to be edited. Note that the image file has to be transferred from CLIENT to SERVER. I have attached both screenshots for any indent issues.
This is how I've been working on it.
1. create a folder in server pc called "server"
2. create a folder in client pc called "client"
3. also include the image file you want to transfer in the same "client" folder
4. client side ask "name of the file you want to send", I type in the name of the image file in the folder
5. server side ask me "name of the file you want to receive", I type any random name.
6. the file transfers and gets saved to "server" folder in the server pc.
############################Server.py##############################################
from socket import *
def create_socket(server_port): server_socket = socket(AF_INET, SOCK_STREAM) server_socket.bind(('', server_port)) server_socket.listen(1) return server_socket
def recv_message(server_socket, buffer_size): file = open(filename, 'wb') connection_socket, address = server_socket.accept() file_data= connection_socket.recv(buffer_size) file.write(file_data) file.close() text=("file recieved") return text
server_port = input("Please, enter TCP connection port number: ") server_socket = create_socket(int(server_port))
while (True): filename=input(str("enter filename for the incomming file:")) incoming_message = recv_message(server_socket, 1048576) print ("{}" .format(incoming_message)) ##################################Client.py###########################################
from socket import *
def establish_connection(server_IP, server_port): client_socket = socket(AF_INET, SOCK_STREAM) client_socket.connect((server_IP, server_port)) return client_socket
def send_recv_message(client_socket, message, buffer_size): file = open(filename,'rb') file_data = file.read(buffer_size) client_socket.send(file_data) text=print("File transfered") return text
server_IP = input("Please, enter server IP address: ") server_port = input("Please, enter server TCP connection port number: ") client_socket = establish_connection(server_IP, int(server_port)) print("Connection established ")
filename= input(str("enter filename"))
received_message = send_recv_message(client_socket,filename,1048576)
### Server ### from socket import * def create_socket (server port): server_socket = socket (AF_INET, SOCK_STREAM) server socket.bind(", server port)) server socket.listen (1) return server_socket def recv_message (server_socket, buffer_size): file = open(filename, 'wb') connection_socket, address = server_socket.accept() file_data= connection_socket.recv (buffer_size) file.write(file data) file.close() text=("file recieved") return text server port = input ("Please, enter TCP connection port number: ") server_socket = create_socket (int (server_port)) while (True): filename=input (str("enter filename for the incomming file:")) incoming_message = recv_message (server_socket, 1048576) print ("{}" .format(incoming_message)) ##client## from socket import * def establish_connection (server_IP, server port): client_socket = socket (AF_INET, SOCK_STREAM) client_socket.connect((server_IP, server_port)) return client socket def send_recv_message (client_socket, message, buffer_size): file = open(filename, 'rb') file data = file.read (buffer size) client_socket.send (file_data) text=print("File transfered") return text server_IP = input ("Please, enter server IP address: ") server_port = input ("Please, enter server TCP connection port number: ") client_socket = establish_connection (server_IP, int (server_port)) print ("Connection established ") filename= input (str("enter filename")) received_message = send_recv_message (client_socket, filename, 1048576) ### Server ### from socket import * def create_socket (server port): server_socket = socket (AF_INET, SOCK_STREAM) server socket.bind(", server port)) server socket.listen (1) return server_socket def recv_message (server_socket, buffer_size): file = open(filename, 'wb') connection_socket, address = server_socket.accept() file_data= connection_socket.recv (buffer_size) file.write(file data) file.close() text=("file recieved") return text server port = input ("Please, enter TCP connection port number: ") server_socket = create_socket (int (server_port)) while (True): filename=input (str("enter filename for the incomming file:")) incoming_message = recv_message (server_socket, 1048576) print ("{}" .format(incoming_message)) ##client## from socket import * def establish_connection (server_IP, server port): client_socket = socket (AF_INET, SOCK_STREAM) client_socket.connect((server_IP, server_port)) return client socket def send_recv_message (client_socket, message, buffer_size): file = open(filename, 'rb') file data = file.read (buffer size) client_socket.send (file_data) text=print("File transfered") return text server_IP = input ("Please, enter server IP address: ") server_port = input ("Please, enter server TCP connection port number: ") client_socket = establish_connection (server_IP, int (server_port)) print ("Connection established ") filename= input (str("enter filename")) received_message = send_recv_message (client_socket, filename, 1048576)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