Jump to content

allow multiple incomings


Recommended Posts

#include-once

; ----------------------------------------------------------------------------

; AutoIt Version: 3.1.1.92

; Author: Max Gardner <AutoIt Smith;king.of.all@comcast.net>

; ----------------------------------------------------------------------------

; ----------------------------------------------------------------------------

; Function: TCPStartServer($Port[, $MaxConnection])

; $Port = Port To Start Server On

; $MaxConnection = (Optional) Maximum Connections Allowed; Default 1

;

; Returns:

; Success : Returns Socket and @Error = 0

; Failure : Returns 0 or 1 and sets @Error to Windows API WSAGetLasterror

; 0 = TCP Services Not Avialible On That Port

; -1 = TCP Services Would Not StartUp

; ----------------------------------------------------------------------------

Func TCPStartServer($Port, $MaxConnect = 1)

Local $Socket

$Socket = TCPStartup()

Select

Case $Socket = 0

SetError(@Error)

Return -1

EndSelect

$Socket = TCPListen(@IpAddress1, $Port, $MaxConnect)

Select

Case $Socket = -1

SetError(@Error)

Return 0

EndSelect

SetError(0)

Return $Socket

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPAllowConnection($Socket)

; $Socket = The Main Socket As Returned By TCPStartServer

;

; Returns:

; Success : Returns ConnectedSocket and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; ----------------------------------------------------------------------------

Func TCPAllowConnection($Socket)

Local $ConnectedSocket

$ConnectedSocket = TCPAccept($Socket)

Select

Case $ConnectedSocket >= 0

SetError(0)

Return $ConnectedSocket

Case Else

SetError(@Error)

Return -1

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPReceive($ConnectedSocket, $MaxChar)

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

;

; Returns:

; Success : Returns Recieved String and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; ----------------------------------------------------------------------------

Func TCPReceive($ConnectedSocket, $MaxChar = 512)

Local $Data

$Data = TCPRecv($ConnectedSocket, $MaxChar)

Select

Case $Data = ""

SetError(@Error)

Return -1

Case Else

SetError(0)

Return $Data

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPSendMessage($ConnectedSocket, $String)

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

; $String = Data to be sent

;

; Returns:

; Success : Returns 1 and @Error = 0

; Failure : Returns -1 or 0 and sets @Error to Windows API WSAGetLasterror

;

; ----------------------------------------------------------------------------

Func TCPSendMessage($ConnectedSocket, $String)

Local $Sent

$Sent = TCPSend($ConnectedSocket, $String)

Select

Case $Sent = 0

SetError(@Error)

Return -1

Case Else

SetError(0)

Return 1

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPStopServer($MainSocket, $ConnectedSocket)

; $MainSocket = The Socket As Returned By TCPStartServer

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

;

; Returns:

; Success : Returns 1 and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; 0 = Unable To Shutdown TCP Services

; -1 = Unable To Close Socket/s

; ----------------------------------------------------------------------------

Func TCPStopServer($MainSocket, $ConnectedSocket)

Local $Shutdown

Select

Case $ConnectedSocket <> -1

$ConnectedSocket = TCPCloseSocket($ConnectedSocket)

Select

Case $ConnectedSocket = 0

SetError(@Error)

Return -1

EndSelect

EndSelect

Select

Case $MainSocket <> -1

$MainSocket = TCPCloseSocket($MainSocket)

Select

Case $MainSocket = 0

SetError(@Error)

Return -1

EndSelect

EndSelect

$Shutdown = TCPShutdown()

Select

Case $Shutdown = 0

SetError(@Error)

Return 0

EndSelect

SetError(0)

Return 1

EndFunc

Link to comment
Share on other sites

#include <Misc.au3>

#include 'TCP Funcs.au3'

Local $ConnectedSocket = -1

$MainSocket = TCPStartServer(7000)

While _IsPressed('1B') = 0

If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket)

$Data = TCPReceive($ConnectedSocket)

If @Error = 0 Then ; events to do with incomings

EndIf

Sleep(100)

WEnd

TCPStopServer($MainSocket, $ConnectedSocket)

Link to comment
Share on other sites

#include 'TCP Funcs.au3'

TCPStartup()

$Socket = TCPConnect("10.0.1.100", 7000)

TCPSendMessage($Socket, "This is NOT a test!")

TCPCloseSocket($Socket)

TCPShutdown()

this one is the client side au3, i run it to send a string to the server, it's the 2nd code post.

they both are based on the first one, tcp_funcs.au3

however, the server seems to be able to to only accept and process one incoming connections and does not terminate or process new incomings afterward.

I need it to process one incomining after the other, how do I fix the code?

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