Jump to content

Networking problem


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...