Sign in to follow this  
Followers 0
AlmarM

Chat Messanger

11 posts in this topic

Hello,

I want to create a Chet Messanger, but I dont know where, and how to begin. Could someone explain me more and give me some important information?

AlmarM


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites



Hello,

I want to create a Chet Messanger, but I dont know where, and how to begin. Could someone explain me more and give me some important information?

AlmarM

I write this simple example:

#include <File.au3>

Global $CurrentSocket = 0 
Global $ListenSocket
Global $ConnectedSocket[16]
Global $AUTH[16]

For $INDEX = 0 To 15
    $AUTH[$INDEX] = 0
Next

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,1777,16)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
    
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $AUTH[$INDEX] <> 0 Then
                    For $INDEX = 0 To 15
                        If $ConnectedSocket[$INDEX] <> - 1 And $AUTH[$INDEX] = 1 Then
                            TCPSend($ConnectedSocket[$INDEX],$Recv)
                        EndIf
                    Next
                EndIf
                If StringLeft($Recv,4) = "JOIN" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    If Verify($USER,$PASSWORD) Then 
                        $AUTH[$INDEX] = 1
                        TCPSend($ConnectedSocket[$INDEX],"Successful login.")
                    EndIf
                ElseIf StringLeft($Recv,6) = "CREATE" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    CreateAccount($USER,$PASSWORD)
                    TCPSend($ConnectedSocket[$INDEX],"Account was created.")
                EndIf
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func Verify($USER,$PASSWORD)
    Local $INDEX
    Local $DATA
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",0)
    For $INDEX = 1 To _FileCountLines(@ScriptDir & "\Database.dat")
        $LINE = FileReadLine($DATABASE,$INDEX)
        $DATA = StringSplit($LINE," ")
        If $DATA[1]==$USER AND $DATA[2]==$PASSWORD Then Return 1
    Next
EndFunc

Func CreateAccount($USER,$PASSWORD)
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",1)
    FileWriteLine($DATABASE,$USER & " " & $PASSWORD)
    FileClose($DATABASE)
EndFunc

You can complicate things to create other functions (something like GetUsersList). This is only the server, you can connect with any client. Use

CREATE User Password
to create an account and
JOIN User Password
to log in.

Hope that helps.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I write this simple example:

#include <File.au3>

Global $CurrentSocket = 0 
Global $ListenSocket
Global $ConnectedSocket[16]
Global $AUTH[16]

For $INDEX = 0 To 15
    $AUTH[$INDEX] = 0
Next

$TCP = TCPStartup()
If $TCP = 0 Then
    MsgBox(0, "Error", "Unable to startup TCP Services!")
    Exit
EndIf

$ListenSocket = TCPListen(@IPAddress1,1777,16)
If $ListenSocket = -1 Then
    MsgBox(0, "ERROR", "Unable to start listening on port 1777")
    Exit
