Jump to content

TCP Server accepting multiple clients.


Go to solution Solved by Wildbook,

Recommended Posts

Hi, I'm trying to make a Server/Client tool where the server sends a string of text or a file, and the client responds with a string of text or a file depending on the data it received from the server.

I have tried pretty much every UDF and every example I can find here, but the only one that I've found that works for this is >this, and it randomly cuts off the string received/sent.

What i want is a server showing a list of connected clients, and when I select a client, I'm able to send a string (a command) or file to it. The client should then be able to both read the string and answer with a string or file or write the received file to a file depending on the command it received from the server. 

Please, I need this as soon as possible and I have yet to find something who does this...  :ermm:

Link to comment
Share on other sites

My guess is that your string is too long, you'll need to split that data.

Here's an example: (This is modified from a WebServer.au3 script, I'm not the author)

$bData = Binary("Bleh!") ;Insert long string here
While BinaryLen($bData) ; Send data in chunks (most code by Larry)
    $a = _TCP_Send($hSocket, $bData) ; TCPSend returns the number of bytes sent
    If @Error Then ExitLoop
    $bData = BinaryMid($bData, $a+1, BinaryLen($bData)-$a)
WEnd

On the receiving end, you need to check that you received everything, so maybe add @CRLF & @CRLF at the end of your string and check for it to make sure the string is completed.

Note that the _TCP_Server_Broadcast function won't work with long strings, you'll have to make your own loop through the sockets and use code like above for each.

Link to comment
Share on other sites

My guess is that your string is too long, you'll need to split that data.

Here's an example: (This is modified from a WebServer.au3 script, I'm not the author)

$bData = Binary("Bleh!") ;Insert long string here
While BinaryLen($bData) ; Send data in chunks (most code by Larry)
    $a = _TCP_Send($hSocket, $bData) ; TCPSend returns the number of bytes sent
    If @Error Then ExitLoop
    $bData = BinaryMid($bData, $a+1, BinaryLen($bData)-$a)
WEnd

On the receiving end, you need to check that you received everything, so maybe add @CRLF & @CRLF at the end of your string and check for it to make sure the string is completed.

Note that the _TCP_Server_Broadcast function won't work with long strings, you'll have to make your own loop through the sockets and use code like above for each.

Thanks, I got that part now :D, but how do I look for the ending if TCPRecv returns binary? :huh:

Link to comment
Share on other sites

  • Solution

Hi Wildbook

have a look to >this nice example/tutorial, it can give you some directions

>here an attempt I made time ago

Thanks, this got me in the right direction and I think I know how to do this now! :D

I'll be back here if I run into more problems.

If someone gets here with the same problem as me:

 
I changed
TCPRecv($hSocket, 2048)
to
$Recv = "" ;Declare and/or clean variable for reading buffer.
$sData = "" ;Declare and/or clean variable for storing received data.

Do ;Try to get more data from the "received" buffer.
    $sRecv = TCPRecv($hSocket, 2048) ;Try to get 2048 bytes from the "received" buffer.
    $sData = $sData & $sRecv ;Add the data received from the "reading buffer" variable to the "storing" variable.
Until $sRecv = "" ;Until the "received" buffer is empty.

ConsoleWrite($sData) ;Write the received data to console (for debug purpose, so you know what you received)

Thanks again, both z3r0c00l12 for pointing me in the right direction (Loop reading from TCPRecv until the end of it) and PincoPanco for showing me a coded example of how this would work, and showing me about buffers.

:D

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