0

I want to send i file over TCP but when i try to run this the connection fails, the server receives the file but it gives this error: ERROR: Client timed out before sending a file

import selectors
import sys
from socket import *
import sock

sel1 = selectors.DefaultSelector()
print(len(sys.argv), sys.argv[1], sys.argv[2], sys.argv[3])

host = sys.argv[1]
port = int(sys.argv[2])
file = sys.argv[3]


try:
    # Instaniating socket object
    s = socket(AF_INET, SOCK_STREAM)
    # Getting ip_address through host name
    host_address = gethostbyname(host)
    # Connecting through host's ip address and port number using socket object
    s.connect((host_address, port))

    sel1.register(
        sock,
        selectors.EVENT_READ, data = None)
    fileToSend = open("file.txt", "rb")
    data = fileToSend.read(1024)
    while data:
        print("Sending...")
        fileToSend.close()
        s.send(b"Done")
        print("Done Sending")
        print(s.recv(1024))
        s.shutdown(2)
        s.close()



except:
# Returning False in case of an exception
 sys.stderr.write("Connection Failed")
5
  • 3
    You never send data to the server, you only send Done. Commented Jan 31, 2022 at 23:26
  • Why are you closing the file and socket in the loop? You shouldn't close the socket until you've finished the loop and sent the whole file. Commented Jan 31, 2022 at 23:27
  • You need to keep reading the file in the loop. Commented Jan 31, 2022 at 23:27
  • My advice is to skip the try/except until you get things working. Until that point, you WANT to see the actual exceptions, not your watered down message. Commented Jan 31, 2022 at 23:28
  • Thank You! now im undertsanding better the problem is that now it says that done is an unexpected keyword argument Commented Jan 31, 2022 at 23:37

1 Answer 1

1

Do the writing in a loop. There's no particular reason to chop it into 1024-byte pieces; the network stack will handle that for you.

By the way, your "Done" signal is not a good idea, especially since you're writing a binary file that might very well contain the word "Done". Remember that TCP is a streaming protocol. The other end does not see the exact packets you're sending. That is, just because you send 1024 bytes and 4 bytes, the other end might see it as reads of 256 and 772 bytes.

# Instaniating socket object
s = socket(AF_INET, SOCK_STREAM)
# Getting ip_address through host name
host_address = gethostbyname(host)
# Connecting through host's ip address and port number using socket object
s.connect((host_address, port))

fileToSend = open("file.txt", "rb")
print("Sending...")
while True:
    data = fileToSend.read(1024)
    if not data:
        break
    s.send( data )

fileToSend.close()
s.send(b"Done")
print("Done Sending")
print(s.recv(1024))
s.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you explanation, now the issue is that it says "No data expected until the accio command is issued!",

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.