EndIf
    
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($ListenSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = $CurrentSocket + 1
    EndIf
    For $INDEX = 0 To 15
        If $ConnectedSocket[$INDEX] <> -1 Or $ConnectedSocket[$INDEX] <> "" Then
            $Recv = TCPRecv($ConnectedSocket[$INDEX],1024)
            If $Recv <> "" Then
                If $AUTH[$INDEX] <> 0 Then
                    For $INDEX = 0 To 15
                        If $ConnectedSocket[$INDEX] <> - 1 And $AUTH[$INDEX] = 1 Then
                            TCPSend($ConnectedSocket[$INDEX],$Recv)
                        EndIf
                    Next
                EndIf
                If StringLeft($Recv,4) = "JOIN" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    If Verify($USER,$PASSWORD) Then 
                        $AUTH[$INDEX] = 1
                        TCPSend($ConnectedSocket[$INDEX],"Successful login.")
                    EndIf
                ElseIf StringLeft($Recv,6) = "CREATE" Then
                    $DATA = StringSplit($Recv," ")
                    $USER = $DATA[2]
                    $PASSWORD = $DATA[3]
                    CreateAccount($USER,$PASSWORD)
                    TCPSend($ConnectedSocket[$INDEX],"Account was created.")
                EndIf
            EndIf
        EndIf
    Next
    Sleep(20)
WEnd

Func Verify($USER,$PASSWORD)
    Local $INDEX
    Local $DATA
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",0)
    For $INDEX = 1 To _FileCountLines(@ScriptDir & "\Database.dat")
        $LINE = FileReadLine($DATABASE,$INDEX)
        $DATA = StringSplit($LINE," ")
        If $DATA[1]==$USER AND $DATA[2]==$PASSWORD Then Return 1
    Next
EndFunc

Func CreateAccount($USER,$PASSWORD)
    $DATABASE = FileOpen(@ScriptDir & "\Database.dat",1)
    FileWriteLine($DATABASE,$USER & " " & $PASSWORD)
    FileClose($DATABASE)
EndFunc

You can complicate things to create other functions (something like GetUsersList). This is only the server, you can connect with any client. Use

CREATE User Password
to create an account and
JOIN User Password
to log in.

Hope that helps.

So thats the server?

So if I make a gui, how do I connect to it?

AlmarM

EDIT:

Aha, just

#include "Server.au3"

; GUI Stuff
; A button what connects to CreateAccount()

EDIT:

and the JOIN() is Verify()??

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

For example this

Connect on port 1777.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Ah, but how do I use the CREATE and JOIN functions?

With a .bat?

AlmarM


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

Ah, but how do I use the CREATE and JOIN functions?

With a .bat?

AlmarM

You need a client. Maybe this is more suggestive.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

You need a client. Maybe this is more suggestive.

Aha! Now I get it. But ive got this

$GUI = GUICreate("iTalk", 350, 420, -1, -1)

GUICtrlCreateGroup("Server Information", 10, 10, 305, 70)
GUICtrlCreateLabel("IP", 20, 30)
$IP = GUICtrlCreateInput("", 60, 28, 200, 20)
$Port = GUICtrlCreateInput("", 60, 50, 100, 20)
$Connect = GUICtrlCreateButton("Connect", 165, 50, 96, 20)
$TCP = GUICtrlCreateRadio("TCP", 265, 27)
$UDP = GUICtrlCreateRadio("UDP", 265, 48)
GUICtrlSetState($TCP, 1)
GUICtrlCreateLabel("Port", 20, 53)

$x = GUICtrlCreateButton("i", 320, 15, 20, 20)
$xx = GUICtrlCreateButton("?", 320, 37, 20, 20)
$xxx = GUICtrlCreateButton(":)", 320, 59, 20, 20)

$ChatBox = GUICtrlCreateList("Welcome to ~ iTalk ~", 10, 90, 330, 300)
$TextInput = GUICtrlCreateInput("", 10, 390, 270, 20)
$Send = GUICtrlCreateButton("Send", 285, 390, 55, 20)

GUISetState()
While 1
    $nMsg = GUIGetMsg()
    Select
    Case $nMsg = -3
        Exit
    EndSelect
WEnd

Thats the body I want to use. Now how do I connect to my server?

AlmarM


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

Here is my NetCom client, look at TCP functions:

#include <GUIConstants.au3>
#Include <GuiEdit.au3>

Global $SOCKET
Global $SB_SCROLLCARET = 4

#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("NetCom", 286, 296, 201, 124)
$Group1 = GUICtrlCreateGroup(" Server Info ", 6, 8, 271, 73)
$IP = GUICtrlCreateInput(@IPAddress1, 14, 24, 129, 21)
$PORT = GUICtrlCreateInput("80", 14, 48, 129, 21)
$CON = GUICtrlCreateButton("CONNECT", 149, 21, 121, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$DIS = GUICtrlCreateButton("DISCONNECT", 150, 48, 121, 25, 0)
$Group2 = GUICtrlCreateGroup(" Connection Info ", 6, 88, 271, 199)
$SYSMSG = GUICtrlCreateEdit(">Welcome to NetCom", 12, 104, 257, 109)
$SEND_DATA = GUICtrlCreateEdit("", 12, 220, 193, 61)
$SEND = GUICtrlCreateButton("SEND", 212, 218, 57, 63, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW,$GUI)
#EndRegion ### END Koda GUI section ###

$DATA = ""

While 1
    $RECV = TCPRecv($SOCKET,512)
    If $RECV <> "" Then
        $TEMP_DATA = GUICtrlRead($SYSMSG)
        GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RECV)
        _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
    EndIf
    $MSG = GUIGetMsg()
    Switch $MSG
        Case $GUI_EVENT_CLOSE
            Exit
        Case $CON
            TCPStartup()
            $SOCKET = TCPConnect(TCPNameToIP(GUICtrlRead($IP)),GUICtrlRead($PORT))
            If $SOCKET <> -1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connecting to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Connection...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Unable to connect to " & GUICtrlRead($IP) & " on port " & GUICtrlRead($PORT))
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $DIS
            $RES = TCPCloseSocket($SOCKET)
            If $RES = 1 Then
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnecting from " & GUICtrlRead($IP))
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & ">>Disconnect...OK")
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            EndIf
        Case $SEND
            $TEMP_DATA = GUICtrlRead($SYSMSG)
            _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            IF StringLeft(GUICtrlRead($SEND_DATA),4) = "GET " Then
                Local $SPLIT = StringSplit(GUICtrlRead($SEND_DATA)," ")
                Local $HOST = $SPLIT[2]
                Local $PAGE = $SPLIT[3]
                Local $RESULT = _HTTPGet($HOST,$PAGE)
                $TEMP_DATA = GUICtrlRead($SYSMSG)
                GUICtrlSetData($SYSMSG,$TEMP_DATA & @CRLF & "<<" & $RESULT)
                _GUICtrlEdit_Scroll($SYSMSG,$SB_SCROLLCARET)
            Else
            TCPSend($SOCKET,GUICtrlRead($SEND_DATA))
            GUICtrlSetData($SEND_DATA,"")
            EndIf
        Case -3
            Exit 0
    EndSwitch
