Jump to content

TCPListen: Socket Creation Failed


Recommended Posts

Hi,

I'm trying to write a network chat program and am using the following code:

$remIP = "192.168.1.117"

TCPStartup()

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

192.168.1.117 is my address within the network.

If I try this code on my own PC, it works fine. However, as soon as I try it on a PC with a different IP, it comes back with the error: Socket creation failed.

Can anyone offer advice as to why this may be happening?

Thanks,

--WhiteAvenger

Link to comment
Share on other sites

I don't really know what you're talking about ..

You're trying the SAME application with your computer's IP address on another computer with a different IP address?

How would that return success ?

What i suggest is to use @IPAddress1 macro .

Link to comment
Share on other sites

I don't really know what you're talking about ..

You're trying the SAME application with your computer's IP address on another computer with a different IP address?

How would that return success ?

What i suggest is to use @IPAddress1 macro .

Wait - the IP address I use for TCPListen, should that always be the IP of the PC the program is running on?

I thought it was meant to be the IP I was listening for...

Link to comment
Share on other sites

Yes , it is . However, are you trying to make a server-client based application ?

If so , you need to use the TCPConnect function instead of TCPListen on the client ..

Use TCPListen only on the server.

Yup, I'm doing that.

However, I now have one more problem: How do I find out the IP of the PC that connected? I've got the following script from the help file, but it's always returning 0:

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
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   ;==>SocketToIP

But I keep getting $remIP: Used before being declared errors.

Link to comment
Share on other sites

As shown in the script , there shouldn't be a problem ..

Would you mind posting a bigger part of the script so it's easier to see what's wrong ?

Of course:

TCPStartup()

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

$ConnectedSocket = -1


While 1
    reconnect()
    $ReturnSocket = TCPConnect($remIP, 1001)
    ;MsgBox(0,"","connected")

    While 1
        $recv = TCPRecv($ConnectedSocket, 2048)
        If @error Then ExitLoop
        
        If $recv <> "" Then
            MsgBox(0,"","received: "&$recv)
        EndIf
    WEnd

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


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

Func reconnect()
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    Sleep(200)
    $remIP = SocketToIP($ConnectedSocket)
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   ;==>SocketToIP
Link to comment
Share on other sites

I know what's wrong already ..

Replace the previous reconnect() function with this

Func reconnect()
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket = -1
    Sleep(200)
    $remIP = SocketToIP($ConnectedSocket)
Return $remip
EndFunc   ;==>reconnect

Instead..

The error occured because $remip was not declared before it was used in the TCPConnect..

Link to comment
Share on other sites

I know what's wrong already ..

Replace the previous reconnect() function with this

Func reconnect()
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket = -1
    Sleep(200)
    $remIP = SocketToIP($ConnectedSocket)
Return $remip
EndFunc   ;==>reconnect

Instead..

The error occured because $remip was not declared before it was used in the TCPConnect..

Of course - it's a local instead of a global, isn't it? Stupid of me.

But still - $ip is 0 after connecting, so the script can't connect back to the client...

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