Jump to content
Sign in to follow this  

TCP Receive Question

Recommended Posts


I am running a simple server <> client tcp program and I need it to update the receive every time the user that is connected hits enter. So every return, right now it updates after every keystroke

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;SERVER!! Start Me First !!!!!!!!!!!!!!!


Func Example()
  ; Set Some reusable info
  ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = ""
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv, $Help

    $Help = "EGM Web Monitor Transfer Tool Help Center\n"
    $Help &= "Line 1"

  ; Start The TCP Services

  ; Create a Listening "SOCKET".
  ;   Using your IP Address and Port 33891.
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

  ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit

  ; Create a GUI for messages
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)

  ; Initialize a variable to represent a connection
    $ConnectedSocket = -1

  ;Wait for and Accept a connection
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

  ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

  ; GUI Message Loop
    While 1
        $msg = GUIGetMsg()

      ; GUI Closed
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

      ; Try to receive (up to) 2048 bytes
        $recv = TCPRecv($ConnectedSocket, 2048)
        If $recv = "h"&@CRLF&"e"&@CRLF&"l"&@CRLF&"p" Then
            TCPSend($ConnectedSocket, $Help)
      ; If the receive failed with @error then the socket has disconnected
        If @error Then ExitLoop

      ; Update the edit control with what we have received
        If $recv <> "" Then GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))

    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

EndFunc ;==>Example

; Function to return IP Address from a connected socket.
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
        $aRet = 0

    $sockaddr = 0

    Return $aRet
EndFunc ;==>SocketToIP

It's an example script that I just tweaked a little

Edited by Fearsoldier

Share this post

Link to post
Share on other sites

Isn't it updating after every stroke because you told it to?

$recv <> ""

Maybe use something like:

StringInStr($recv, Chr(13)) > 0 ; Chr(13) (enter) is somewhere in $recv then the outcome > 0

Seems you know the better of copying and pasting muttley

My active project(s): A-maze-ing generator (generates a maze)

My archived project(s): Pong3 (Multi-pinger)

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.