WEnd

Func _HTTPGet($sHost, $sPage="")
    Local $iSocket = _HTTPConnect($sHost)
    If @error Then Return SetError(1, 0, "")
   
    Local $sCommand = "GET " & $sPage & " HTTP/1.1" & @CRLF
   
    $sCommand &= "Host: " & $sHost & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Referer: " & $sHost & @CRLF
    $sCommand &= "Connection: close" & @CRLF & @CRLF
   
    Local $BytesSent = TCPSend($iSocket, $sCommand)
    If $BytesSent = 0 Then Return SetError(2, @error, 0)
   
    Local $sRecv = "", $sCurrentRecv
   
    While 1
        $sCurrentRecv = TCPRecv($iSocket, 16)
        If @error <> 0 Then ExitLoop
        If $sCurrentRecv <> "" Then $sRecv &= $sCurrentRecv
    WEnd
   
    _HTTPShutdown($iSocket)
   
    Return $sRecv
EndFunc

Func _HTTPConnect($sHost, $iPort=80)
    TCPStartup()
   
    Local $sName_To_IP = TCPNameToIP($sHost)
    Local $iSocket = TCPConnect($sName_To_IP, $iPort)
   
    If $iSocket = -1 Then
        TCPCloseSocket($iSocket)
        Return SetError(1, 0, "")
    EndIf
   
    Return $iSocket
EndFunc

Func _HTTPShutdown($iSocket)
    TCPCloseSocket($iSocket)
    TCPShutdown()
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ok, now I got that... I saw u had the same problem. How to chat :)?

AlmarM

EDIT:

we need this command?

