Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to make a server and client which sends a file from client to server and the server saves it to hard then the server asks for another file and if the answer of client is yes then the client sends the second file then the server again saves it and if the client answer is no server close the socket when i run this code the first file is sent
and received successfully but after that both of the server and the client freeze and nothing happens what is wrong with it and how can i fix it?

What I have tried:

my server code:

import socket

host = 'localhost'

port = 4444

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host, port))

s.listen(5) 

(client, (ip, port))=s.accept()


while True:

    data = "".join(iter(lambda: client.recv(1), "\n"))

with open('filehere.txt', 'w') as file:



    for item in data:

        file.write("%s" % item)

    if not data: break


client.send("is there any other file?")

d = client.recv(2048)

if d == "yes":

    while True:

        data = "".join(iter(lambda: client.recv(1), "\n")

        with open('filehere1.txt', 'w') as file:

             for item in data:

                 file.write("%s" % item)

        if not data: break

        s.close()

else:

    s.close()


my client code:

import socket

host = 'locahost'

port = 4444

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((host, port))


f = open('myfile.txt', 'rb')

l = f.read()

while True:

    for line in l:

        s.send(line)

    break

f.close()

d = s.recv(2048)

a = raw_input(d)


if a == "yes":

    s.send("yes")

    f = open('myfile1', 'rb')

    l = f.read()

    while True:

        for line in l:

            s.send(line)
        break

    f.close()

else:

    s.close()
Posted
Comments
Richard MacCutchan 16-Apr-19 11:52am    
It is quite possible that the two ends are getting confused about which one should send the next message, so they are each waiting for the peer to respond.
amir78 16-Apr-19 14:17pm    
what should i do to solve it?do you have any suggestion?
Richard MacCutchan 16-Apr-19 15:40pm    
You need the client to send information with each message to tell the server whether it is the last message or not.
amir78 16-Apr-19 23:33pm    
could you explain more?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900