Jump to content

TCP Chat stuck on username display


Recommended Posts

Hello, I am making a chat with TCP. I have hit a road block trying to get usernames to be shown up on the client. Any help would be appreciated.

Server:

#include <Array.au3>

TCPStartup()

Global $SERVER, $SOCKET[1]

Dim $People[50];[2];Changed to 2, don't need IP.

$SERVER = TCPListen(@IPAddress1,1337)
If @error Then
    MsgBox(0, "", @error)
    MsgBox(0,"error","Error to listening on port 1337.")
    Quit()
EndIf

$username = ""

FileOpen("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt")

While 1
    sleep(25)
    
    $TConn = TCPAccept($SERVER)
    
    If $TConn <> -1 Then
        _ArrayAdd($SOCKET,$TConn)
        $People[0] += 1
        $People[$People[0]] = $Username;<-- You need to have their username here.
        MsgBox(0, "Usernames", $People[1])
    EndIf
    
    For $Cx = 0 to UBound($SOCKET) - 1
        If $SOCKET[$Cx] <> "" Then
            $RECV = TCPRecv($SOCKET[$Cx], 512) 
            If @error Then
                TCPCloseSocket($SOCKET[$Cx])
                _ArrayDelete($SOCKET,$Cx)
                _ArrayDelete($People, $Cx);<-- Removes the username from $People
                $People[0] -= 1
            EndIf
            
            $Name = StringSplit($RECV, "~")
            
            If $Name[1] = "USER" Then
                $Name[2] = $Username
            Else
            If $RECV <> "" Then
                sendall($RECV)
            EndIf
        EndIf
        EndIf
    Next

WEnd

Func sendall($msg)
    If $msg <> "" Then
        For $Ox = 0 to UBound($SOCKET) - 1
            If $SOCKET[$Ox] <> "" Then
                FileWriteLine("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt", "[" & @Hour & ":" & @MIN & ":" & @SEC & "] " & $msg)
                If TCPSend($SOCKET[$Ox], "[" & @Hour & ":" & @MIN & ":" & @SEC & "] " & $msg) == 0 And @error Then
                    TCPCloseSocket($SOCKET[$Ox])
                    _ArrayDelete($SOCKET,$Ox)
                    _ArrayDelete($People, $Ox);<-- Removes the username from $People
                    $People[0] -= 1
                EndIf
            EndIf
        Next
    EndIf
EndFunc

Func Quit()
    For $Qx = 0 to UBound($SOCKET) - 1
        TCPCloseSocket($SOCKET[$Qx])
    Next
    FileClose("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt")
    TCPShutdown()
    Exit
EndFunc

Client:

#Include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
#include <INET.au3>


Global $username = InputBox("Username", "What would you like your username to be?")

$login = 0

TCPStartup()

Global $CONNECT

$CONNECT = TCPConnect("71.52.190.87" ,1337)
If @error Then
    MsgBox(16, "Error", "Cannot connect to 71.52.190.87 on port 1337.")
    Quit()
EndIf

TCPSend($CONNECT, "USER~" & $username)