Func addrSend($sMsg)
    If $rConnection == -1 Then 
        addItem("-!- Not connected to any server")
        Return
    EndIf
    Switch $connectionProtocol
        Case "TCP"
            $dataSend = TCPSend($rConnection,$sMsg&@CRLF)
        Case "UDP"
            
            $dataSend = UDPSend($rConnection,$sMsg&@CRLF)
    EndSwitch
    
    If $dataSend == 0 Then
        addItem("-!- Could not send data. Possible connection problems. Closing disconnecting...")
        addrDisconnect()
        MsgBox(0,0,$rConnection&@CRLF&@error)
    Else
        addItem(">> "&$sMsg)
    EndIf
    GUICtrlSetData($msgInput,"")
EndFunc
Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

Ok, now I got that... I saw u had the same problem. How to chat :)?

AlmarM

EDIT:

we need this command?

Func addrSend($sMsg)
    If $rConnection == -1 Then 
        addItem("-!- Not connected to any server")
        Return
    EndIf
    Switch $connectionProtocol
        Case "TCP"
            $dataSend = TCPSend($rConnection,$sMsg&@CRLF)
        Case "UDP"
            
            $dataSend = UDPSend($rConnection,$sMsg&@CRLF)
    EndSwitch
    
    If $dataSend == 0 Then
        addItem("-!- Could not send data. Possible connection problems. Closing disconnecting...")
        addrDisconnect()
        MsgBox(0,0,$rConnection&@CRLF&@error)
    Else
        addItem(">> "&$sMsg)
    EndIf
    GUICtrlSetData($msgInput,"")
EndFunc
I have no problem. Start the server and then two clients (in my case NetCom). What is so hard to get it.

Then create an account with CREATE User Password and join with JOIN User Password.

Then you can chat as you want.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

I have no problem. Start the server and then two clients (in my case NetCom). What is so hard to get it.

Then create an account with CREATE User Password and join with JOIN User Password.

Then you can chat as you want.

Oh, srry. I tought you posted a topic about how to chat in mainwindow.

But why cant I chat with this:

While 1
    $nMsg = GUIGetMsg()
    Select
    Case $nMsg = -3
        Exit
    Case $nMsg = $Connect
        $Read_IP = GUICtrlRead($IP)
        $Read_Port = GUICtrlRead($Port)
        TCPStartup()
        $Socket = TCPConnect(TCPNameToIP(GUICtrlRead($IP)),GUICtrlRead($Port))
        If $Socket <> -1 Then
            $Temp_Data = GUICtrlRead($ChatBox)
            GUICtrlSetData($ChatBox, $Temp_Data & @CRLF & ">> Connecting to " & $Read_IP & ":" & $Read_Port)
            $Temp_Data = GUICtrlRead($ChatBox)
            GUICtrlSetData($ChatBox, $Temp_Data & @CRLF & ">> Connection: OK")
            _GUICtrlEdit_Scroll($ChatBox, $SB_SCROLLCARET)
        Else
            $Temp_Data = GUICtrlRead($ChatBox)
            GUICtrlSetData($ChatBox, $Temp_Data & @CRLF & ">> Unable to connect to " & $Read_IP & ":" & $Read_Port)
            _GUICtrlEdit_Scroll($ChatBox, $SB_SCROLLCARET)
        EndIf
    Case $Send
        $Read_Text = GUICtrlRead($TextInput)
        $Temp_Data = GUICtrlRead($ChatBox)
        _GUICtrlEdit_Scroll($ChatBox, $SB_SCROLLCARET)
        If StringLeft($Read_Text, 4) = "GET " Then
            Local $SPLIT = StringSplit($Read_Text, " ")
            Local $HOST = $SPLIT[2]
            Local $PAGE = $SPLIT[3]
            Local $RESULT = _HTTPGet($HOST, $PAGE)
            $Temp_Data = GUICtrlRead($ChatBox)
            GUICtrlSetData($ChatBox, $Temp_Data & @CRLF & "<< " & $RESULT)
            _GUICtrlEdit_Scroll($ChatBox, $SB_SCROLLCARET)
        Else
            TCPSend($Socket, $Read_Text)
            GUICtrlSetData($TextInput, "") 
        EndIf
    EndSelect
WEnd

It keep repeating the GUICtrlSetData($TextInput, "")

I cant typ!


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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