Jump to content

Simple Messenger


Recommended Posts

I have been trying to make a simple messenger to understand the TCP functions. I made a quick sloppy GUI just so i can have an input box and recieve messages. I can't understand what's wrong with my code, so here it is. please help

CODE

#include <GUIConstants.au3>

$window = GUICreate ("Messenger", 500, 500)

$inbox = GUICtrlCreateEdit ("", 5, 5, 100, 100)

$input = GUICtrlCreateInput ("", 5, 200, 100, 20)

$send = GUICtrlCreateButton ("Send", 125, 125, 50, 50)

GUISetState ()

TCPStartup ()

$connect = TCPConnect ("127.0.0.1", 65432)

$listen = TCPListen ("127.0.0.1", 65432)

Do

$msg = GUIGetMsg()

$accept = TCPAccept ($listen)

$recieve = TCPRecv ($accept, 2048)

GUICtrlSetData ($inbox, $recieve)

Switch $msg

Case $send

$message = GUICtrlRead ($input)

TCPSend ($connect, $message)

EndSwitch

Until $msg = $GUI_EVENT_CLOSE

TCPShutdown ()

GUIDelete ($window)

Exit

Link to comment
Share on other sites

Your code has some problems, first you use TCPConnect() before TCPListen() that of course will not work.

You also use TCPAccept in the loop, this is not necessary. You should also check if you got something from TCPRecieve before setting the inbox control.

Here's the clean up version of your code :)

#include <GUIConstantsEx.au3> ; Use this instead, since GUIConstants is obsolete 

$window = GUICreate("Messenger", 500, 500)
$inbox = GUICtrlCreateEdit("", 5, 5, 100, 100)
$input = GUICtrlCreateInput("", 5, 200, 100, 20)
$send = GUICtrlCreateButton("Send", 125, 125, 50, 50)
GUISetState()

TCPStartup()
$listen = TCPListen("127.0.0.1", 65432)
$connect = TCPConnect("127.0.0.1", 65432)
$accept = TCPAccept($listen)

Do
    $msg = GUIGetMsg()


    $recieve = TCPRecv($accept, 2048)
    If $recieve <> "" Then
        GUICtrlSetData($inbox, $recieve)
    EndIf

    Switch $msg

        Case $send
            $message = GUICtrlRead($input)
            TCPSend($connect, $message)
    EndSwitch

Until $msg = $GUI_EVENT_CLOSE

TCPShutdown()

;~ GUIDelete($window) ;This is redundant
;~ Exit ; So is this

Good luck! :(

Broken link? PM me and I'll send you the file!

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...