$Window = GUICreate("Donald's Instant Messenger", 420, 300)
GUISetState()
Global $info = GUICtrlCreateEdit("Everything that is sent will be logged." & @CRLF, 10, 10, 280, 200, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $Names = GUICtrlCreateEdit($Username & @CRLF, 300, 10, 100, 280, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $sending = GUICtrlCreateInput("", 10, 220, 200, 70)
$b_send = GUICtrlCreateButton("Send", 220, 220, 70, 70, BitOR($BS_DEFPUSHBUTTON, $WS_GROUP))

TCPSend($CONNECT, $username & " has connected from " & _GetIP())

While True
    $RECV = TCPRecv($CONNECT,512)
    If @error Then Quit()
        
    $State = WinGetState($Window)
    
    If $RECV <> "" Then
        GUICtrlSetData($info, $RECV & @CRLF, True)
        If BitAND($State, 16) Then
        WinFlash($Window, "", 10, 4)
        EndIf
    EndIf
    
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Quit()
    Case $msg = $b_send
        say()
    EndSelect
    
    
WEnd


Func say()
    $DATA = GUICtrlRead($Sending)
    If $DATA <> "" Then
        TCPSend($CONNECT, $username & ": " & $DATA)
        GUICtrlSetData($sending, "")
    EndIf
EndFunc

Func Quit()
    TCPSend($CONNECT, $username & " has disconnected.")
    sleep(50)
    TCPCloseSocket($CONNECT) ; Close socket
    TCPShutdown()
    Exit
EndFunc
Link to comment
Share on other sites

Hey Donald :)

I assume you mean something like a list of the users currently connected?

Hello Bwochinski, Yeah that's it. I tried pretty hard to get this far I'm sad to say but I didn't get very far.

I was planning on just adding the names to a text file and have them host them from that but I thought it would actually be easier trying to use arrays instead.

Edited by Donald8282
Link to comment
Share on other sites

Well it looks like you keep track of who's connected in an array already, so all you need to do is be able to send that to the client, but identify it as information rather than a chat message.

I did a bit of poking around on your scripts from the last time, setting it up with a _very_ simple chat protocol. I don't want to step on your toes, but I could post them as an example of what I mean... Just give me a few to clean things up.

Link to comment
Share on other sites

Well it looks like you keep track of who's connected in an array already, so all you need to do is be able to send that to the client, but identify it as information rather than a chat message.

I did a bit of poking around on your scripts from the last time, setting it up with a _very_ simple chat protocol. I don't want to step on your toes, but I could post them as an example of what I mean... Just give me a few to clean things up.

Sure, examples would be great.

Link to comment
Share on other sites

I didn't have a chance to really test these, so there will probably be a couple bugs, but the basics should all be there...

Let me know if I can explain anything.

SERVER:

#include <Array.au3>
#Include <String.au3>

TCPStartup()

Global $SERVER, $SOCKET[1], $NICKS[1]

$SERVER = TCPListen(@IPAddress1,1337)
If @error Then
    MsgBox(0,"error","Error listening on port 1337.")
    Quit()
EndIf

While 1
    sleep(25)
   
    $TConn = TCPAccept($SERVER)
   
    If $TConn <> -1 Then
        _ArrayAdd($SOCKET,$TConn)
    EndIf
   
    recvAll()
    
WEnd

Func recvAll()
        Local $RECV, $cmd, $text
    
    For $Cx = 0 to UBound($SOCKET) - 1
        If $SOCKET[$Cx] <> "" Then
            $RECV = TCPRecv($SOCKET[$Cx], 512)
            If StringLen($RECV) > 7 Then
                $cmd = StringLeft($RECV, 7)
                $text = StringMid($RECV, 8)
                Switch $cmd
                Case "CONNECT"
                    $NICKS[$Cx] = $text
                    notifyAll("New user " & $text & " has connected.")
                    sendAllNicks()
                Case "NICKNAM"
                    changeNick($Cx,$text)
                Case "DISCONN"
                    notifyAll("User " & $NICKS[$Cx] & " has disconnected.")
                    _ArrayDelete($SOCKET,$Cx)
                    _ArrayDelete($NICKS,$Cx)
                Case "CHATMSG"
                    chatAll($Cx,$text)
                Case "USERMSG"
                    chatUser($Cx,$text)
                Case "NICKLST"
                    sendNicks($SOCKET[$Cx])
                EndSwitch
                
            EndIf
        EndIf
    Next
EndFunc

Func sendAllNicks()
        For $user In $SOCKET
                If $user <> "" Then
                        sendNicks($user)
                EndIf
        Next
EndFunc

Func sendNicks($user)
        TCPSend($user, "NICKLST" & _ArrayToString($NICKS))
EndFunc

Func changeNick($userNum,$newNick)
        If StringLen($newNick) >= 3 Then
                If $NICKS[$userNum] <> $newNick Then
                        If _ArraySearch($NICKS,$newNick) == -1 Then
                                notifyAll("User " & $NICKS[$userNum] & " is now known as " & $newNick)
                                $NICKS[$userNum] = $newNick
                        Else
                                notifyUser($SOCKET[$userNum],"Nickname " & $newNick & " is already in use!")
                        EndIf
                EndIf
        Else
                notifyUser($SOCKET[$userNum],"Nickname " & $newNick & " is too short. Must be at least 3 characters.")
        EndIf
EndFunc

Func notifyUser($socket, $msg)
        TCPSend($socket, "NOTIFYM" & $msg)
EndFunc

Func notifyAll($msg)
        For $user In $SOCKET
                If $user <> "" Then
                        notifyUser($user, $msg)
                EndIf
        Next
EndFunc

Func chatAll($from,$msg)
    If $msg <> "" Then
        For $Ox = 0 to UBound($SOCKET) - 1
            If $SOCKET[$Ox] <> "" Then
                If TCPSend($SOCKET[$Ox], "CHATMSG" & $NICKS[$from] & "|" & $msg) == 0 And @error Then
                    TCPCloseSocket($SOCKET[$Ox])
                    _ArrayDelete($SOCKET,$Ox)
                    _ArrayDelete($NICKS,$Ox)
                EndIf
            EndIf
        Next
    EndIf
EndFunc

Func chatUser($from,$msg)
    
    Local $to,$text
    
    $to = _StringBetween($msg, "", "|")
    $text = _StringBetween($msg, "|", "")
    
    If $msg <> "" Then
        $userID = _ArraySearch($NICKS,$to)
        If $userID <> -1 Then
            If TCPSend($SOCKET[$userID], "USERMSG" & $NICKS[$from] & "|" & $text) == 0 And @error Then
                TCPCloseSocket($SOCKET[$userID])
                _ArrayDelete($SOCKET,$userID)
                _ArrayDelete($NICKS,$userID)
            EndIf
        Else
            notifyUser($SOCKET[$from], "Nickname " & $to & " is not online.")
        EndIf
    EndIf
EndFunc

Func Quit()
    For $Qx = 0 to UBound($SOCKET) - 1
        notifyUser($SOCKET[$Qx], "Server is shutting down...")
        TCPCloseSocket($SOCKET[$Qx])
    Next
    TCPShutdown()
    Exit
EndFunc

CLIENT:

#Include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
#include <String.au3>


Global $username
Global Const $winTitle = "Donald's Instant Messenger"

TCPStartup()

Global $CONNECT, $NICKLIST


GUICreate($winTitle, 500, 500)
GUISetState()

Global $btnConnect = GUICtrlCreateButton("Connect", 10, 10, 100, 30)

Global $info = GUICtrlCreateEdit("", 10, 50, 325, 350, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $edtNicklist = GUICtrlCreateEdit("",340, 50, 150, 350, $ES_READONLY)

Global $sending = GUICtrlCreateInput("", 10, 410, 400, 70)
Global $b_send = GUICtrlCreateButton("Send", 420, 410, 70, 70, BitOR($BS_DEFPUSHBUTTON, $WS_GROUP))


While True
    
        If IsObj($CONNECT) Then
                $RECV = TCPRecv($CONNECT,512)
                If @error Then Quit()
            
                If $RECV <> "" Then
                        If StringLen($RECV) > 7 Then
                                $cmd = StringLeft($RECV, 7)
                                $text = StringMid($RECV, 8)
                                Switch $cmd
                                Case "NOTIFYM"
                                        displayNotify($text)
                                Case "CHATMSG"
                                        displayChat($text)
                                Case "NICKLST"
                                        updateNicks($text)
                                Case "USERMSG"
                                        displayUserMsg($text)
                                EndSwitch
                            
                        EndIf
                EndIf
        EndIf
   
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        Quit()
    Case $b_send
        say()
    Case $btnConnect
        toggleConnect()
    EndSwitch
   
   
WEnd

Func updateNicks($text)
    
        GUICtrlSetData($edtNicklist, StringReplace($text, "|", @CRLF))
    
EndFunc

Func toggleConnect()
    If TCPSend($CONNECT, "ping") Then
        destroyConnect()
    Else
        initConnect()
    EndIf
EndFunc

Func initConnect()
    
    If StringLen($username) < 3 Then
            $username= InputBox("Username", "What would you like your username to be?")
    EndIf
   
    displayNotify("Connecting to server...")
   
    $CONNECT = TCPConnect("" ,1337)
    If @error Then
            MsgBox(16, "Error", "Cannot connect to '' on port 1337.")
            displayNotify("Unable to connect")
            Return
    EndIf
   
    TCPSend($CONNECT, "CONNECT" & $username)
   
    displayNotify("Connected!")
    GUICtrlSetData($btnConnect, "Disconnect")
    
EndFunc

Func destroyConnect()
    
    TCPSend($CONNECT, "DISCONN")
    TCPCloseSocket($CONNECT)
    $CONNECT = 0
    displayNotify("You have disconnected.")
    GUICtrlSetData($btnConnect, "Connect")
    GUICtrlSetData($edtNickList, "")
    
EndFunc

Func displayNotify($text)
        GUICtrlSetData($info, "*** " & $text & " ***" & @CRLF, True)
EndFunc

Func displayChat($text)
    
    Local $nick, $msg
   
    $nick = _StringBetween($text, "", "|")
    $msg = _StringBetween($text, "|", "")
   
    If $nick == $username Then
            $nick = "* " & $nick
    EndIf
   
    GUICtrlSetData($info, timestamp() & $nick & ": " & $msg & @CRLF, True)
    
EndFunc

Func displayUserMsg($text)
    
    Local $nick, $msg
   
    $nick = _StringBetween($text, "", "|")
    $msg = _StringBetween($text, "|", "")
   
    GUICtrlSetData($info, "** From " & $nick & ": " & $msg & @CRLF, True)
EndFunc

Func timestamp()
    
    Local $timestamp

    $timestamp = "[hh:mm:ss] "
    $timestamp = StringReplace($timestamp, "hh", @HOUR)
    $timestamp = StringReplace($timestamp, "mm", @MIN)
    $timestamp = StringReplace($timestamp, "ss", @SEC)
    
    Return $timestamp
    
EndFunc

Func say()
    $DATA = GUICtrlRead($Sending)
    If $DATA <> "" Then
        If StringLeft($DATA,6) == "/nick " Then
            TCPSend($CONNECT, "NICKNAM" & StringMid($DATA,7))
        ElseIf StringLeft($DATA,5) == "/msg " Then
            TCPSend($CONNECT, "USERMSG" & StringMid($DATA,6))
            GUICtrlSetData($info, "** To " & _StringBetween($DATA, "", "|") & ": " & _StringBetween($DATA, "|", "") & @CRLF, True)
        Else
            TCPSend($CONNECT, "CHATMSG" & $DATA)
        EndIf
        GUICtrlSetData($sending, "")
    EndIf
EndFunc

Func Quit()
        destroyConnect()
        sleep(50)
    TCPShutdown()
    Exit
EndFunc
Edited by bwochinski
Link to comment
Share on other sites

I'm working on an easy to use Chat UDF, using Kip's TCP UDF.

If you want, you could take a look at my other server/client project. :)

The source is really messy and it really needs to be re-written. But that's what i'm currently doing.

Here are some screenshots

Server

Posted Image

Server: Online (Well... local]

Posted Image

Client

Posted Image

Client & Server: Connected

Posted Image

Full log

[08:00:30] F&S Chat Server [v0.0.1] initialized.
[08:01:56] Starting server...
[08:01:56] Server online.
[08:04:03] Client connected. [IP: 192.168.2.56][ID: 584]
[08:04:44] [ChatRoom] [AlmarM]: Hello! This is my chat! :)
[08:04:57] [ChatRoom] [AlmarM]: What do you think? ^^
[08:05:46] Client [584] disconnected!
[08:05:46] [ChatRoom] 584 has disconnected.
[08:05:56] Closing server...
[08:05:56] Server offline.

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.

