WhiteAvenger Posted January 6, 2009 Share Posted January 6, 2009 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: expandcollapse popupTCPStartup() $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 More sharing options...
SoulA Posted January 6, 2009 Share Posted January 6, 2009 (edited) 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 January 6, 2009 by SoulA Link to comment Share on other sites More sharing options...
WhiteAvenger Posted January 6, 2009 Author Share Posted January 6, 2009 Yup, that seems to help. Thanks a lot! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now