Jump to content
Sign in to follow this  
BrettF

TCP Help

Recommended Posts

Argh. I can't see why, but I keep getting error 10048 (socket in use) and I don't know why! Here is a snippet of my code.

Server:

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

TCPStartup ()

Dim $nport = 9732
Dim $ConnectedSocket = -1
Dim $szIPADDRESS = @IPAddress1

Dim $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")",300,200)
Dim $edit = GUICtrlCreateEdit("",10,10,280,180)
GUICtrlSetData ($edit, "Initial Startup at " & _Now ())
GUISetState()

$MainSocket = TCPListen($szIPADDRESS, $nPORT)


While 1
    $nmsg = GUIGetMsg ()
    $ConnectedSocket = TCPAccept($MainSocket)
    Select
        Case $nmsg = $GUI_EVENT_CLOSE
            Exit
        Case $ConnectedSocket <> -1
            $szIP_Accepted = SocketToIP($ConnectedSocket)
            $recv = TCPRecv( $ConnectedSocket, 2048 )
            If Not @error Then
                If $recv <> "" Then
                    $text = "[" & $szIP_Accepted & "] " & $recv
                    GUICtrlSetData ($edit, GUICtrlRead ($edit) & @CRLF & $text)
                    Sleep (10)
                    $sock = TCPConnect ($szIP_Accepted, 9732)
                    If $sock = -1 Then GUICtrlSetData ($edit, GUICtrlRead ($edit) & @CRLF & ">> ERROR! UNABLE TO CONNECT TO HOST!")
                    TCPSend ($sock, "RECVOK")
                    If @error Then 
                        GUICtrlSetData ($edit, GUICtrlRead ($edit) & @CRLF & ">> ERROR! UNABLE TO SEND RESPONSE TO HOST " & $szIP_Accepted & "!")
                    Else
                        GUICtrlSetData ($edit, GUICtrlRead ($edit) & @CRLF & ">> Sent 'RECVOK' to host " & $szIP_Accepted)
                    EndIf
                EndIf
            EndIf
    EndSelect
WEnd

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

Func OnAutoItExit ()
    TCPCloseSocket($ConnectedSocket)
    TCPShutDown()
EndFunc

Client:

#include <GUIConstants.au3>
#include <Array.au3>
$objErr = ObjEvent("AutoIt.Error", "MyErrFunc")
TCPStartup()

Global $username, $firstname, $firsttime, $privileges, $time, $birthday, $id
Global $global_serverip = "192.168.0.104"
Global $global_serverport = 9732
Global $global_computerno = 1
Global $global_connection_timeout = 10000
_Login("username", "password", $global_computerno)

Func _Login($user, $pass, $pc_number)
    $socket = TCPConnect($global_serverip, $global_serverport)
    If $socket = -1 Then _Exit(1, "0x000001")
    TCPSend($socket, "/Client " & $user & "/computer" & $global_computerno)
    If @error Then _Exit(1, "0x000002")
    TCPCloseSocket($socket)
    $MainSocket = TCPListen(@IPAddress1, $global_serverport, 128)
    If $MainSocket = -1 Then
        MsgBox(0, "", @error)
        MsgBox(0, "", @extended)
        Exit
    EndIf
    $ConnectedSocket = TCPAccept($MainSocket)
    If $ConnectedSocket <> -1 Then
        $timer = TimerInit()
        While 1
            $recv = TCPRecv($ConnectedSocket, 9000)
            ConsoleWrite(@CR & "> " & $recv)
            If Not @error Then
                If $recv <> "" Then
                    Select
                        Case $recv = "RECVOK"
                            MsgBox(0, "", "RECVOK")
                            ExitLoop
                        Case $recv = "RECVBAD"
                            _Exit(1, "0x000003")
                    EndSelect
                Else
                    ToolTip("nothing yet")
                EndIf
            EndIf
            If TimerDiff($timer) >= $global_connection_timeout Then
                _Exit(2, "0x000004")
            EndIf
        WEnd
    EndIf
EndFunc  ;==>_Login


Func MyErrFunc()
    $hexnum = Hex($objErr.number, 8)
    MsgBox(0, "", "We intercepted an Error!!" & @CRLF & @CRLF & _
            "err.description is: " & $objErr.description & @CRLF & _
            "err.windescription is: " & $objErr.windescription & @CRLF & _
            "err.lastdllerror is: " & $objErr.lastdllerror & @CRLF & _
            "err.scriptline is: " & $objErr.scriptline & @CRLF & _
            "err.number is: " & $hexnum & @CRLF & _
            "err.source is: " & $objErr.source & @CRLF & _
            "err.helpfile is: " & $objErr.helpfile & @CRLF & _
            "err.helpcontext is: " & $objErr.helpcontext & @CRLF & @CRLF & _
            "Please do not close this dialouge, and contact an administrator.")
EndFunc  ;==>MyErrFunc

Func _Exit($type, $code)
    Local $e_text, $e_title
    If $type = 1 Then
        $e_title = "Fatal Error! "
        $e_text = "The system has encountered a fatal error" & @CRLF & @CRLF & _
                "Please contact an administrator and quote error code " & $code & @CRLF & _
                "Your patience is appriciated.  Thank you."
    ElseIf $type = 2 Then
        $e_title = "Connection Timeout!"
        $e_text = "The timeout to server was reached.  Please try again, or contact an administrator.  " & @CRLF & @CRLF & _
                "Your patience is appreiceated.  Thank you."
    EndIf
    MsgBox(0, $e_title, $e_text)
    Exit
EndFunc  ;==>_Exit

Where am I going wrong?

Share this post


Link to post
Share on other sites

I tested your code and it ran perfectly on my computer, had it running over 10 minutes without any problems.

Maybe the problem lies in your firewal or NAT?

Hmmmmmmm... I hope so. Thanks!

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  

×
×
  • Create New...