Jump to content

Problem with tcp connections


Recommended Posts

Client:[autoit]#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_Icon=..\..\..\..\Program Files\AutoIt3\Aut2Exe\Icons\SETUP02.ICO

#AutoIt3Wrapper_Compression=4

#AutoIt3Wrapper_UseAnsi=y

#AutoIt3Wrapper_Res_Comment=TCP Chat Client

#AutoIt3Wrapper_Res_Description=BHIRCC

#AutoIt3Wrapper_Res_Fileversion=0.0.0.1

#AutoIt3Wrapper_Res_LegalCopyright=BlackHat Industries

#AutoIt3Wrapper_Res_Field=Author|Glyph

#AutoIt3Wrapper_Run_Obfuscator=y

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstants.au3>

#include <GuiEdit.au3>

#include <GuiListView.au3>

#include <File.au3>

Opt("TCPTimeout", 50)

Global $list2[1];guilist

$list2[0] = ""

Global $list3[1];name

$list3[0] = ""

$name = ""

$pass = ""

$Port = ""

$Server = ""

Global $Section = "MAIN"

$users = 0

$maxx3 = 6 ;chracters

$maxx4 = 0

$defaultname = ""

$gui = GUICreate("Client v0.4B ALPHA1 | By: Glyph & Rodent", 500, 240)

GUISetBkColor("0x000000", $gui)

$window = GUICtrlCreateEdit("", 10, 10, 380, 175, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))

GUICtrlSetBkColor(-1, "0x000000")

GUICtrlSetColor(-1, "0x11CCFF")

GUICtrlSetFont(-1, 9, 400, "", "Segoe UI")

;GUICtrlSetFont (-1,8.5, 400, "", "Tohoma")

$input = GUICtrlCreateInput("", 10, 190, 325, 20)

GUICtrlSetBkColor($input, "0x000000")

GUICtrlSetColor($input, "0x11CCFF")

GUICtrlSetFont(-1, 8.5, 400, "", "Segoe UI")

$send = GUICtrlCreateButton("Send", 335, 190, 56, 20)

$list = GUICtrlCreateListView("(0) Channel", 400, 10, 90, 200)

GUICtrlSetBkColor(-1, "0x000000")

GUICtrlSetColor(-1, "0xBadCab")

$filemenu = GUICtrlCreateMenu("&File")

$fileitem1 = GUICtrlCreateMenuItem("&Connect", $filemenu)

$fileitem2 = GUICtrlCreateMenuItem("&Disconnect", $filemenu)

GUICtrlCreateMenuItem("", $filemenu)

$fileitem5 = GUICtrlCreateMenuItem("&Register", $filemenu)

$fileitem3 = GUICtrlCreateMenuItem("&Setup", $filemenu)

GUICtrlCreateMenuItem("", $filemenu)

$fileitem4 = GUICtrlCreateMenuItem("&Exit", $filemenu)

$helpmenu = GUICtrlCreateMenu("&Help")

$helpitem1 = GUICtrlCreateMenuItem("&Online ReadMe", $helpmenu)

GUICtrlCreateMenuItem("", $helpmenu)

$helpitem2 = GUICtrlCreateMenuItem("&About", $helpmenu)

Global $data = ""

;MsgBox(0,"",GUICtrlRead($List))

Global $MainSocket

Local $MaxLength = 2048; Maximum Length Of bytes

TCPStartup()

$maxx = 20 ;chracters

$maxx2 = 0

$token = ""

Do

$maxx2 += 1

$token = $token & Chr(Random(33, 126, 1))

Until $maxx2 = $maxx

$MainSocket = -1

GUISetState(@SW_SHOW, $gui)

$setupgui = GUICreate("Setup", 175, 120)

GUICtrlCreateLabel("Username:", 10, 5, 75, 20)

$input1 = GUICtrlCreateInput(IniRead("Config.ini","MAIN","Username",""), 10, 20, 75, 20)

GUICtrlCreateLabel("Password:", 90, 5, 75, 20)

$input2 = GUICtrlCreateInput(IniRead("Config.ini","MAIN","Password",""), 90, 20, 75, 20)

GUICtrlCreateLabel("Server:", 10, 45, 75, 20)

$input3 = GUICtrlCreateInput(IniRead("Config.ini","MAIN","IP",""), 10, 60, 75, 20)

GUICtrlCreateLabel("Port:", 90, 45, 75, 20)

