Sign in to follow this  
Followers 0
Jabberwock

TCP/IP Problems

2 posts in this topic

Hi, umm I was just wondering why my server dosn't always respond to the commands sent, i'll paste the source code of the client/server and mabye one of you could figure it out, i setup a few hotkeys for testing F1-F4 and F5 = Clear

Run it in the beta autoit

;Client


#include <GUIConstants.au3>

$g_IP = InputBox("EQC Client","Enter The Server IP Address",@IPADDRESS1,"",300,200)
$g_port = 33891
HotKeySet("{F1}", "test1")
HotKeySet("{F2}", "test2")
HotKeySet("{F3}", "test3")
HotKeySet("{F4}", "test4")
HotKeySet("{F5}", "clearwindow")
TCPStartUp()

$socket = TCPConnect( $g_IP, $g_port )
;If $socket = -1 Then Exit
If $socket = -1 Then
MsgBox(4096, "ERROR", "Connection was Rejected.", 0)
EndIf
$GOOEY = GUICreate("EQC Client - Status: Online",350,220)
$edit = GUICtrlCreateEdit("",10,40,330,150,$WS_DISABLED)
$input = GUICtrlCreateInput("",10,10,250,20)
$butt = GUICtrlCreateButton("Send",260,10,80,20,$BS_DEFPUSHBUTTON)

$filemenu = GuiCtrlCreateMenu ("File")
$commandsmenu = GuiCtrlCreateMenu ("Commands")
$helpmenu = GuiCtrlCreateMenu ("?")
$aboutitem = GuiCtrlCreateMenuitem ("Creator",$helpmenu)
$exitnow = GuiCtrlCreateMenuitem ("Exit",$filemenu)
$test1 = GuiCtrlCreateMenuitem ("Test1",$commandsmenu)
$test2 = GuiCtrlCreateMenuitem ("Test2",$commandsmenu)
$test3 = GuiCtrlCreateMenuitem ("Test3",$commandsmenu)
$test4 = GuiCtrlCreateMenuitem ("Test4",$commandsmenu)
$clearwindow = GuiCtrlCreateMenuitem ("Clear Console",$commandsmenu)
GUISetState()
While 1
   $msg = GUIGetMsg()
   If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $aboutitem Then
            Msgbox(0,"EQC Client","Jabberwock")
                EndIf
        If $msg = $exitnow Then
            ExitLoop
                EndIf
        If $msg = $test1 Then
            TCPSend( $socket, "~~test1" )
                EndIf
        If $msg = $test2 Then
            TCPSend( $socket, "~~test2" )
                EndIf
        If $msg = $test3 Then
            TCPSend( $socket, "~~test3" )
                EndIf
        If $msg = $test4 Then
            TCPSend( $socket, "~~test4" )
                EndIf
        If $msg = $clearwindow Then
            GUICtrlSetData($edit,"")
                EndIf
   If $msg = $butt Then
      $ret = TCPSend($socket, GUICtrlRead($input))
      If @ERROR Then ExitLoop
      If $ret > 0 Then GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF)
      GUICtrlSetData($input,"")
   EndIf

   $recv = TCPRecv($socket, 512 )
    $err = @error   

   If $recv = "~~rejected" Then
      GUICtrlSetData($edit, GUICtrlRead($edit) & "~~Connection Rejected" & @CRLF)
      WinSetTitle($GOOEY,"","Connection Rejected")
      MsgBox(4096, "@ERROR", "Connection was Rejected.", 0)
      Sleep(2000)
      TCPSend( $socket, "~~whatever")
      ExitLoop
   ElseIf $recv = "~~bye" Then
      GUICtrlSetData($edit, GUICtrlRead($edit) & "~~Connection Lost" & @CRLF)
      WinSetTitle($GOOEY,"","Connection Lost")
      MsgBox(4096, "@ERROR", "Connection was lost.", 0)
      Sleep(2000)
      ExitLoop
   EndIf

   If $err <> 0 Then ExitLoop
   If $err=0 AND $recv <> "" Then GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)
WEnd

Func OnAutoItExit()
   TCPSend( $socket, "~~bye" )
   TCPCloseSocket( $socket )
   TCPShutDown()
EndFunc

Func test1()
TCPSend( $socket, "~~test1" )
EndFunc

Func test2()
TCPSend( $socket, "~~test2" )
EndFunc

Func test3()
TCPSend( $socket, "~~test3" )
EndFunc

Func test4()
TCPSend( $socket, "~~test4" )
EndFunc

Func clearwindow()
GUICtrlSetData($edit,"")
EndFunc

And

;Server


#Include <Constants.au3>
Global $MainSocket, $ConnectedSocket = -1
Global $g_IP = @IPADDRESS1
Global $g_port = 33891
Opt("TrayMenuMode",1)
$exititem = TrayCreateItem("Exit")
; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET"
;==============================================
$MainSocket = TCPListen($g_IP, $g_port,  100 )
If $MainSocket = -1 Then Exit
$RogueSocket = -1

While 1
;special commands
$recv = TCPRecv( $ConnectedSocket, 512 )

If $recv = "~~test1" Then
TCPSend($ConnectedSocket, "Command test1 was received and executed!")
EndIf

If $recv = "~~test2" Then
TCPSend($ConnectedSocket, "Command test2 was received and executed!")
EndIf

If $recv = "~~test3" Then
TCPSend($ConnectedSocket, "Command test3 was received and executed!")
EndIf

If $recv = "~~test4" Then
TCPSend($ConnectedSocket, "Command test4 was received and executed!")
EndIf

;end special commands
$msg = TrayGetMsg()
    Select
        Case $msg = $exititem
            ExitLoop
    EndSelect
   If $RogueSocket > 0 Then
      $recv = TCPRecv( $RogueSocket, 512 )
      If NOT @error Then
         TCPCloseSocket( $RogueSocket )
         $RogueSocket = -1
      EndIf
   EndIf

  ; If no connection look for one
  ;--------------------
   If $ConnectedSocket = -1 Then
      $ConnectedSocket = TCPAccept( $MainSocket)
      If $ConnectedSocket >= 0 Then
          TCPSend($ConnectedSocket, "We are now connected!")
      EndIf
   Else

      $RogueSocket = TCPAccept( $MainSocket)
      If $RogueSocket > 0 Then 
          TCPSend( $RogueSocket, "~~rejected" )
      EndIf
      $recv = TCPRecv( $ConnectedSocket, 512 )
      If @error Or $recv = "~~bye" Then
         TCPCloseSocket( $ConnectedSocket )
         $ConnectedSocket = -1
      EndIf
   EndIf
WEnd
Func OnAutoItExit()
   If $ConnectedSocket > -1 Then 
      TCPSend( $ConnectedSocket, "~~bye" )
      Sleep(2000)
      TCPRecv( $ConnectedSocket,  512 )
      TCPCloseSocket( $ConnectedSocket )
   EndIf
   TCPCloseSocket( $MainSocket )
   TCPShutDown()
EndFunc

Func SOCKET2IP($SHOCKET)
   $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

   $a = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET,"ptr",DLLStructGetPtr($sockaddr), _
                                            "int_ptr",DLLStructGetSize($sockaddr))
   If Not @error And $a[0] = 0 Then
      $a = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
      If Not @error Then $a = $a[0]
   Else
      $a = 0
   EndIf

   DLLStructDelete($sockaddr)

   Return $a
EndFunc

Client.au3

Server.au3


Share this post


Link to post
Share on other sites



Nvm, rewrote it and it was fixed, guess i messed up some where.


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