Jump to content

TCP server problem, TCPRECV problem very basic


Recommended Posts

Hi,

my script doesnt work and i cant figure out how to fix the problem. a client program is sending a STRING via port 6969 through the established socket.

client ----> my server.

what happens is, i tried $x=TCPRecv(mysocket) but $x is always blank, and i tired various things but i can never get the $x... to contain the data sent by the client.

so here it again what i want to do:

send a string from the client to the server.. i know the string is sent from the client correctly as other programs can easily get what the client is sending. however my script returns 1-1-1-1-1-1-1-1-1-1 :S.

thanks

#include <File.au3>

Global $MainSocket
Local $MaxLength = 600; Maximum Length Of Text
Local $Port = 6969; Port Number
Local $Server = @IPAddress1; Server IpAddress
Dim $accept = -1
Dim $Data = -1

TCPStartup()

$MainSocket = TCPlisten($Server, $Port)

If $MainSocket = -1 Then
    MsgBox(16, "Error", "Unable to connect.")
    Exit
EndIf

While 1
    
    $accept = TCPAccept($MainSocket)
    If $accept >= 0 Then
        $Data = TCPRecv($MainSocket, $MaxLength)
        MsgBox(0,"","server and client connected",1)
        ExitLoop
    EndIf
    FileWrite("somebs.txt", $Data)
WEnd

Func OnAutoItExit()
    TCPCloseSocket($MainSocket)
    TCPShutdown()
    
EndFunc  ;==>OnAutoItExit
Link to comment
Share on other sites

I fixed it my self with the code below but though i dont understand why my other code didnt work, this below code was created via trail and error.

#include <File.au3>

Global $MainSocket
Local $MaxLength = 600; Maximum Length Of Text
Local $Port = 6969; Port Number
Local $Server = @IPAddress1; Server IpAddress
Dim $recivied =""
Dim $testa = ""
TCPStartup()

$MainSocket = TCPlisten($Server, $Port)

If $MainSocket = -1 Then
    MsgBox(16, "Error", "Unable to connect.")
    Exit
EndIf

While 1
    $testa = TCPAccept($MainSocket)
If $testa >= 0  Then
        $recivied = TCPRecv($testa, $MaxLength) 
    EndIf
    
    FileWrite("somebs.txt",$recivied)
WEnd

Func OnAutoItExit()
    TCPCloseSocket($MainSocket)
    TCPShutdown()
    
EndFunc  ;==>OnAutoItExit
Link to comment
Share on other sites

You should use TCPRecv() in a While...WEnd loop just in case the server/client doesn't respond quick enough. Use it like this:

While 1
    $testa = TCPAccept($MainSocket)
    If $testa >= 0 Then
        $recieved = ""
        While $recieved = ""
            $recivied = TCPRecv($testa, $MaxLength)
        WEnd
        FileWrite("somebs.txt", $recivied)
    EndIf
WEnd
I hope I helped! :P
Link to comment
Share on other sites

yeah i understand that, the problem was with autoit helpfile it says TCPRecv gets its socket id from tcp listen, which is incorrect it should get it from TCP accept.

mainsocket The main socket identifier (SocketID) as returned by a TCPListen function.

ill report this as a bug

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