Sign in to follow this  
Followers 0
Madza91

TCPSend error...

12 posts in this topic

Hi all...i have some questions about TcpSend... i run example server script (on my computer) than client and works normal, but when send that client script, for someone works - for someone not, why ? Is that because 'someone' have dinamic IP or something like that? :S

Server:

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

$g_IP = @IPAddress1;in client i set xxx.xxx.xxx.xxx only for security reasons:)
TCPStartup()

$MainSocket = TCPListen($g_IP, 65432, 100)
If $MainSocket = -1 Then Exit
$RogueSocket = -1

$GOOEY = GUICreate("my server", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 40, 280, 150, $WS_DISABLED)
$input = GUICtrlCreateInput("", 10, 10, 200, 20)
$butt = GUICtrlCreateButton("Send", 210, 10, 80, 20, $BS_DEFPUSHBUTTON)
GUISetState()

Dim $ConnectedSocket = -1

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $butt Then
        If $ConnectedSocket > -1 Then
            $ret = TCPSend($ConnectedSocket, GUICtrlRead($input))
            If @error Or $ret < 0 Then
                TCPCloseSocket($ConnectedSocket)
                WinSetTitle($GOOEY, "", "my server - Client Disconnected")
                $ConnectedSocket = -1
            ElseIf $ret > 0 Then
                GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF)
            EndIf
        EndIf
        GUICtrlSetData($input, "")
    EndIf

    If $RogueSocket > 0 Then
        $recv = TCPRecv($RogueSocket, 512)
        If Not @error Then
            TCPCloseSocket($RogueSocket)
            $RogueSocket = -1
        EndIf
    EndIf

    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket < 0 Then
            $ConnectedSocket = -1
        Else
            WinSetTitle($GOOEY, "", "my server - Client Connected")
        EndIf
    Else
        $RogueSocket = TCPAccept($MainSocket)
        If $RogueSocket > 0 Then
            TCPSend($RogueSocket, "~~rejected")
        EndIf

        $recv = TCPRecv($ConnectedSocket, 512)

        If $recv <> "" And $recv <> "~~bye" Then
            GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)

        ElseIf @error Or $recv = "~~bye" Then
            WinSetTitle($GOOEY, "", "my server - Client Disconnected")
            TCPCloseSocket($ConnectedSocket)
            $ConnectedSocket = -1
        EndIf
    EndIf
WEnd

GUIDelete($GOOEY)

