Jump to content

In search for TCP send & receive example script


jwka
 Share

Recommended Posts

Hi,

I have scanned around but not so successful yet in serch of mentionned script.

I nned to establish a more complex "handshaked" communication:

- send "command"

- receive result (text, multiple bytes up to a couple of hundreds)

- send "ack"

- receive "ready"

and start over again.

Seems I need a listenner AND a sender. But I have no glue how to do it.

Can anyone help with a small example?

Thanks so much in advance!

jwka

Link to comment
Share on other sites

Client:

Global $Connected2
$IP = "5.59.4.36"
$Port =65432
$MyIP = @IPAddress1
;Start TCP services
TCPStartup()
;Connecting to main socket
$ConnectedSocket = TCPConnect($IP, $Port)
MsgBox(0,"",$ConnectedSocket)
If @error Then
MsgBox(4096, "Error ", "TCPConnect error: " & @error)
EndIf
;Send info
TCPSend($ConnectedSocket, $MyIP)
; Create a Listening "SOCKET"
$MainSocket2 = TCPListen($MyIP, $Port)
If @error Then
MsgBox(4096, "Error ", "TCPListen error: " & @error)
Exit
EndIf
While 1
$Connected2 = TCPAccept( $MainSocket2)
; look for client connection
$recv = TCPRecv($Connected2, 10000000)
If Not @error Then Endit()
WEnd
Func Endit()
MsgBox(0,"connected",$recv)
TCPCloseSocket($ConnectedSocket)
sleep(300)
TCPCloseSocket($Connected2)
TCPShutdown ( )
exit
EndFunc

Server:

global $connectedsocket
$ip = @IPAddress1
MsgBox(0,"",@IPAddress1)
$port = 65432
;to start tcp services
TCPStartup()
;create listening socket
$mainsocket = TCPListen($ip, $port)
If $mainsocket = -1 Then Exit
;Accept incoming clients and recieve info
While 1
$connectedsocket = TCPAccept($mainsocket)
If $ConnectedSocket >= 0 Then
$ip2 = TCPRecv($connectedsocket,1000000)
;create new socket to send connected message
$socket2 = TCPConnect($ip2, $port)
TCPSend($socket2, "connected")
EndIf
WEnd
TCPCloseSocket($connectedsocket)
TCPCloseSocket($socket2)
TCPShutdown()
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...