Sign in to follow this  
Followers 0
Shyke

Chat Program Problem

7 posts in this topic

#1 ·  Posted (edited)

#include <GuiConstants.au3>
Dim $mConnectSocket = -1, $mListenSocket = -1, $mCallSocket = -1, $port = 33891, $serverip = @IPAddress1, $listenip = @IPAddress1, $data = "", $mAccept = 0, $mRecieve = "", $handshake = 0
TCPStartUp()

$username = InputBox("Messeger", "Your username?")
$listenQ = MsgBox(36, "Messeger","Will you be listening?")
If $listenQ = 6 Then mListen()
If $listenQ = 7 Then
    $toCall = InputBox("Messeger", "Who will you be calling (IPv4)?")
    $serverip = $toCall
    mCall()
EndIf
If @error Then Exit

$gui = GuiCreate("Messeger" & " | " & $username & " |", 370, 320, -1, -1)

GuiCtrlCreateGroup("Messeger", 10, 10, 350, 300)
$messages = GuiCtrlCreateEdit("", 20, 30, 330, 240, $ES_AUTOVSCROLL + $ES_READONLY)
$text = GuiCtrlCreateInput("test message here", 20, 280, 250, 20)
$sendB = GuiCtrlCreateButton("Send", 280, 280, 70, 20)

Func mSend($txt)
    If $txt = "" Then Return
    TCPSend($mCallSocket, $txt)
    If @error Then ErrorF()
    GUICtrlSetData($messages, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] <" & $username & "> " & $txt & @CRLF, 1)
    GUICtrlSetData($text, "")
EndFunc

Func mCall()
    If $handshake < 2 Then
        $mCallSocket = TCPConnect($serverip, $port)
        If $mCallSocket = -1 Then
            mCall()
        Else
            $handshake = $handshake + 1
            mListen()
        EndIf
    EndIf
EndFunc

Func mListen()
    If $handshake < 2 Then
        $mListenSocket = TCPListen($listenip, $port)
        If $mListenSocket = -1 Then
            ErrorF()
        Else
            $handshake = $handshake + 1
            mAccept()
            mCall()
        EndIf
    EndIf
EndFunc

Func mAccept()
    Do
        $mConnectSocket = TCPAccept($mListenSocket)
        If $mConnectSocket <> - 1 Then
            $mAccept = 1
        EndIf
    Until $mConnectSocket <> -1
EndFunc

Func ErrorF()
    ;MsgBox(0, "", "There has been an error: " & @error)
    TCPCloseSocket($mConnectSocket)
    TCPShutDown()
    ;Exit
EndFunc

GuiSetState()
Do
    $msg = GuiGetMsg()
    If $mAccept = 1 Then
        $mRecieve = TCPRecv($mConnectSocket, 2048)
        If @error Then
            GUICtrlSetData($messages, "Connection terminated @ " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF, 1)
            $mAccept = -1
            ErrorF()
        Else
            If $mRecieve <> "" Then GUICtrlSetData($messages, $mRecieve & @CRLF, 1)
        EndIf
    Else
        GUICtrlSetState($text, $GUI_DISABLE)
        GUICtrlSetState($sendB, $GUI_DISABLE)
        Sleep(5000)
    EndIf
    If $msg = $sendB Then mSend(GuiCtrlRead($text))
    If $msg = $GUI_EVENT_CLOSE Then Exit
Until 0

There's my code.

Could anyone tell me why it immediatly terminates when I run two of them? The goal was to have peer to peer chat but it doesn't work out so well... Could anyone also tell me what I could do to improve the code?

Edited by Shyke

Share this post


Link to post
Share on other sites



Could you please change your code tags to autoit tags to make it easier to read? Thank you.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

I don't know how to - would you mind specifying how?

Share this post


Link to post
Share on other sites

Paste your script into the message. At the start of the scirpt put:

At the end of the script put:

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Task completed.

Thank-you, I was unaware of that feature.

Share this post


Link to post
Share on other sites

1. You do not seem to close your Group ( GUICtrlCreateGroup ("",-99,-99,1,1) )

2. You do not show your gui ( GuiSetState() )

See how it goes with correction.

:P

Share this post


Link to post
Share on other sites

This function does not look right. Why is it calling it's self? Here is a peer-to-peer chat example. Take a gander at it and it might point you in the right direction.

Func mCall()
    If $handshake < 2 Then
        $mCallSocket = TCPConnect($serverip, $port)
        If $mCallSocket = -1 Then
            mCall()
        Else
            $handshake = $handshake + 1
            mListen()
        EndIf
    EndIf
EndFunc

A little reading goes a long way. Post count means nothing.

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  
Followers 0