Jump to content
Sign in to follow this  
GuessMyName

TCP Client and Server

Recommended Posts

GuessMyName

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

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
GuessMyName

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

Share this post


Link to post
Share on other sites
water

Glad you could solve your problem :)

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

×