Func OnAutoItExit()
    If $ConnectedSocket > -1 Then
        TCPSend($ConnectedSocket, "~~bye")
        Sleep(2000)
        TCPRecv($ConnectedSocket, 512)
        TCPCloseSocket($ConnectedSocket)
    EndIf
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc   ;==>OnAutoItExitoÝ÷ Ø)bz{ZºÚ"µÍÚ[ÛYH ÑÕRPÛÛÝ[Ë]LÉÝÂÔÝ

B[H ÌÍÜÞÙÈH   ][ÝÞ  ][ÝÂ[H    ÌÍÜÞTQTÔÈHÔ[YUÒT
    ÌÍÜÞÙÊB[H ÌÍÛÔH
MÌ[H   ÌÍÐÛÛXÝYÛØÚÙ]HLBÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÜÞTQTÔË ÌÍÛÔ
B[H ÌÍÜÞ]BYÜ[ÙÐÞ
LL  ][ÝÑÜ][ÝË  ][ÝÕÔÛÛXÝZ[]ÔÐHÜ    ][ÝÈ  [ÈÜB[ÙBÚ[HB ÌÍÜÞ]HH[]Þ
    ][ÝÑ]HÜÙ][ÝË  [È [È ][ÝÑ[][ÛZ]ÈHÑTT][ÝÊBYÜÜ ÌÍÜÞ]HH ][ÝÉ][ÝÈ[^]ÛÜÔÙ[
    ÌÍÐÛÛXÝYÛØÚÙ]TYÜÌH  [È ][ÝÈ  ][ÝÈ  [ÉÌÍÜÞ]JBYÜ[^]ÛÜÑ[[Y

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites



Dim $szServerPC = "xxx.xxx.xxx.xxx"

Dim $szIPADDRESS = TCPNameToIP($szServerPC)

It looks like you are using an IP address for $szServerPC, so then you do not need TCPNameToIP.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

That example script is from Help...

Btw, i set now something like this, and pls wait to send to someone to check -works or not... :)

;Dim $szServerPC = "xxx.xxx.xxx.xxx" - removed line...

Dim $szIPADDRESS = "xxx.xxx.xxx.xxx"


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Same thing... again error, can't connect with client on some else computer...


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Dim $szServerPC = @ComputerName
Dim $szIPADDRESS = TCPNameToIP($szServerPC)

$szServerPC should = @ComputerName

When you're trying to get the IP Address for that computer... It's going to call up the var ... $szServerPC, which is set to your computer's name... which is "@ComputerName".

$szIPADDRESS = TCPNameToIP($szServerPC)

Takes your computer's name and converts it to the IP Address.

Edited by RyGoTypE

Share this post


Link to post
Share on other sites

he meant to create server and client which will communicate between themselves over internet, and not over local network, so his IP should be:

#include <Inet.au3>
$My_IP = _GetIP()

right?, well I tried using this and I got WSA error 10049

WSAEADDRNOTAVAIL

Error Number: 10049

Cannot assign requested address. 

The requested address is not valid in its context. Normally 
results from an attempt to bind to an address that is not 
valid for the local machine, or connect/sendto an address 
or port that is not valid for a remote machine.

why's this?

Share this post


Link to post
Share on other sites

Okay, try this...

Server:

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

Dim $szServerPC = @ComputerName
Dim $g_IP = TCPNameToIP($szServerPC)
TCPStartup()

$MainSocket = TCPListen($g_IP, 65432, 100)
If $MainSocket = -1 Then Exit
$RogueSocket = -1

$GOOEY = GUICreate("my server", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 40, 280, 150, $WS_DISABLED)
$input = GUICtrlCreateInput("", 10, 10, 200, 20)
$butt = GUICtrlCreateButton("Send", 210, 10, 80, 20, $BS_DEFPUSHBUTTON)
GUISetState()

Dim $ConnectedSocket = -1

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $butt Then
        If $ConnectedSocket > -1 Then
            $ret = TCPSend($ConnectedSocket, GUICtrlRead($input))
            If @error Or $ret < 0 Then
                TCPCloseSocket($ConnectedSocket)
                WinSetTitle($GOOEY, "", "my server - Client Disconnected")
                $ConnectedSocket = -1
            ElseIf $ret > 0 Then
                GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF)
            EndIf
        EndIf
        GUICtrlSetData($input, "")
    EndIf

    If $RogueSocket > 0 Then
        $recv = TCPRecv($RogueSocket, 512)
        If Not @error Then
            TCPCloseSocket($RogueSocket)
            $RogueSocket = -1
        EndIf
    EndIf

    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket < 0 Then
            $ConnectedSocket = -1
        Else
            WinSetTitle($GOOEY, "", "my server - Client Connected")
        EndIf
    Else
        $RogueSocket = TCPAccept($MainSocket)
        If $RogueSocket > 0 Then
            TCPSend($RogueSocket, "~~rejected")
        EndIf

        $recv = TCPRecv($ConnectedSocket, 512)

        If $recv <> "" And $recv <> "~~bye" Then
            GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)

        ElseIf @error Or $recv = "~~bye" Then
            WinSetTitle($GOOEY, "", "my server - Client Disconnected")
            TCPCloseSocket($ConnectedSocket)
            $ConnectedSocket = -1
        EndIf
    EndIf
WEnd

GUIDelete($GOOEY)

Func OnAutoItExit()
    If $ConnectedSocket > -1 Then
        TCPSend($ConnectedSocket, "~~bye")
        Sleep(2000)
        TCPRecv($ConnectedSocket, 512)
        TCPCloseSocket($ConnectedSocket)
    EndIf
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc   ;==>OnAutoItExitoÝ÷ Ø)bz{ZºÚ"µÍÚ[ÛYH ÑÕRPÛÛÝ[Ë]LÉÝÂÔÝ

B[H ÌÍÜÞÙÈHÛÛ][YB[H ÌÍÜÞTQTÔÈHÔ[YUÒT
    ÌÍÜÞÙÊB[H ÌÍÛÔH
MÌ[H   ÌÍÐÛÛXÝYÛØÚÙ]HLBÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÜÞTQTÔË ÌÍÛÔ
B[H ÌÍÜÞ]BYÜ[ÙÐÞ
LL  ][ÝÑÜ][ÝË  ][ÝÕÔÛÛXÝZ[]ÔÐHÜ    ][ÝÈ  [ÈÜB[ÙBÚ[HB ÌÍÜÞ]HH[]Þ
    ][ÝÑ]HÜÙ][ÝË  [È [È ][ÝÑ[][ÛZ]ÈHÑTT][ÝÊBYÜÜ ÌÍÜÞ]HH ][ÝÉ][ÝÈ[^]ÛÜÔÙ[
    ÌÍÐÛÛXÝYÛØÚÙ]TYÜÌH  [È ][ÝÈ  ][ÝÈ  [ÉÌÍÜÞ]JBYÜ[^]ÛÜÑ[[Y

Share this post


Link to post
Share on other sites

RyGoTypE, tnx for your reply, but sandin was right, I need server+client over internet, and not LAN and

Dim $szServerPC = @ComputerName

Dim $g_IP = TCPNameToIP($szServerPC)

are statements only for LAN (at least I think so...). Is it posible to have client+server connected to eachother over internet?


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

RyGoTypE, tnx for your reply, but sandin was right, I need server+client over internet, and not LAN and

Dim $szServerPC = @ComputerName

Dim $g_IP = TCPNameToIP($szServerPC)

are statements only for LAN (at least I think so...). Is it posible to have client+server connected to eachother over internet?

Yeah I'm trying to figure that out my self right now. I keep getting that error from the client over the internet.

Share this post


Link to post
Share on other sites

Ok, if you figure out tell us :)


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Using the following code, I was able to get this working over the internet...

Server:

;SERVER!! Start Me First !!!!!!!!!!!!!!!
#include <GUIConstants.au3>

Dim $szServerPC = @ComputerName
Dim $g_IP = TCPNameToIP($szServerPC)

; Start The TCP Services
;==============================================
TCPStartUp()

; Create a Listening "SOCKET"
;==============================================
$MainSocket = TCPListen($g_IP, 65432,  100 )
If $MainSocket = -1 Then Exit
$RogueSocket = -1

; Create a GUI for chatting
;==============================================
$GOOEY = GUICreate("my server",300,200)
$edit = GUICtrlCreateEdit("",10,40,280,150,$WS_DISABLED)
$input = GUICtrlCreateInput("",10,10,200,20)
$butt = GUICtrlCreateButton("Send",210,10,80,20,$BS_DEFPUSHBUTTON)
GUISetState()

; Initialize a variable to represent a connection
;==============================================
Dim $ConnectedSocket = -1

; GUI Message Loop
;==============================================
While 1
   $msg = GUIGetMsg()

   ; GUI Closed
   ;--------------------
   If $msg = $GUI_EVENT_CLOSE Then ExitLoop

   ; User Pressed SEND
   ;--------------------
   If $msg = $butt Then
      If $ConnectedSocket > -1 Then
         $ret = TCPSend( $ConnectedSocket, GUICtrlRead($input))
         If @ERROR Or $ret < 0 Then
            ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
            ;----------------------------------------------------------------
            TCPCloseSocket( $ConnectedSocket )
            WinSetTitle($GOOEY,"","my server - Client Disconnected")
            $ConnectedSocket = -1
         ElseIf $ret > 0 Then
            ; UPDATE EDIT CONTROL WITH DATA WE SENT
            ;----------------------------------------------------------------
            GUICtrlSetData($edit, GUICtrlRead($edit) & GUICtrlRead($input) & @CRLF )
         EndIf
      EndIf
      GUICtrlSetData($input,"")
   EndIf

   If $RogueSocket > 0 Then
      $recv = TCPRecv( $RogueSocket, 512 )
      If NOT @error Then
         TCPCloseSocket( $RogueSocket )
         $RogueSocket = -1
      EndIf
   EndIf

   ; If no connection look for one
   ;--------------------
   If $ConnectedSocket = -1 Then
      $ConnectedSocket = TCPAccept( $MainSocket)
      If $ConnectedSocket < 0 Then
         $ConnectedSocket = -1
      Else
         WinSetTitle($GOOEY,"","my server - Client Connected")
      EndIf

   ; If connected try to read some data
   ;--------------------
   Else
      ; EXECUTE AN UNCONDITIONAL ACCEPT IN CASE ANOTHER CLIENT TRIES TO CONNECT
      ;----------------------------------------------------------------
      $RogueSocket = TCPAccept( $MainSocket)
      If $RogueSocket > 0 Then 
          TCPSend( $RogueSocket, "~~rejected" )
      EndIf

      $recv = TCPRecv( $ConnectedSocket, 512 )

      If $recv <> "" And $recv <> "~~bye" Then
         ; UPDATE EDIT CONTROL WITH DATA WE RECEIVED
         ;----------------------------------------------------------------
         GUICtrlSetData($edit, GUICtrlRead($edit) & ">" & $recv & @CRLF)

      ElseIf @error Or $recv = "~~bye" Then
         ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1
         ;----------------------------------------------------------------
         WinSetTitle($GOOEY,"","my server - Client Disconnected")
         TCPCloseSocket( $ConnectedSocket )
         $ConnectedSocket = -1
      EndIf
   EndIf
WEnd

GUIDelete($GOOEY)

Func OnAutoItExit()
   ;ON SCRIPT EXIT close opened sockets and shutdown TCP service
   ;----------------------------------------------------------------------
   If $ConnectedSocket > -1 Then 
      TCPSend( $ConnectedSocket, "~~bye" )
      Sleep(2000)
      TCPRecv( $ConnectedSocket,  512 )
      TCPCloseSocket( $ConnectedSocket )
   EndIf
   TCPCloseSocket( $MainSocket )
   TCPShutDown()
EndFuncoÝ÷ Ø)bz{ZºÚ"µÍÐÓQS ÌÌÎÈÝYHYÝ[ÈHÑTTÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÉÌÌÎÂÈÙYHÔXÝ^[BÚ[ÛYH    ÑÕRPÛÛÝ[Ë]LÉÝÂÈÝHÔÙXÙÂÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÔÝ

BÈÙ]ÛÛYH]ØXH[ÂËKKKKKKKKKKKKKKKKKKKKKKKKKBÈÙ]   ÌÍÜÞTQTÔÈÈÚ]HÑTTËÙHÚ[Ú[ÙHHÈ[YH[È[TYÜÂ[H   ÌÍÜÞTQTÔÈH    ][ÝÔÔPÒQPÈVTSTÈÑTT][ÝÂ[H   ÌÍÛÔH
MÌÈ[]X[^HHXXHÈÙ[HÛÛXÝ[ÛÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOB[H   ÌÍÐÛÛXÝYÛØÚÙ]HLBÐ][ÈÛÛXÝÈÑTT]]ÈT[Ô
MÌÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÌÍÐÛÛXÝYÛØÚÙ]HÔÛÛXÝ
    ÌÍÜÞTQTÔË ÌÍÛÔ
B[H ÌÍÜÞ]BÈYHÈ[ÜÚÝÈ]YÜ[ÙÐÞ
LL  ][ÝÑÜ][ÝË  ][ÝÕÔÛÛXÝZ[YÚ]ÔÐHÜ    ][ÝÈ  [ÈÜBÈYHÈÈÜÛÜ[[]ÞÜ]BÈÈÙ[ÈHÑTT[ÙBÓÛÜÜ]ÚÚ[ÈÜ]HÈÙ[ÈHÑTTÚ[HBÈ[]ÞÜ]HÈ[ÛZ]   ÌÍÜÞ]HH[]Þ
    ][ÝÑ]HÜÙ][ÝË  [È [È ][ÝÑ[]HÈ[ÛZ]ÈHÑTT][ÝÊBÈY^HØ[Ù[H[]ÞÜX]H][ÈÙH^]ÝÜ]ÛÜYÜÜ  ÌÍÜÞ]HH ][ÝÉ][ÝÈ[^]ÛÜÈÙHÚÝ[]H]H[  ÌÍÜÞ]K]È][ÈÙ[]ÝYÚÝÛÛXÝYÛØÚÙ]ÔÙ[
    ÌÍÐÛÛXÝYÛØÚÙ] ÌÍÜÞ]JBÈYHÙ[Z[YÚ]Ü[HÛØÚÙ]ÈØÛÛXÝYËKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKBYÜ[^]ÛÜÑ[[Y

Share this post


Link to post
Share on other sites

Get tired of answering this one... it hits the forum every month or two...

Server has to listen on @ipaddress1, or the adapter that is in the pc...

If behind a router, the router needs to forward the port listened on of router IP to the IP mentioned above.

Client has to connect to that IP if connected direct to internet or router's IP if behind a router.

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

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  
Followers 0