Jump to content

Error 10049 with TCPListen


Recommended Posts

Hey all

I'm writing my first server/client program for practice and I've run into a bit of a wall.

I think I've done everything correctly but no matter what I do I always get error 10049 which is WSAEADDRNOTAVAIL

I know that the value that is being sent for the IP and port is 192.168.1.103 and 12030, of which is the valid address of the machine I'm on and an unused port.

It's line 31 (in the serverStart function) where the bad call happens

Heres my code, sorry if it's terrible spaghetti code by I'm on my own and a rookie:

#include <GUIConstantsEx.au3>

$ip = @IPAddress1;sets the variable $ip to the first ip address in the adapter. Sometimes this is 127.0.0.1 but we want the local IP
$port = 12030;sets the port to a random number between 12000 to 13000
Dim $socketdata[4][4]
$switchbit = 0
$flag1 = 0 

;Checks to make sure that the function to automatically set the IP address did not return 127.0.0.1
if $ip = "127.0.0.1" Then ;and if it did, then set it to the next IP in the list (as some machines have localhost first)
    $ip = @IPAddress2  
EndIf

;Create a GUI for setting the various settings
GUICreate("Server Properties", 280, 200)
$ipcontrol = GUICtrlCreateInput($ip, 35, 25, 150)
GUICtrlCreateLabel ("IP:", 15, 25)
$portcontrol = GUICtrlCreateInput($port, 35, 50, 50)
GUICtrlCreateLabel ("Port:", 5, 50)
GUICtrlCreateLabel ("Don't forget to forward your port in your routers settings", 10, 80)
GUISetState(@SW_SHOW)
$start = GUICtrlCreateButton("Start Server", 50, 120, 75, 50)
$stop = GUICtrlCreateButton("Stop Server", 150, 120, 75, 50)


Func serverStart();Start TCP Services and listen on a port 
$ip = GUICtrlGetState($ipcontrol)
$port = GUICtrlGetState($portcontrol)
TCPStartUp()
$TCPListen = TCPListen ($ip, $port, 1)
If $TCPListen = -1 Then
    MsgBox(1,"TCP Error", @error )
    MsgBox(1,$ip,$port)
    Leave()
    EndIf
EndFunc

while 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $start
        $switchbit = 0
        serverStart()
    Case $msg = $GUI_EVENT_CLOSE
        Leave()
    Case $msg = $stop
        $switchbit = 0 
        serverStop()
    EndSelect
    
    acceptConnection()
WEnd

Func acceptConnection()
    if $switchbit = 0 Then Return
$Accept = TCPAccept($TCPListen)
EndFunc

Func serverStop()
    TCPShutdown()
    Return
EndFunc

Func Leave()
Exit
EndFunc
Edited by centizen
Link to comment
Share on other sites

When you read from a control use GuiCtrlRead() not GuiCtrlGetState().

Replace

$ip = GUICtrlGetState($ipcontrol)
$port = GUICtrlGetState($portcontrol)

with

$ip = GUICtrlRead($ipcontrol)
$port = GUICtrlRead($portcontrol)

When the words fail... music speaks.

Link to comment
Share on other sites

Thanks for your help Andreik, it cleared up the issue I posted about. Unfortunately however, I am now getting a different error code, 10048. Apparently this one means that the port I'm trying to listen on is already in use, but no matter which port I try, (All forwarded in the router beforehand) the error still persists. This is true no matter which computer I run it on as well. I really don't want to give up on this but I can't figure out this new problem either. Is there anything else you can see that might be causing this issue?

And thanks very much for your help in the first place.

Link to comment
Share on other sites

10048 is error code for WSAEADDRINUSE (Address already in use.) Be sure you don't have this script running somewhere and you still try to run because surely will fail or be sure you didn't press twice start server button.

When the words fail... music speaks.

Link to comment
Share on other sites

Thank you so much andreik, the last thing you said was the clicker. I realized that in my If/Else for checking to see if TCPListen returned an error, it would only notify me if the function returned -1, and say nothing if it worked, making me double click the button. I added Elseifs for possible error return values and then made an else congratulating me on getting it to work.

It worked!!

Thanks again, you've been a great help.

Edited by centizen
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...