Jump to content

TCP multiclient


Recommended Posts

ok well i have read some other peoples scripts IT IS BEYOND ME so i was peicing my own together EVER slowely and learnig the way.. any way i got a regular chat between one server and one client, i HAVE seen the topics about the multiclients andliie i said im being a turtle Drfrankenstien, lol anywho i was waondering why i get an array error when i have previously GLOBALed the varialbe with an array, here is my whole server script (if i am correct the client wont be needed becuase the client doesnt need to know how many OTHER clients are connected to the server so one clinet+one server should still use the same clinet am i right?

#Include <GUIConstantsEx.Au3>
#Include <WindowsConstants.Au3>
#Include <EditConstants.Au3>
#Include <StaticConstants.Au3>
#Include <Misc.Au3> 
#Include <GUIEdit.Au3>
#Include <Date.Au3>
#Include <File.Au3>
Opt ('GUIOnEventMode','1')

If FileExists(@Scriptdir & '\Server.ini') then
Else 
    IniWrite(@Scriptdir & '\Server.ini','#Server','Username','Server')
    IniWrite(@Scriptdir & '\Server.ini','#Server','Display_Picture','')
    IniWrite(@Scriptdir & '\Server.ini','#Server','Server_Ip',@IPAddress1)
    IniWrite(@Scriptdir & '\Server.ini','#Server','Server_Port','1234')
    IniWrite(@Scriptdir & '\Server.ini','#Font','Font_Name','WST_czec')
    IniWrite(@Scriptdir & '\Server.ini','#Font','Font_Color','0x00FF00')
    IniWrite(@Scriptdir & '\Server.ini','#Font','Font_Size','10')
    IniWrite(@Scriptdir & '\Server.ini','#Font','Console_Color','0x000000')
    IniWrite(@Scriptdir & '\Server.ini','#Font','Send_Color','0x000000')
EndIf

TcpStartUp ()
#Region;GLOBAL VARIABLES;
Global $Last

Global $Server_IP = IniRead (@ScriptDir & '\Server.ini','#Server','Server_IP','')

If IniRead (@ScriptDir & '\Server.ini','#Server','Lan_Mode','') = 'True' Then $Server_IP = @IpAddress1 
    
Global $Server_Port = IniRead (@ScriptDir & '\Server.ini','#Server','Server_Port','')
Global $Username = IniRead (@ScriptDir & '\Server.ini','#Server','Username','')
Global $Server = TcpListen ($Server_IP, $Server_Port, 30)

Global $maxconnect = 29
$max = $maxconnect + 1

Global $Connected_Socket[$max]

for $i = 1 to $maxconnect step 1
    $Connected_Socket[$i] = -1
Next
#EndRegion

If $Server = -1 Then Exit MsgBox (16,'Error Code 01','Cannot Start Server On IP : ' & $Server_IP & ', Port : ' & $Server_Port, 0)


$GUI = GUICreate ('TCP Chat Server - Started On IP : ' & $Server_IP & ', Port : ' & $Server_Port ,700,350,-1,-1,1,BitOR(128, $WS_EX_TOPMOST ))
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')
#Region;MENU|MENUITEM:
$File = GUICtrlCreateMenu('File')
$User_Menu = GUICtrlCreateMenu('User',$File)
$Change = GUICtrlCreateMenu('Change',$File)
$Send_Menu = GUICtrlCreateMenu('Send',$File)

$Exit = GUICtrlCreateMenuItem('Exit',$File)
GUICtrlSetOnEvent($Exit,'_Exit')

$Disconnect_Client = GUICtrlCreateMenuItem('Disconnect Client',$User_Menu)
GUICtrlSetOnEvent($Disconnect_Client,'_Disconnect_Client_VIA_Menu')

$C_User_Name = GUICtrlCreateMenuItem('User Name',$Change)
GUICtrlSetOnEvent($C_User_Name,'_Change_User_Name')

$C_Dis_Pic = GUICtrlCreateMenuItem('Display Picture',$Change)
GUICtrlSetOnEvent($C_Dis_Pic,'_Change_Display_Picture')

$C_Bckgrnd_C = GUICtrlCreateMenuItem('Console Background Color',$Change)
GUICtrlSetOnEvent($C_BckGrnd_C,'_Change_Console_Background')

$S_Bckgrnd_C = GUICtrlCreateMenuItem('Send Background Color',$Change)
GUICtrlSetOnEvent($S_BckGrnd_C,'_Change_Send_Background')

$C_Font_C = GUICtrlCreateMenuItem('Font',$Change)
GUICtrlSetOnEvent ($C_Font_C, '_Change_Font')


$S_File = GUICtrlCreateMenuItem('File',$Send_Menu)
$S_Pic = GUICtrlCreateMenuItem('Picture',$Send_Menu)

#EndRegion

#Region;COLOR & INIREAD;
$Font = IniRead(@ScriptDir & '\Server.ini','#Font', 'Font_Name', '')
$Font_C = IniRead(@ScriptDir & '\Server.ini','#Font', 'Font_Color', '')
$Font_S = IniRead(@ScriptDir & '\Server.ini','#Font','Font_Size','')
$fontcolor = 0

$BckColor = 0
$Bckgrnd_CC = IniRead(@ScriptDir & '\Server.ini','#Font', 'Console_Color', '')
$Bckgrnd_CS = IniRead(@ScriptDir & '\Server.ini','#Font', 'Send_Color', '')
$Dis_Pic_Ini = IniRead(@ScriptDir & '\Server.ini','#Server','Display_Picture','')
#EndRegion

#Region;PICTURES;
$Client_Pic = GUICtrlCreatePic(@Scriptdir & '\None.bmp',550,0,145,150)
$Dis_Pic = GUICtrlCreatePic($Dis_Pic_Ini,550,155,145,150)
$BIN_Pic = 0
$Open_Pic = 0
#EndRegion

#region;EDITS;
$Console = GUICtrlCreateEdit ('',0,0,550,230,2103360)
GUICtrlSetFont ($Console, $Font_S,'','',$Font)
GUICtrlSetBkColor ($Console, $Bckgrnd_CC)
GUICtrlSetColor ($Console, $Font_C)
$Send = GUICtrlCreateEdit ('',0,230,550,75,2101248)
GUICtrlSetFont ($Send, $Font_S,'','',$Font)
GUICtrlSetColor ($Send, $Font_C)
GUICtrlSetBkColor ($Send, $Bckgrnd_CS)
GUICtrlSetState ($Send, $GUI_FOCUS)
GUISetState (@SW_SHOW, $GUI)
#EndRegion

#Region;USERNAME GUI;
$User_Name_GUI = GUICreate('Previous Name = ' & $Username,300,20,-1,-1,-1,BitOR(128, $WS_EX_TOPMOST ))
$Input = GUICtrlCreateInput ($Username, 0,0,250,20,1)
$User_Ok = GUICtrlCreateButton('Ok',250,0,50,20)
GUICtrlSetOnEvent($User_Ok,'_User_Ok')
GUICtrlSetFont ($Input, 9,'','',$Font)
GUISetState (@SW_HIDE, $User_Name_GUI)
#EndRegion

#Region;CONSOLE LOG;
_Console_Log ('Loading Settings...')
_Console_Log ('IP : ' & $Server_IP & ', Port : ' & $Server_Port & '...')
_Console_Log ('Username : ' & $Username)
_Console_Log ('Server Has Started.')
_Console_Log ('Waiting For Client To Connect.')
#EndRegion

_Accept_Connection ()

While 1
If WinActive ($GUI) And _IsPressed ('0D') = 1 Then _Send ()
$Data = TcpRecv ($Connected_Socket, 1000000000)

If IsBinary($Data) == 1 Then
    _Set_Client_Pic()
EndIf   

If StringInStr ($Data, '/Client.Disconnected') == 1 Then 
    _Disconnected ()
    ElseIf $Data <> '' Then 
    _Console_Log ($Data)
    SoundPlay ('Alert.wma',0)
    EndIf 
Sleep (10)
WEnd

Func _Set_Client_Pic()
    GUICtrlSetImage($Client_Pic,$Data)
    GUICtrlSetImage($Client_Pic,$Data)
EndFunc

Func _IsEmpty ($String)
FileWrite ('SCheck.txt', $String)
$Count = _FileCountLines ('SCheck.txt')
Local $Check[$Count + 5]
For $Array = 1 To $Count
$sRead = FileReadLine ('SCheck.txt', $Count)
If $sRead = '' Then $Check[$Array] = 0
Next
FileDelete ('SCheck.txt')
For $Array = 1 To $Count
If $Check[$Array] = 1 Then Return 0 
Next 
Return 1
EndFunc

Func _Disconnect_Client_VIA_Menu ()
    TCPSend($Connected_Socket,'/Server Has Closed...')
    TcpCloseSocket ($Connected_Socket)
EndFunc

Func _Change_Display_Picture ()
    $Open_Pic = FileOpenDialog('Display Picture', 'My Pictures','all (*)','','',$GUI)
    GUICtrlSetImage ( $Dis_Pic,$Open_Pic)
    IniWrite(@ScriptDir & '\Server.ini','#Server','Display_Picture',' ' & $Open_Pic)
EndFunc

Func _Send_File ()
EndFunc

Func _Change_User_Name ()
    GUISetState(@SW_SHOW,$User_Name_GUI)
    GUISetState(@SW_HIDE, $GUI)
EndFunc

Func _User_Ok ()
    IniWrite(@ScriptDir & '\Server.ini','#Server','Username',GUICtrlRead($Input))
    GUISetState(@SW_HIDE  ,$User_Name_GUI)
    GUISetState(@SW_SHOW, $GUI)
EndFunc

Func _Change_Font ()
    $fontcolor = _ChooseFont('arial',8,0xFF0000,0,0,0,0,$GUI)
    If (@error) Then
    MsgBox(0, "", "Error _Change_Font: " & @error)
Else
    GUICtrlSetFont($Console, $fontcolor[3], '', '',$fontcolor[2])
    GUICtrlSetFont($Send, $fontcolor[3], '', '',$fontcolor[2])
    GUICtrlSetColor($Console, $fontcolor[7])
    GUICtrlSetColor($Send, $fontcolor[7])
    IniWrite(@ScriptDir & '\Server.ini','#Font','Font_Name',' ' & $fontcolor[2])
    IniWrite(@ScriptDir & '\Server.ini','#Font','Font_Color',' ' & $fontcolor[7])
    IniWrite(@ScriptDir & '\Server.ini','#Font','Font_Size',' ' & $fontcolor[3])
EndIf
EndFunc

Func _Change_Console_Background ()
    $bckcolor = _ChooseColor(2, 0x000000, 1, $GUI)
    GUICtrlSetBkColor($Console, $bckcolor)
    IniWrite(@ScriptDir & '\Server.ini','#Font','Console_Color',' ' & $bckColor)
EndFunc

Func _Change_Send_Background ()
    $bckcolor = _ChooseColor(2, 0x000000, 1, $GUI)
    GUICtrlSetBkColor($Send, $bckcolor)
    IniWrite(@ScriptDir & '\Server.ini','#Font','Send_Color',' ' & $bckColor)
EndFunc

Func _Send ()
If GUICtrlRead ($Send) <> '' And GUICtrlRead ($Send) <> $Last Then 
$What_2_Send = ($Username & ' Say(s) : ' & GUICtrlRead($Send))
$Tcp_Send = TcpSend ($Connected_Socket, $What_2_Send)
If $Tcp_Send >= 1 Then 
_Console_Log ($What_2_Send)
Else 
_Disconnected ()
EndIf 
EndIf 
$Last = GUICtrlRead ($Send)
GUICtrlSetData ($Send, '')
EndFunc

Func _Console_Log ($Data)
FileWriteLine (@TempDir & '\Server.log', $Data) 
$Read = FileRead (@TempDir & '\Server.log')
GUICtrlSetData ($Console, $Read)
_GUICtrlEdit_LineScroll ($Console, 0, _GUICtrlEdit_GetLineCount ($Console) - 1)
EndFunc

Func _Accept_Connection ()
Do
Sleep (10)
$Connected_Socket = TcpAccept ($Server)
Until $Connected_Socket <> -1
SoundPlay ('Online.wma',0)
_Console_Log ('Client Has Connected.')
EndFunc

Func _Disconnected ()
TcpCloseSocket ($Connected_Socket)
$Connected_Socket = -1
_Console_Log ('Client Has Disconnected.')
Sleep (500)
_Console_Log ('Waiting For Client To Connect...')
_Accept_Connection ()
EndFunc

Func _Exit ()
$Username = IniRead(@ScriptDir & '\Server.ini','#Server','Username','')

tcpshutdown()
Exit 
EndFunc

Func OnAutoItExit ()
TcpSend ($Connected_Socket[$i], '/Disconnect');<====ERROR HERE;
IniWrite (@ScriptDir & '\Server.ini','#Server','Username',' ' & $Username)
If FileExists (@TempDir & '\Server.log') Then 
$Msg = MsgBox (BitOr (32,4),'Delete?','Would You Like To Delete The Server Logs?',0)
If $Msg <> 7 Then FileDelete (@TempDir & '\Server.log')
EndIf 
EndFunc

and this is a copy of the error message

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\TCP\#Server.au3"

C:\TCP\#Server.au3 (271) : ==> Subscript used with non-Array variable.:

TcpSend ($Connected_Socket[$i], '/Disconnect')

TcpSend ($Connected_Socket^ ERROR

>Exit code: 1 Time: 2.028

Edited by bob00037
Link to comment
Share on other sites

Yepp, I took a look at it.. and the program is right.. it isn't an array. :)

Take a look here.

Func _Accept_Connection ()
Do
Sleep (10)
$Connected_Socket = TcpAccept ($Server)
Until $Connected_Socket <> -1
SoundPlay ('Online.wma',0)
_Console_Log ('Client Has Connected.')
EndFunc

Func _Disconnected ()
TcpCloseSocket ($Connected_Socket)
$Connected_Socket = -1
_Console_Log ('Client Has Disconnected.')
Sleep (500)
_Console_Log ('Waiting For Client To Connect...')
_Accept_Connection ()
EndFunc

On lines 248 and 256, you declared the variable $Connected_Socket, therefore I believe (and I could be wrong) that the program no longer looked at your variable as an array.

I'm a total programming noob, but I just saw it, and figured that I'd try to help (even though there's a huge possibility that I could be 100% wrong). :o

Good luck!

Edited by bundyxc
Global $arr[2]

$arr[0]="hip"
$arr[1]="hip"
;^^ hip hip array. ^^
Link to comment
Share on other sites

well i tend to agreee with you but im not sure.. so idk lol i have already tried declaring the array in the func and nadda...

Link to comment
Share on other sites

im sorry to do this i KNOW this is annoying but ughhh

BUMP!!!

anyone know how i can

#1 fix the array nonesense

#2 actually do a multiclient better

#3 should i just use MSN instead xD

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