Link to comment
Share on other sites

I'm working on an easy to use Chat UDF, using Kip's TCP UDF.

If you want, you could take a look at my other server/client project. :)

The source is really messy and it really needs to be re-written. But that's what i'm currently doing.

Here are some screenshots

Server

Posted Image

Server: Online (Well... local]

Posted Image

Client

Posted Image

Client & Server: Connected

Posted Image

Full log

[08:00:30] F&S Chat Server [v0.0.1] initialized.
[08:01:56] Starting server...
[08:01:56] Server online.
[08:04:03] Client connected. [IP: 192.168.2.56][ID: 584]
[08:04:44] [ChatRoom] [AlmarM]: Hello! This is my chat! :)
[08:04:57] [ChatRoom] [AlmarM]: What do you think? ^^
[08:05:46] Client [584] disconnected!
[08:05:46] [ChatRoom] 584 has disconnected.
[08:05:56] Closing server...
[08:05:56] Server offline.

This is very nice. Thanks for showing me it =D I think I'll learn a lot from this. It looks like this was a pain to make.
Link to comment
Share on other sites

This is very nice. Thanks for showing me it =D I think I'll learn a lot from this. It looks like this was a pain to make.

Well, yes. But more pain is coming.

Like I said, im rewriting it. But this time with my own UDF (using Kip's TCP UDF). :)

I will create functions like

  • _ChatX_CreateServer(ip, port)
  • _ChatX_CreateClient(ip, port)
  • _ChatX_RegisterCommand(command_name, function_to_call, function_parameters)
  • _ChatX_RemoveCommand(command_name)
I hope I can release a version soon in Example Script. :)

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.

