Jump to content
Sign in to follow this  
annelinn

Networking problem

Recommended Posts

annelinn

Hello all,

I'm trying to learn about TCP and have written a simple server/client program.

1. Client connects to server

2. Server acknowledges connection

3. Client encrypts a short message using the rijndael cipher

4. Client sends the message to the server

5. Server decrypts the message, and displays it

6. Steps 3-5 repeat ten times at random intervals

Now, the problem is this. The following code works just fine:

For $a = 1 to 10
    Sleep(Random(3000, 5000))
    TCPSend($Socket, Encrypt($message))                        ; Send the message
Next

But, if you try to send multiple packets at almost same time (as in the next example), some of them are received by the server correctly, but some are received as gibberish!

For $a = 1 to 10
    Sleep(Random(3000, 5000))
    TCPSend($Socket, Encrypt($message))                        ; Send the message
    Sleep(500)
    TCPSend($Socket, Encrypt($message & " Version 2"))    ; Send the message with some text appended
Next

I don't understand! Have I missed something obvious, or is AutoIT incapable of sending more than one packet a second?

post-48660-1240036933_thumb.jpg

Share this post


Link to post
Share on other sites
Richard Robertson

Are you sure that the server is decrypting a complete message?

Share this post


Link to post
Share on other sites
annelinn

Are you sure that the server is decrypting a complete message?

I think it is confusing two messages as one, and trying to decrypt them both as a single message.

Example: message 26732 is sent twice, and decrypted twice

Example: next message, 30414, is sent twice, but decrypted once, and the decrypted text is garbled

The question is: does TCPReceive() receive both messages, and confuse them as one? (below is the code from the server)

$data = TCPRecv($socket, 512)
$result = Decrypt($data)
; Display the decrypted message
_GUICtrlEdit_AppendText($window, $result & @CRLF)
Edited by annelinn

Share this post


Link to post
Share on other sites
trancexx

Or client encrypting properly?

This is totally off topic but why using that ugly 'win classic' style?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
annelinn

Or client encrypting properly?

This is totally off topic but why using that ugly 'win classic' style?

I'm as sure as I can be that the encrypting algorithm is correct, because I'm using the one skinnywhiteguy wrote (rijndael.au3):

http://www.autoitscript.com/forum/index.php?showtopic=44581

And my implementations are:

Func Encrypt($message, $key))
    Return StringToBinary(_rijndaelCipher($key, $message))
EndFunc

and

Func Decrypt($message, $key)
    Return BinaryToString(_rijndaelInvCipher($key, $message))
EndFunc

About ugly windows themes: one ugly theme is much the same as any other ^_^

Edited by annelinn

Share this post


Link to post
Share on other sites
Richard Robertson

How about adding an integer value of the length of the message before sending it. Then have the server read the length value, and pick up that many bytes for the message. That way you know you only have one message at a time.

Like

client: encrypt "something"->"237841"

client: send 6

client: send "237841"

server: read incoming, get a 6

server: read 6 bytes

server: decrypt "237841"->"something"

Edited by Richard Robertson

Share this post


Link to post
Share on other sites
annelinn

An excellent idea. Despite it being so obvious, it never occured to me. Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×