$input4 = GUICtrlCreateInput(IniRead("Config.ini","MAIN","Port",""), 90, 60, 75, 20)

$button1 = GUICtrlCreateButton("Apply/Close", 90, 90, 75, 20)

$button2 = GUICtrlCreateButton("Cancel/Close", 10, 90, 75, 20)

GUISetState(@SW_HIDE, $setupgui)

While 1

Sleep(5)

If WinActive($gui) Then

HotKeySet("{ENTER}", "_send")

Else

HotKeySet("{ENTER}")

EndIf

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then Exit

;connect to server

If $msg = $fileitem3 Then GUISetState(@SW_SHOW, $setupgui)

If $msg = $button1 Then

GUISetState(@SW_HIDE, $setupgui)

EndIf

If $msg = $button2 Then GUISetState(@SW_HIDE, $setupgui)

If $msg = $fileitem1 Then

If $MainSocket = -1 Then

$MainSocket = TCPConnect(TCPNameToIP(GUICtrlRead($input3)), GUICtrlRead($input4))

EndIf

If $MainSocket = -1 Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Client failed to connect!!" & @CRLF)

Else

TCPSend($MainSocket, "`Login" & Chr(29) & GUICtrlRead($input1) & Chr(29) & GUICtrlRead($input2) & Chr(29) & $token & Chr(29) & "Home" & Chr(29) & "4");`Login[]My[]Pass[]Token[]Channel[]Flag[]Version

EndIf

EndIf

;leave server

If $msg = $fileitem2 Then

If $MainSocket = -1 Then _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Client isn't connected!!" & @CRLF)

If $MainSocket <> -1 Then

TCPSend($MainSocket, "`Leave")

EndIf

EndIf

If $msg = $fileitem5 Then

If $MainSocket = -1 Then

$MainSocket = TCPConnect($Server, $Port)

EndIf

If $MainSocket = -1 Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Client failed to connect!!" & @CRLF)

Else

TCPSend($MainSocket, "`Register" & Chr(29) & $name & Chr(29) & $pass)

EndIf

EndIf

;exit

If $msg = $fileitem4 Then

Exit

EndIf

$data = TCPRecv($MainSocket, $MaxLength)

Sleep(10)

$sdata = StringSplit($data, Chr(29))

If $sdata[0] <= 1 Then

If $data <> "" Then

; Unconditional Receive

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] +Server Broadcast+ " & $data & @CRLF)

EndIf

EndIf

If $sdata[0] = 2 Then

If $sdata[1] = "`Join" Then

$users += 1

_ArrayAdd($list2, GUICtrlCreateListViewItem($sdata[2], $list))

_ArrayAdd($list3, $sdata[2])

;_ArrayDisplay($list2,"wee")

GUICtrlSetData($list, "(" & $users & ") Channel")

If $sdata[2] = $name Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] You are now connected." & @CRLF)

Else

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $sdata[2] & " has joined." & @CRLF)

EndIf

EndIf

;HOLY SHI-, THIS WAS CONFUSING! v.v

If $sdata[1] = "`Leave" Then

If $sdata[2] = GUICtrlRead($input1) Then

;TCPCloseSocket($MainSocket)

MsgBox(16, "Client", "You have been disconnected.")

EndIf

If $sdata[2] = Chr(6) Then

Exit

;TCPCloseSocket($mainsocket)

MsgBox(16, "Client", "Server has disconnected.")

EndIf

;+===----;===----;===----;===----;===----;===----

$Pos = _ArraySearch($list3, $sdata[2])

If $Pos > -1 Then

;_ArrayDisplay($list3,$pos&" - "&$sdata[2])

GUICtrlDelete($list2[$Pos])

_ArrayDelete($list2, $Pos)

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $list3[$Pos] & " has left." & @CRLF)

_ArrayDelete($list3, $Pos)

;+===----;===----;===----;===----;===----;===----

$users -= 1

GUICtrlSetData($list, "(" & $users & ") Channel")

EndIf

EndIf

If $sdata[1] = "~Fail" Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Fail: " & $sdata[2] & @CRLF)

TCPCloseSocket($MainSocket)

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Disconnected." & @CRLF)

EndIf

If $sdata[1] = "~Success" Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Success: " & $sdata[2] & @CRLF)

EndIf

EndIf

If $sdata[0] = 3 Then

If $sdata[1] = "~Say" Then

For $lol = 0 To $users

If $list3[$lol] = $sdata[2] Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] <" & $list3[$lol] & "> " & $sdata[3] & @CRLF)