Link to comment
Share on other sites

That's an awesome looking setup you've got going there AlmarM, should be pretty cool when you get it (re) done!

I finally got done debugging my scripts from above, all should be in working order now.

At the moment it's hard-coded to run the client and server on the same machine, but changing the places where I put the @IPAddress1 macro can fix that.

Chat like normal, also has commands:

"/nick nickname" to change your nickname

"/msg nickname message" to send a private message to a user

SERVER:

#include <Array.au3>
#Include <String.au3>

TCPStartup()

Global $SERVER, $SOCKET[1] = [""], $NICKS[1] = [""]

$SERVER = TCPListen(@IPAddress1,1337)
If @error Then
    MsgBox(0,"error","Error listening on port 1337.")
    Quit()
EndIf

While 1
    sleep(25)
   
    $TConn = TCPAccept($SERVER)
   
    If $TConn <> -1 Then
        _ArrayAdd($SOCKET,$TConn)
        _ArrayAdd($NICKS,"guest")
    EndIf
   
    recvAll()
    
WEnd

Func recvAll()
    Local $RECV, $cmd, $text
    
    For $Cx = 0 to UBound($SOCKET) - 1
        
        $RECV = ""
        $curLine = ""
        
        If $SOCKET[$Cx] <> "" Then
            
            $RECV = TCPRecv($SOCKET[$Cx], 512)
            If StringLen($RECV) > 7 Then
                
                $RECV = StringSplit($RECV, @CRLF)
                
                For $curLine In $RECV
                
                    $cmd = StringLeft($curLine, 7)
                    $text = StringMid($curLine, 8)
                    
                    Switch $cmd
                    Case "CONNECT"
                        $NICKS[$Cx] = StringReplace($text,"|","_")
                        notifyAll("New user " & $text & " has connected.")
                        sendAllNicks()
                    Case "NICKNAM"
                        changeNick($Cx,$text)
                    Case "DISCONN"
                        notifyAll("User " & $NICKS[$Cx] & " has disconnected.")
                        _ArrayDelete($SOCKET,$Cx)
                        _ArrayDelete($NICKS,$Cx)
                    Case "CHATMSG"
                        chatAll($Cx,$text)
                    Case "USERMSG"
                        chatUser($Cx,$text)
                    Case "NICKLST"
                        sendNicks($SOCKET[$Cx])
                    EndSwitch
                    
                Next
                
            EndIf
        EndIf
    Next
