Jump to content

[Solved] Little help with TCP Server/Client


Recommended Posts

First of all, in your server, to recieve data, you use the connected socket identifier(as returned by TCPAccept) not the main socket identifier(returned by TCPListen).

Also in your client-script, because of that the script exits instantly after sending the bytes, causing the connected socket to be invalid(causing the server to miss the message as the socket gets disconnected).

Modified your code a little:

Server:

TCPStartUp()

$sListenAddress = @IPAddress1
$iListenPort = 8084

$iMainsocket = TCPListen($sListenAddress, $iListenPort)
If $iMainsocket = -1 Then Exit(MsgBox(0, "Error", "Could not create the listening socket"))

While 1
    $ConnectedSocket = TCPAccept($iMainsocket)
    If $ConnectedSocket >= 0 Then
        $info=TCPRecv($ConnectedSocket,512)
        msgbox(0,"Display message !",$info)
    EndIf
    Sleep(250)
Wend

Client:

TCPStartUp()

$g_IP = @IPAddress1
$Message = "Hello World"

$socket = TCPConnect( $g_IP, 8084 )
TCPSend($socket,$message)
Sleep(2500); Sleep some to keep the connection open, allowing the server to parse the message
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...