You are not logged in.

#1 2006-01-14 09:55:25

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

sockets

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

#2 2006-01-14 12:14:08

demonus
Member
Registered: 2005-01-31
Posts: 62

Re: sockets

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

#3 2006-01-14 12:23:07

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

Re: sockets

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 sad

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

#4 2006-01-14 13:10:29

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: sockets

major wrote:
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

#5 2006-01-14 13:22:24

demonus
Member
Registered: 2005-01-31
Posts: 62

Re: sockets

major wrote:

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 sad

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

Board footer

Powered by FluxBB