EndFunc

Func sendAllNicks()
    For $user In $SOCKET
        If $user <> "" Then
                sendNicks($user)
        EndIf
    Next
EndFunc

Func sendNicks($user)
        TCPSend($user, "NICKLST" & _ArrayToString($NICKS) & @CRLF)
EndFunc

Func changeNick($userNum,$newNick)
    $newNick = StringReplace($newNick,"|","_")
    If StringLen($newNick) >= 3 Then
        If $NICKS[$userNum] <> $newNick Then
            If _ArraySearch($NICKS,$newNick) == -1 Then
                notifyAll("User " & $NICKS[$userNum] & " is now known as " & $newNick)
                setNick($userNum,$newNick)
                sendAllNicks()
            Else
                notifyUser($SOCKET[$userNum],"Nickname " & $newNick & " is already in use!")
                setNick($userNum, $NICKS[$userNum])
            EndIf
        EndIf
    Else
        notifyUser($SOCKET[$userNum],"Nickname " & $newNick & " is too short. Must be at least 3 characters.")
        setNick($userNum, $NICKS[$userNum])
    EndIf
EndFunc

Func setNick($user, $nick)
    $NICKS[$user] = $nick
    TCPSend($SOCKET[$user], "NICKNAM" & $nick & @CRLF)
EndFunc

Func notifyUser($socket, $msg)
        TCPSend($socket, "NOTIFYM" & $msg & @CRLF)
EndFunc

Func notifyAll($msg)
    For $user In $SOCKET
        If $user <> "" Then
            notifyUser($user, $msg)
        EndIf
    Next
EndFunc

Func chatAll($from,$msg)
    If $msg <> "" Then
        For $Ox = 0 to UBound($SOCKET) - 1
            If $SOCKET[$Ox] <> "" Then
                If TCPSend($SOCKET[$Ox], "CHATMSG" & $NICKS[$from] & "|" & $msg & @CRLF) == 0 And @error Then
                    TCPCloseSocket($SOCKET[$Ox])
                    _ArrayDelete($SOCKET,$Ox)
                    _ArrayDelete($NICKS,$Ox)
                EndIf
            EndIf
        Next
    EndIf