EndIf

Next

EndIf

If $sdata[1] = "~Whisperto" Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] <To:" & $sdata[2] & "> " & $sdata[3] & @CRLF)

EndIf

If $sdata[1] = "~Whisperfrom" Then

_GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] <From:" & $sdata[2] & "> " & $sdata[3] & @CRLF)

EndIf

EndIf

If $msg = $send Then

If $MainSocket = -1 Then _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Client isn't connected!!" & @CRLF)

If $MainSocket <> -1 And GUICtrlRead($input) <> "" Then

TCPSend($MainSocket, "~Say" & Chr(29) & GUICtrlRead($input) & Chr(29) & $token)

GUICtrlSetData($input, "")

EndIf

EndIf

WEnd

Func OnAutoItExit()

If $MainSocket <> -1 Then

TCPSend($MainSocket, "`Leave")

EndIf

TCPShutdown()

EndFunc ;==>OnAutoItExit

Func _send()

If $MainSocket = -1 Then _GUICtrlEdit_AppendText($window, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] Client isn't connected!!" & @CRLF)

If $MainSocket <> -1 And GUICtrlRead($input) <> "" Then

TCPSend($MainSocket, "~Say" & Chr(29) & GUICtrlRead($input) & Chr(29) & $token)

GUICtrlSetData($input, "")

EndIf

EndFunc ;==>_send

; Function to return IP Address from a connected socket.

;--------------------------------------------------------

Func SocketToIP($SHOCKET)

Local $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

Local $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _

"ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))

If Not @error And $aRet[0] = 0 Then

$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))

If Not @error Then $aRet = $aRet[0]

Else

$aRet = 0

EndIf

$sockaddr = 0

Return $aRet

EndFunc ;==>SocketToIP

#cs

$var = IniReadSectionNames(@WorkingDir & "\config.ini")

If @error = 1 Then

GUICtrlSetData($Combo4, "User Profiles|MAIN|(Open Config.ini for Editing)", "User Profiles")

Else

GUICtrlSetData($Combo4, "User Profiles|MAIN|")

For $i = 3 To $var[0]

GUICtrlSetData($Combo4, $var[$i] & "|", "User Profiles")

Next

EndIf

#ce

Func _GUICtrlCreatecheckbox($rText, $rLeft, $rTop, $rLength, $rHieght, $rBackColor = "", $rTextColor = "")

Local $PCcheck = GUICtrlCreateCheckbox("", $rLeft, $rTop, 15, 15)

Local $PCLabel = GUICtrlCreateLabel($rText, $rLeft + 15, $rTop, $rLength - 15, $rHieght)

If $rTextColor <> "" Then GUICtrlSetColor(-1, $rTextColor)

If $rBackColor <> "" Then GUICtrlSetBkColor(-1, $rBackColor)

Return $PCcheck

EndFunc ;==>_GUICtrlCreatecheckbox

tolle indicium

Link to comment
Share on other sites

If anyone has an example of how to correctly disconnect a socket without having the need to restart the whole application, it would be incredibly appreciated!

tolle indicium

Link to comment
Share on other sites

If there's something you don't understand, please let me know.

Help!

Dude. Its the weekend, people are out doing things. You already bumped three times, mods frown upon this. I wouldn't bump more than once a day.

Also put a clear description of the problem before the code, so it doesn't seem like an after thought.

Link to comment
Share on other sites

Dude. Its the weekend, people are out doing things. You already bumped three times, mods frown upon this. I wouldn't bump more than once a day.

Also put a clear description of the problem before the code, so it doesn't seem like an after thought.

Sorry for being an annoyance :/

You won't see this bumped for a few days then.

I tried describing the problem as clearly as possible in the first post, so if you don't understand that then let me know...

tolle indicium

Link to comment
Share on other sites

Try uncommenting the TCPCloseScoket().

Still have the same problem, but now it's a stray open socket because it's clsoing a socket that isn't open.

The server handles the disconnect when the packet "`leave" has been received, right before it disconnects the socket it sends another "`leave[]name" to the clients, then actaulyl disconnects the client.

tolle indicium

Link to comment
Share on other sites

Why would it close a socket which isn't open if you have a variable equal to the one it connects to?

The server itself closes the cleint socket, if the client then closes the socket to the server, it will be closing something that's already closed.

I just realized I left something out, i've been testing this all on my local pc, think that would have any impact?

Edited by BackStabbed

tolle indicium

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