Jump to content

Problems with TCP functions


Recommended Posts

Hello,

I am trying to write a server/client program for a network and have come upon a problem.

The function SocketToIP (from the Help file for TCPRecv) always returns 0. I have looked at it and found that is because aRet[0] returns -1. But I don't know what that means...

This is my program:

TCPStartup()

$MainSocket = TCPListen(@IPAddress1, 999)
If $MainSocket = -1 Then
    MsgBox(0, "Error", "Socket creation failed.")
    Exit
EndIf

$ConnectedSocket = -1
$ReturnSocket = -1


While 1
    $ReturnSocket = -1
    $remote = reconnect()
    $ReturnSocket = TCPConnect($remote, 1001)
    
    While 1
        $recv = TCPRecv($ConnectedSocket, 2048)
        If @error Then ExitLoop
        
        If $recv <> "" Then
            ;MsgBox(0,"","received: "&$recv)
            GetRecvMeaning($recv, ";;")
        EndIf
    WEnd
    

    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
WEnd

Func GetRecvMeaning($str, $delim)
    $splits = StringSplit($str, $delim, 1)
    Switch StringLower($splits[1])
        [...]
    EndSwitch
EndFunc   ;==>GetRecvMeaning


Func closeServer()
    TCPShutdown()
    Exit
EndFunc   ;==>closeServer

Func reconnect()
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    Sleep(200)
    MsgBox(0,"",$ConnectedSocket)
    $remIP = SocketToIP($ConnectedSocket)
    MsgBox(0,"",$remIp)
    Return $remip
EndFunc   ;==>reconnect

Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "ptr", 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   ;==>SocketToIPoÝ÷ Ø(wµúèØ^rXÛ"uë¬xºÚ"µÍÌÍÐÛÛXÝYÛØÚÙ]HLBÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÝÙ]NNJ

I hope someone can help me with this.

Thanks,

--WhiteAvenger

Link to comment
Share on other sites

Try this... I think the one in the help file is broken. This one might work.

Func SocketToIP($SHOCKET) ;gets ip of connected machine
    Local $sockaddr, $aRet

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

    $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
Edited by SoulA
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...