EndFunc

Func chatUser($from,$msg)
    
    Local $to,$text
    
    $to = _StringBetween($msg, "", "|")
    $text = _StringBetween($msg, "|", "")
    
    If $msg <> "" Then
        $userID = _ArraySearch($NICKS,$to[0])
        If $userID <> -1 Then
            If TCPSend($SOCKET[$userID], "USERMSG" & $NICKS[$from] & "|" & $text[0] & @CRLF) == 0 And @error Then
                TCPCloseSocket($SOCKET[$userID])
                _ArrayDelete($SOCKET,$userID)
                _ArrayDelete($NICKS,$userID)
            EndIf
        Else
            notifyUser($SOCKET[$from], "Nickname " & $to[0] & " is not online.")
        EndIf
    EndIf
EndFunc

Func Quit()
    For $Qx = 0 to UBound($SOCKET) - 1
        notifyUser($SOCKET[$Qx], "Server is shutting down...")
        TCPCloseSocket($SOCKET[$Qx])
    Next
    TCPShutdown()
    Exit
EndFunc

CLIENT:

#Include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
#include <String.au3>


Global $username
Global Const $winTitle = "Donald's Instant Messenger"

TCPStartup()

Global $CONNECT = 0, $NICKLIST = ""


GUICreate($winTitle, 500, 500)
GUISetState()

Global $btnConnect = GUICtrlCreateButton("Connect", 10, 10, 100, 30)

