You are not logged in.
Pages: 1
Ok, i am now learning how to do networkprograms in python. i can send and recive data.. But how do i send data ( a file or something), until all the data is sent. If i dont know the filesize (so i can set the buffersize..). i now have:
while 1:
variable = socket.recv(1024)
if not variable:
break:
However, that will require me to close the socket on the sending side, otherwise the socket on the recv-side will just listen for more data. I dont want to close any socket, but do i have to?
Offline
make up some protocol to handle this type of events, like in ftp you may initially receive the size of a file, implementing this kind of approach is quite easy. another option is to create a temporary tile and as the buffer fills up -> flush the buffer to this temp file... and continue receiving the rest of the file
Offline
Aye, but what in case of some bigass directory? i dont want to wait the minut it takes to traverse throu it to get the sizees. And also, the second method you described whould be something like this:
while 1:
var = socket.recv(buffert)
if no var:
break:
var2+=var
that is what i have so far. but i still need to close the socket
I was thinking...
Can i use a "control"-socket aswell, witch sends a "EOF" that will make the data-connection stop listening for more data, that way i dont have to open a new socket for every file, yes?
I was also wondering, hur much computerpower will go away if i have a new socket for every file?
Offline
while 1: variable = socket.recv(1024) if not variable: break:
You don't want to use any variation of this code, because its possible that the sending socket will be disconnected unexpectedly (nework errors, etc). This would leave your code waiting indefinately (until a timeout occurs), and you would never know whether all the information had been sent or not.
I'm not really sure how you could use a 'control' socket, as the two sockets would send asynchronously. You might get the 'control' EOF signal before the other socket has actually received everything from the other system. You could solve this by implementing a protocol that uses timestamps or counts the total amount of data sent or some such.
Another (better? I'm not the world's greatest network programmer) option is to put a sentinel value of some sort between each item you want to send. You can then delimit on that value. I can't remember how sockets behave if you send an EOF character (I think it might close the stream), but you might be able delimit on some special character like that.
Dusty
Offline
Aye, but what in case of some bigass directory? i dont want to wait the minut it takes to traverse throu it to get the sizees. And also, the second method you described whould be something like this:
while 1: var = socket.recv(buffert) if no var: break: var2+=var
that is what i have so far. but i still need to close the socket
I was thinking...
Can i use a "control"-socket aswell, witch sends a "EOF" that will make the data-connection stop listening for more data, that way i dont have to open a new socket for every file, yes?
I was also wondering, hur much computerpower will go away if i have a new socket for every file?
first of all you will have to traverse the directory somehow when sending it as a whole -> the other end has to know how to separate the stream into files.
moreover as dusty mentioned socket may be closed (other end has disconnected), in order to delimit the transmission send some kind of EOT message
i would do it like this
first goes the info about file -> size, name, md5sum -> then data -> at the receiving end assemble -> sending end sends EOT -> you reply with some sort of ack, more less this way
Offline
Pages: 1