×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    Python connection.

    Hi,
    Maybe someone can explain me this hard,but stupid,problem.

    i have a client written in python for file tranfer(WLAN) and do this:
    1)Request file size.
    2)Do a progress bar for Send/Total.
    3)Get pieces of file.

    The protocol is this:
    client(python) say : s.send('SCR').
    Server(VB6) answer: SIZE of file.
    client say: s.send('SEN') and client start get data with Buffer=s.recv(8192).

    Well...when client get SIZE this is NULL or a character...
    If i do:
    client(python) say : s.send('SCR').
    Server(VB6) answer: SIZE & vbcrlf
    client say: s.send('SEN') and client start get data with Buffer=s.recv(8192).

    All it's ok but the 2nd time,in the same exectuion,i no get SIZE value...

    Can anyone explain me? there is a problem in s.recv,but i no understand.

    Piece:
    Code:
       s.send('SCR')
       print '----Screen Download----'
       Dati=0
       Tot = s.recv(8192)
       print Tot
       Screen = open('c:/screen.jpg','wb')
       s.send('SEN')
       while int(Tot)>Dati:  <-Error some time
        Buffer=s.recv(8192)
       ...
       ...

    Edit:
    The problem is "Tot = s.recv(8192)" sometime no pause the program for wait data...why?
    Last edited by ixiodor; 2009-12-29 at 15:52.

  2. #2
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Python connection.

    Hi ixiodor, fine ?

    Please, take care with send() and recv() calls. TCP is byte oriented, not message oriented. So, network and operating system conditions (delay, buffers, throughput and so on) may change the amount of bytes received at each call to recv(). When you write s.recv(8192) you are saying that you can receive at most 8192 bytes, however you can receive *any* amount of bytes from 0 to 8192. Event hough the streaming is guaranteed by TCP, the programmer must check the amount of bytes sent or received by his function, splitting when necessary, waiting more chunks when something is still missing and so on.

    Moreover, when sending the size to client, which encondig did you use ? send() only handle bytes so you need to define your way. For instance, you can send a number as a string ( send(str(123) ) and decode it properly at recv(). Or you can code it as a four bytes long using big endian memory alignment. The last option is better (even though more complicated) since a fixed amount of bytes is always expected and you can communicate with little and big endian platforms. struct module can help in this task. I recommend to take a look in this program as well.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    Re: Python connection.

    Hi and thanks for your answer.But problem is the same...When i click "send file" all go fine,If i click another time,in the same execution,"send file" i get this error.It's a logic problem.

    I Try this:
    Send "SCR" from client.
    In server i blocked the send of the SIZE and client get error without data sending...Python no wait for a data_arrival...sometime...
    Code:
    #---------------Download Screen------------------------
    def DownScreen():
       s.send('SCR')                      #Send HandShake
       Tot = s.recv(8192)                #Wait for the Total of Byte to recive(Some time him no wait)
       print u'----Screen Download----'
       print Tot                          #For debug i wanna see what recived.
       Dati=0
       Screen = open('c:/screen.jpg','wb')#Open file
       s.send('SEN')                      #I am ready,send the file.
       Tot=int(Tot)                       #Convert Str in Int(and give error there because '' can't be converted to int)
       while Tot!=Dati:                   #Get all Data
        if Tot-Dati<8192:
         Buffer=s.recv(Tot-Dati)          #If what i need is <8192B resize the buffer
        else:
         Buffer=s.recv(8192)              #Else get 8192 B.
        Screen.write(Buffer)              #Write Bytes
        Dati=Dati + len(Buffer)           #How much you send me?
        Perc=(float(Dati)/float(Tot))*100 #Progress
        Perc=str(round(Perc,0))+"%"       #//
        t=appuifw.Text()                  #//
        appuifw.app.body=t                #//
        t.set(u"Download: "+ Perc)        #//
       Screen.close()                     #File Send,Close Handle
       print u'-----Screen End-----'     
       appuifw.app.body = lb              #Show Me Principal Menu
    #-------------------------------------------------------
    This is the Function. The program no have a reason for no wait data.(The 1° time go ever,if i wanna do a new download,recv no wait,in the same execution)
    Last edited by ixiodor; 2009-12-30 at 01:03.

  4. #4
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Python connection.

    Quote Originally Posted by ixiodor View Post
    Hi and thanks for your answer.But problem is the same...When i click "send file" all go fine,If i click another time,in the same execution,"send file" i get this error.It's a logic problem.
    Sorry, ixiodir, but this code will not work as you expect. Please read again my considerations and the links I sent. Some good socket stuff will help you as well. You need to rewrite the code, unfortunately.

    For instance, there is no guarantee that s.send('SCR') will send all bytes in a unique call to send(). It can send one byte, returning 1, and you need to check the result and send the remaining bytes. So, use sendall() instead send().

    When you write Tot = s.recv(8192) you believe that you will receive a message with all bytes used in server when it called send(str(number)). But you can not have sure that this behavior will happen always. Again: tcp is streaming oriented, not message oriented.

    Worst, when you decide to send a number as its ascii representation, you do not have a way to determine when the number arrived or not. You need to use a fix amount of bytes to represent the number or some kind of token/field separator. Imagine that you call send(str(12345)) and after you call send("qwerty"). At client side, your call to recv(8192) *may* receive "12345qwerty".

    I am not sure if I am being clear. The problem is not related to python but to socket concepts.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    Re: Python connection.

    I do programs from 8 years...i know how work TCP protocol and "Nagle's algorithm" isn't in this case...i recive "12345qwerty" if i send two messagge in a time < of 200ms.Maybe i am unable to explain the situation and invite you to accept me in msn.

    I try to be more clear...Example:
    Normal situation:
    client: SCR
    server: 108byte
    client: SND
    server: Send a lot of packets(FILE).
    File tranfered.

    This work fine the 1° time.Now i press another time the button and i have:
    client: SCR
    client: recived NULL(Without server send)
    client: SND
    exit.

    The program walk alone...i invite you to see the entire program because i try to do in a new program this:
    s.send("CON")
    Data=s.recv()
    print Data
    s.send("CON1")
    Data=s.recv()
    print Data
    s.send("CON2")
    print Data
    Data=s.recv()

    And all work fine...

  6. #6
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Python connection.

    Quote Originally Posted by ixiodor View Post
    I do programs from 8 years...i know how work TCP protocol and "Nagle's algorithm" isn't in this case...i recive "12345qwerty" if i send two messagge in a time < of 200ms.
    Don't take me wrong, ok ? I am not making any consideration related to your programming skills, I was only pointing that the program has some potential weakness. And they are not only related to nagle but to socket library, operating system, network conditions and so on.

    Maybe i am unable to explain the situation and invite you to accept me in msn.
    Please, use my dibo login plus gmail.com and you will find me at gtalk (tomorrow, probably: 02AM here!).

    I try to be more clear...Example:
    ...
    This work fine the 1° time.Now i press another time the button and i have:
    client: SCR
    client: recived NULL(Without server send)
    client: SND
    exit.

    The program walk alone...
    When do you say NULL are you saying empty string ? Empty string means connection closed gracefully (at least from one side, a half close, for instance). None is not expected (an exception should be raised instead).

    i invite you to see the entire program ...
    I did it at first time, believe me

    Marcelo

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    Re: Python connection.

    Quote Originally Posted by marcelobarrosalmeida View Post
    I did it at first time, believe me
    This is only a small part...only the function where i get problems...
    After i get a NULL string in recv,my application return to main menù,and i can send other commands,then the connection still alive...

    Belive me,isn't a simple problem...Maybe the problem not is "recv" but other things in the program logic.(or S.O because this program run on a S60 5th).
    This program isn't perfect,no have error handle but the "logic" is fine.

    Thanks for all.
    Last edited by ixiodor; 2009-12-30 at 13:46.

  8. #8
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Python connection.

    Quote Originally Posted by ixiodor View Post
    After i get a NULL string in recv,my application return to main menù,and i can send other commands,then the connection still alive...
    You can send and receive, right ? Or just send ?
    For a blocking socket (I am guessing you are using blocking sockets), empty string means connection closed, even if for only one side (you can continue to send in this case but you will not receive more data). If the behavior is different, like None as return or both sides working, we may be facing a new bug in PyS60

    Quote Originally Posted by ixiodor View Post
    This is only a small part...only the function where i get problems...
    I understand. But from the snippet that I am seeing, I can not help so much.
    BTW, sockets objects may not be shared between threads, I am not sure if you are using threads but ..

    See you!

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    Re: Python connection.

    SOLVED

    The problem was this:
    1° time all it's ok,BUT last size bytes send are 8000.I,in the client,get only a partial number of bytes,then other stay in a buffer.
    Next time i call recv,i recive old bytes in the buffer.

    A thanks to marcelobarrosalmeida for all the time and helps ^^.

  10. #10
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Python connection.

    Quote Originally Posted by ixiodor View Post
    SOLVED

    The problem was this:
    1° time all it's ok,BUT last size bytes send are 8000.I,in the client,get only a partial number of bytes,then other stay in a buffer.
    Next time i call recv,i recive old bytes in the buffer.

    A thanks to marcelobarrosalmeida for all the time and helps ^^.
    It was a great debug session over gtalk
    Welcome to PyS60 community, ixiodor.

Similar Threads

  1. Replies: 2
    Last Post: 2009-12-01, 12:55
  2. start midlet by bluetooth on S40
    By peba25 in forum Mobile Java Networking & Messaging & Security
    Replies: 15
    Last Post: 2009-11-05, 09:35
  3. Python for S60.Read it once
    By jalpesh_patel17 in forum Symbian
    Replies: 0
    Last Post: 2008-09-08, 05:45
  4. Replies: 1
    Last Post: 2008-01-08, 09:58
  5. Replies: 5
    Last Post: 2003-02-07, 11:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×