Global $info = GUICtrlCreateEdit("", 10, 50, 325, 350, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $edtNicklist = GUICtrlCreateEdit("",340, 50, 150, 350, $ES_READONLY)

Global $sending = GUICtrlCreateInput("", 10, 410, 400, 70)
Global $b_send = GUICtrlCreateButton("Send", 420, 410, 70, 70, BitOR($BS_DEFPUSHBUTTON, $WS_GROUP))


While True
    
    $RECV = ""
    
    If $CONNECT <> 0 Then
        
        $RECV = TCPRecv($CONNECT,512)
        If @error Then destroyConnect()
       
        If StringLen($RECV) > 7 Then
            
            $RECV = StringSplit($RECV, @CRLF)
            
            For $curLine In $RECV
                
                $cmd = StringLeft($curLine, 7)
                $text = StringMid($curLine, 8)
                
                Switch $cmd
                Case "NOTIFYM"
                    displayNotify($text)
                Case "CHATMSG"
                    displayChat($text)
                Case "NICKLST"
                    updateNicks($text)
                Case "USERMSG"
                    displayUserMsg($text)
                Case "NICKNAM"
                    $username = $text
                EndSwitch
            Next
               
        EndIf
    EndIf
   
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        Quit()
    Case $b_send
        say()
    Case $btnConnect
        toggleConnect()
    EndSwitch
   
   
WEnd

Func updateNicks($text)
    
        GUICtrlSetData($edtNicklist, StringReplace($text, "|", @CRLF))
    
EndFunc

Func toggleConnect()
    If TCPSend($CONNECT, "ping" & @CRLF) Then
        destroyConnect()
    Else
        initConnect()
    EndIf
EndFunc

Func initConnect()
    
    If StringLen($username) < 3 Then
            $username= InputBox("Username", "What would you like your username to be?")
    EndIf
   
    displayNotify("Connecting to server...")
   
    $CONNECT = TCPConnect(@IPAddress1 ,1337)
    If @error Then
            MsgBox(16, "Error", "Cannot connect to " & @IPAddress1 & " on port 1337.")
            displayNotify("Unable to connect")
            GUICtrlSetData($btnConnect, "Connect")
            Return
    EndIf
   
    TCPSend($CONNECT, "CONNECT" & $username & @CRLF)
   
    displayNotify("Connected!")
    GUICtrlSetData($btnConnect, "Disconnect")
    
EndFunc

Func destroyConnect()
    
    TCPSend($CONNECT, "DISCONN" & @CRLF)
    TCPCloseSocket($CONNECT)
    $CONNECT = 0
    displayNotify("You have disconnected.")
    GUICtrlSetData($btnConnect, "Connect")
    GUICtrlSetData($edtNickList, "")
    
EndFunc

Func displayNotify($text)
        GUICtrlSetData($info, "*** " & $text & " ***" & @CRLF, True)
EndFunc

Func displayChat($text)
    
    Local $nick, $msg
   
    $nick = _StringBetween($text, "", "|")
    $msg = _StringBetween($text, "|", "")
   
    If $nick[0] == $username Then
            $nick[0] = "* " & $nick[0]
    EndIf
   
    GUICtrlSetData($info, timestamp() & $nick[0] & ": " & $msg[0] & @CRLF, True)
    
EndFunc

Func displayUserMsg($text)
    
    Local $nick, $msg
   
    $nick = _StringBetween($text, "", "|")
    $msg = _StringBetween($text, "|", "")
   
    GUICtrlSetData($info, "** From " & $nick[0] & ": " & $msg[0] & @CRLF, True)
EndFunc

Func timestamp()
    
    Local $timestamp

    $timestamp = "[hh:mm:ss] "
    $timestamp = StringReplace($timestamp, "hh", @HOUR)
    $timestamp = StringReplace($timestamp, "mm", @MIN)
    $timestamp = StringReplace($timestamp, "ss", @SEC)
    
    Return $timestamp
    
EndFunc

Func say()
    $DATA = GUICtrlRead($Sending)
    If $DATA <> "" Then
        If StringLeft($DATA,6) == "/nick " And StringLen($DATA) > 8 Then
            TCPSend($CONNECT, "NICKNAM" & StringMid($DATA,7) & @CRLF)
        ElseIf StringLeft($DATA,5) == "/msg " And StringLen($DATA) > 9 Then
            $DATA = StringTrimLeft($DATA,5)
            $toNick = _StringBetween($DATA, "", " ")
            $toMsg = _StringBetween($DATA, " ", "")
            TCPSend($CONNECT, "USERMSG" & $toNick[0] & "|" & $toMsg[0] & @CRLF)
            GUICtrlSetData($info, "** To " & $toNick[0] & ": " & $toMsg[0] & @CRLF, True)
        Else
            TCPSend($CONNECT, "CHATMSG" & $DATA & @CRLF)
        EndIf
        GUICtrlSetData($sending, "")
    EndIf
EndFunc

Func Quit()
    destroyConnect()
    sleep(50)
    TCPShutdown()
    Exit
EndFunc
Link to comment
Share on other sites

Keep me posted on your progress Donald, I'm interested to see how you solve the problem :)

Will do, Could you help me out on my problem though? I need to get rid of lines with no text in my Users.txt file. I've never tried to before.

Link to comment
Share on other sites

I must be getting tired, took me a bit to write this up. But wanted it to be reliable..

#include <File.au3>
#Include <Array.au3>

Func deleteBlank($filename)
    Local $text[1]
    _FileReadToArray($filename,$text)
    
    For $i=$text[0] To 1 Step -1
        If StringStripWS($text[$i],8) == "" Then
            _ArrayDelete($text,$i)
        EndIf
    Next
    _ArrayDelete($text,0)
    
    _FileWriteFromArray($filename,$text)
EndFunc
Link to comment
Share on other sites

I just got mine to work =D Doing it through a file was easier for me.

Server:

#include <Array.au3>
#Include <File.au3>

TCPStartup()

Global $SERVER, $SOCKET[1]

$SERVER = TCPListen(@IPAddress1,1337)
If @error Then
    MsgBox(0, "", @error)
    MsgBox(0,"error","Error to listening on port 1337.")
    Quit()
EndIf

$username = ""

FileOpen("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt")
FileOpen("Users.txt")

While 1
    sleep(25)
    
    HotKeySet("{END}", "Quit")
    
    $TConn = TCPAccept($SERVER)
    
    If $TConn <> -1 Then
        _ArrayAdd($SOCKET,$TConn)
    EndIf
    
    For $Cx = 0 to UBound($SOCKET) - 1
        If $SOCKET[$Cx] <> "" Then
            $RECV = TCPRecv($SOCKET[$Cx], 512) 
            If @error Then
                TCPCloseSocket($SOCKET[$Cx])
                _ArrayDelete($SOCKET,$Cx)
                _FileWriteToLine("Users.txt", $Cx, "", 1)
                $Names = FileRead("Users.txt")
                sendalluser("USER~" & $Names)
            EndIf
            
            $Name = StringSplit($RECV, "~")
            
            If $Name[1] = "USER" Then
                FileWriteLine("Users.txt", $Name[2])
                $Names = FileRead("Users.txt")
                sendalluser("USER~" & $Names)
            Else
            If $RECV <> "" Then
                sendall($RECV)
            EndIf
        EndIf
        EndIf
    Next
WEnd

Func sendall($msg)
    If $msg <> "" Then
        For $Ox = 0 to UBound($SOCKET) - 1
            If $SOCKET[$Ox] <> "" Then
                FileWriteLine("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt", "[" & @Hour & ":" & @MIN & ":" & @SEC & "] " & $msg)
                If TCPSend($SOCKET[$Ox], "[" & @Hour & ":" & @MIN & ":" & @SEC & "] " & $msg) == 0 And @error Then
                    TCPCloseSocket($SOCKET[$Ox])
                    _ArrayDelete($SOCKET,$Ox)
                    _FileWriteToLine("Users.txt", $SOCKET[$Ox], "", 1)
                    $Names = FileRead("Users.txt")
                    sendalluser("USER~" & $Names)
                EndIf
            EndIf
        Next
    EndIf
EndFunc


Func sendalluser($msg)
    If $msg <> "" Then
        For $Ox = 0 to UBound($SOCKET) - 1
            If $SOCKET[$Ox] <> "" Then
                If TCPSend($SOCKET[$Ox], $msg) == 0 And @error Then
                    TCPCloseSocket($SOCKET[$Ox])
                    _ArrayDelete($SOCKET,$Ox)
                    _FileWriteToLine("Users.txt", $SOCKET[$Ox], "", 1)
                    $Names = FileRead("Users.txt")
                    sendalluser("USER~" & $Names)
                EndIf
            EndIf
        Next
    EndIf
EndFunc

Func Quit()
    For $Qx = 0 to UBound($SOCKET) - 1
        TCPCloseSocket($SOCKET[$Qx])
    Next
    FileDelete("Users.txt")
    FileClose("File log " & @Mon & " " & @MDAY & " " & @YEAR & ".txt")
    TCPShutdown()
    Exit
EndFunc

Client:

#Include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
#include <INET.au3>


Global $username = InputBox("Username", "What would you like your username to be?")

$login = 0

TCPStartup()

Global $CONNECT

$CONNECT = TCPConnect("71.52.190.87" ,1337)
If @error Then
    MsgBox(16, "Error", "Cannot connect to 71.52.190.87 on port 1337.")
    Quit()
EndIf

TCPSend($CONNECT, "USER~" & $username)

$Window = GUICreate("Donald's Instant Messenger", 420, 300)
GUISetState()
Global $info = GUICtrlCreateEdit("Everything that is sent will be logged." & @CRLF, 10, 10, 280, 200, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $Names = GUICtrlCreateEdit("", 300, 10, 100, 280, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFFFFFF)

Global $sending = GUICtrlCreateInput("", 10, 220, 200, 70)
$b_send = GUICtrlCreateButton("Send", 220, 220, 70, 70, BitOR($BS_DEFPUSHBUTTON, $WS_GROUP))

TCPSend($CONNECT, $username & " has connected from " & _GetIP())

While True
    $RECV = TCPRecv($CONNECT,512)
    If @error Then Quit()
        
    $State = WinGetState($Window)
    
    $nameget = StringSplit($RECV, "~")
    
    
    If $nameget[1] = "USER" Then
        GUICtrlSetData($Names, $nameget[2])
    Else
        If $RECV <> "" Then
            GUICtrlSetData($info, $RECV & @CRLF, True)
            If BitAND($State, 16) Then
                WinFlash($Window, "", 10, 4)
            EndIf
        EndIf
    EndIf
    
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Quit()
    Case $msg = $b_send
        say()
    EndSelect
    
    
WEnd


Func say()
    $DATA = GUICtrlRead($Sending)
    If $DATA <> "" Then
        TCPSend($CONNECT, $username & ": " & $DATA)
        GUICtrlSetData($sending, "")
    EndIf
EndFunc

Func Quit()
    TCPSend($CONNECT, $username & " has disconnected.")
    sleep(50)
    TCPCloseSocket($CONNECT) ; Close socket
    TCPShutdown()
    Exit
EndFunc

If you would please run my client and let me see if it's working correctly. I need to do a few tests before I think I'm finished.

Anyone that wants to test it with me can, I have the server up.

Edited by Donald8282
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...