Jump to content

Chat (TCP) Problems


Recommended Posts

Hi Everybody out there!

I'm pretty new to AutiIt3 but i've begun on a chat. Actually i almost thought i had did it, but it doesn't work..

1) It can't connect to other computers, it only works (to point 2) when i'm inserting my own IP

2) $recvByte = TCPRecv($connectSocket, 2048)

If @error Then MsgBox(0,"Error:","There was an error: " & @error) And i got a error. And @error is -1.

The Code:

#include <GUIConstants.au3>
#include <Date.au3>


$noConnection = True
$incomingCon = False
$yourIP = ""
$myIP = @IPAddress1
$conPort = 65432
$windowWidth = 500
$windowHeight = 400
$mainWindow = GUICreate("Chat Vers. 1.0 (" & $myIP & ")", $windowWidth, $windowHeight)

$fileMenu = GUICtrlCreateMenu("&File")
$connectMenuItem = GUICtrlCreateMenuItem("Connect",$fileMenu)
$closeMenuItem = GUICtrlCreateMenuItem("Close", $fileMenu)

$messageShowEdit = GUICtrlCreateEdit(getTime() & @CRLF, 10, 10, $windowWidth - 20, 300, BitOR($ES_READONLY, $ES_MULTILINE, $WS_VSCROLL))
$sendInput = GUICtrlCreateInput("", 10, 320, $windowWidth - 80, 20)
$sendButton = GUICtrlCreateButton("Send", 435, 315, 60, 30)

TCPStartup()
$mainSocket = TCPListen($myIP, $conPort)
If $mainSocket = -1 Then Exit


GUISetState()
While 1
    $msg = GUIGetMsg()
; Chat TCP stuff
    $connectSocket = TCPAccept($mainSocket)
    If $connectSocket >= 0 Then
;~      $yourIP = SocketToIP($connectSocket)
        $incomingCon = True
        MsgBox(0,"Incoming Connection","A connection has been made with your friend")
    EndIf
    
    If $incomingCon = True Then
    $recvByte = TCPRecv($connectSocket, 2048)
    If @error Then MsgBox(0,"Error:","There was an error: " & @error)
    
    If $recvByte <> "" Then
        MsgBox(0,"","Tester")
        updateEdit($recvByte, 0)
    EndIf
    EndIf
        
; Cases
    Select
    case $msg = $GUI_EVENT_CLOSE OR $msg = $closeMenuItem
        ExitLoop
        
    case $msg = $connectMenuItem
        $recIP = InputBox("IP Address","Please enter a valid IP address.")
        $outSocket = TCPConnect($recIP, $conPort)
        If $outSocket = -1 Then 
            MsgBox(0, "Error","Invalid IP Address")
        Else
            MsgBox(0,"Connected","You've been succesfully connected")
            $noConnection = False
        EndIf
            

    case $msg = $sendButton
        $newMessage = GUICtrlRead($sendInput)
        If $noConnection = False Then
            MsgBox(0,"","Test")
            TCPSend($outSocket, $newMessage)
            If @error Then ExitLoop
        EndIf
        updateEdit($newMessage)
        GUICtrlSetData($sendInput,"")
        GUICtrlSetState($sendInput, $GUI_FOCUS)
    EndSelect
WEnd

TCPShutdown()
    
Func getTime()
    $localTime = _Date_Time_GetLocalTime()
    $startTime = _Date_Time_SystemTimeToDateTimeStr($localTime)
    Return $startTime
EndFunc

Func updateEdit($newMessage, $youSaid = 1)
    If $newMessage <> "" Then
            $oldTxt = GUICtrlRead($messageShowEdit)
            If $noConnection <> True Then
                If $youSaid = 1 Then
                    GUICtrlSetData($messageShowEdit, $oldTxt & @CRLF & "You said: " & $newMessage)
                Else
                    GUICtrlSetData($messageShowEdit, $oldTxt & @CRLF & "Your friend said: " & $newMessage)
                EndIf
            Else
                GUICtrlSetData($messageShowEdit, $oldTxt & @CRLF & "No connection...")
            EndIf
    EndIf
    Return
EndFunc

I'm sorry for the messy code, but i've done a bigtime test on the program.. :)

Hope you guys can help!

~Saebekassebil

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