0

This is the client and server program where a client sends a file to server to save in the server. There is a issuse in that same file name is not getting copied on the server with same file size

Please help me in this

Client program

import socket
import sys


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost",9999))
path=raw_input("Please enter the complete PATH of your file :  ")
f=open (path, "rb") 
l = f.read(256)
while (l):
    s.sendall(l)
    l = f.read(10000)
s.close()

Server Program

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost",9999))
s.listen(10) 

while True: 
    s, address = s.accept()

    print address
    i=1
    f = open( str(i),'wb') #open in binary
    #i=i+1
    while (True):       

        l=s.recv(256)
        #while (l):
        f.write(l)
        l=s.recv(256)
        print 'File recieve succesfully'
f.close()
#sc.close()
s.close()

Thanks in advance

3
  • 1
    You mean your question about shutil with completely different code? Your main problem is you never send the filename from the client to the server. Where the server opens a file called "1" to write, it should be opening a file with the correct name. Only the client knows the correct name, so it has to somehow tell the server.
    – Useless
    Commented May 31, 2014 at 10:49
  • i know that it takes 1 but how should i tell the server to copy that exact same file which i am sending from the client to server thats where i stuck and the file i send to server is around 600kb but it saves only 248kb any solutions on that Commented May 31, 2014 at 10:56
  • 1
    This question appears to be off-topic because it is about an implementation issue. It would be better asked on other SE sites but doesn't provide enough information to diagnose the problem.
    – user1345223
    Commented May 31, 2014 at 19:07

1 Answer 1

0

Start by walking through the code and thinking about what the client knows about the data it is sending and what the server knows about the data it is receiving. You will have to send 2 types of messages: the data and the filename. How you do that is up to you.

Without over-thinking it, maybe try writing the filename first (followed by a newline or special character) then send the file data. On the server side accept the connection, read in data until you find a newline character (that's the filename), then receive the rest of the data and write it to the file.

Also, the server code you've provided doesn't work, at least I don't think, since you never break out of your while True loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.