Jump to content

TCP Client and Server


Recommended Posts

Hello

I have been training in TCP connections and with the help of some tutorials i have made a Client and a Server here are the Scripts:

Client:

Tcpstartup()

$Socket = TCPConnect(@IPAddress1, 403)

If $Socket = -1 Then
MsgBox(0, "Error", 'Could not conncet to the server')
EndIf

;$SendBytes = TCPSend($Socket, 'Hello, client has made connection with the server')
While 1
$Order = InputBox('Send Order', 'Type your order here:')
$SendBytes = TCPSend($Socket, $Order)

If $SendBytes = 0 Then
MsgBox(0, 'Error', 'The packet was not able to reach the server')
EndIf
WEnd

TCPCloseSocket($Socket)
TCPShutdown()

Server:

TCPStartup()

$MainSocket = TCPListen(@IPAddress1, 403)

While 1
$AcceptedSocket = TCPAccept($MainSocket)

If $AcceptedSocket <> -1 Then
$RecievedData = TCPRecv($AcceptedSocket, 1000000)
If $RecievedData <> "" Then
MsgBox(0, 'Data Recived', $RecievedData)
TCPCloseSocket($AcceptedSocket)
EndIf
EndIf
WEnd

I don't know why but the Client does not work i mean this does not work:

$Order = InputBox('Send Order', 'Type your order here:')
$SendBytes = TCPSend($Socket, $Order)

However if i delete this:

While 1
$Order = InputBox('Send Order', 'Type your order here:')
$SendBytes = TCPSend($Socket, $Order)

If $SendBytes = 0 Then
MsgBox(0, 'Error', 'The packet was not able to reach the server')
EndIf
WEnd

And insert this instead (Without comment mark of course), it works (sends the text):

;$SendBytes = TCPSend($Socket, 'Hello, client has made connection with the server')

Anyone can help me?

Thanks

Link to comment
Share on other sites

Have a look at the examples in the help file for TCPSend and TCPReceive. The examples show how it should be done.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ha I corrected my script now it works.

Client:

HotKeySet('E', '_Exit')

TCPStartup()

$TCPConncet = TCPConnect(@IPAddress1, 403)

If $TCPConncet = - 1 Then Exit

;TCPSend($TCPConncet, 'Close')

While 1
$Order = InputBox('Send Order', 'Type your order here:')
TCPSend($TCPConncet, $Order)
WEnd

Func _Exit()
Exit
EndFunc

Server:

HotKeySet('~', '_Exit')
TCPStartup()

$TCPListen = TCPListen(@IPAddress1, 403)

Do
$TCPAccept = TCPAccept($TCPListen)
Until $TCPAccept <> -1

While 1
Do
$TCPRecive = TCPRecv($TCPAccept, 1000000)
Until $TCPRecive <> ""
MsgBox(0, 'Data Recived', $TCPRecive)
WEnd

Func _Exit()
Exit
EndFunc
Link to comment
Share on other sites

Glad you could solve your problem :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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