3

I have been able to receive the file from the socket and download it, but when I try to push a message from the server to the client the message is never displayed on the client side.

Below is the code and any help would be highly appreciated as I am a novice to network programming.

# get the hostname
host = socket.gethostname()
port = 5000  # initiate port no above 1024
Buffer = 1024

server_socket = socket.socket()  # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port))  # bind host address and port together


# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept()  # accept new connection
print("Connection from: " + str(address))
f = open("FileFromServer.txt", "wb")
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(Buffer)
while data:
        f.write(data)
        print("from connected user: " + str(data))
        data = conn.recv(Buffer)

f.close() 

print 'Data Recivede'
datas = 'Recived the file Thanks'
if datas is not '':
    conn.send(datas)  # send data to the client

conn.close()  # close the connection





host = socket.gethostname()  # as both code is running on same pc
port = 5000  # socket server port number

client_socket = socket.socket()  # instantiate
client_socket.connect((host, port))  # connect to the server



with open('T.txt', 'rb') as f:

    print 'file openedfor sending'
    l = f.read(1024)
    while True:
        client_socket.send(l)
        l = f.read(1024)

f.close()

print('Done sending')

print('receiving data...')
data = client_socket.recv(1024)
print data

client_socket.close()  # close the connection
print 'conection closed
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
FDsouza
  • 31
  • 1

1 Answers1

0

The thing is that you both you server and client socket stuck in the while loop:

try this client.py:

import socket

host = socket.gethostname()  # as both code is running on same pc
port = 5000  # socket server port number

client_socket = socket.socket()  # instantiate
client_socket.connect((host, port))  # connect to the server

end = '$END MARKER$'

with open('T.txt', 'rb') as f:
    print('file opened for sending')
    while True:
        l = f.read(1024)
        if len(l + end) < 1024:
            client_socket.send(l+end)
            break
        client_socket.send(l)

print('Done sending')

print('receiving data...')
data = client_socket.recv(1024)

print(data)

client_socket.close()  # close the connection
print('conection closed')

server.py

import socket

# get the hostname
host = socket.gethostname()
port = 5000  # initiate port no above 1024
Buffer = 1024

end = '$END MARKER$'

server_socket = socket.socket()  # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port))  # bind host address and port together

# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept()  # accept new connection
print("Connection from: " + str(address))
# receive data stream. it won't accept data packet greater than 1024 bytes
with open("FileFromServer.txt", "ab") as f:
    while True:
        data = conn.recv(Buffer)
        if end in data:
            f.write(data[:data.find(end)])
            conn.send(b'Recived the file Thanks')
            break
        f.write(data)

conn.close()
ZhouQuan
  • 1,037
  • 2
  • 11
  • 19
  • Hi, I tried running your code above but I am getting an error "error: [Errno 10053] An established connection was aborted by the software in your host machine" and also the complete file wasn't downloaded at the server. The server also could not send a response back. – FDsouza Feb 28 '18 at 12:57
  • I also modified a bit so that at the end of the file it moved out of the loop and the file was completely downloaded but the return message from the server to the client was not executed – FDsouza Feb 28 '18 at 13:17
  • @FDsouza referenece:https://stackoverflow.com/questions/1708835/python-socket-receive-incoming-packets-always-have-a-different-size In client.py, I didnt include all the cases where l+end could be bigger or equal the buffer size – ZhouQuan Feb 28 '18 at 13:59