Sign in to follow this  
Followers 0
ghetek

TCP Volume Control

1 post in this topic

First of all i would like to give a big thanks to nfwu.

So i have a few couch surfers living at my place. i like these guys, very cool dudes but no matter what i do to the system volume they seem to still love to play their music really loud... i made this script to remotely control the volume level on the shared PC (the one hooked up to the big stereo system) from my room upstairs so that i don't have to walk all the way down and yell at them each time. please keep in mind that i got this working not 15 minutes ago and I'm still cleaning the script. i stole a good chunk of it right from the help file and nfwu helped with the looping issue i was having.

list of TODO's:

have the client automatically change a hostname to an ip address

have the client check the volume level string to ensure that it is a numeric value

find a way to have the server report back its current volume level

any other requests?

Server Portion

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.13.7 (beta)
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
#NoTrayIcon
Opt("TrayIconHide", 1)
Opt('MustDeclareVars', 1)
    Global $szIPADDRESS = @IPAddress1
    Global $nPORT = 33891
    Global $MainSocket, $ConnectedSocket, $szIP_Accepted
    Global $msg, $recv
Example()

Func Example()
   ; Set Some reusable info
   ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)

   ; Start The TCP Services
   ;==============================================
    TCPStartup()

   ; 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


   ; Initialize a variable to represent a connection
   ;==============================================
    $ConnectedSocket = -1
  ;;; nfwu: Start an infinite loop
   While 1

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


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

     ; GUI Message Loop
       While 1
          ; Try to receive (up to) 2048 bytes
           $recv = TCPRecv($ConnectedSocket, 2048) 
   
          ; If the receive failed with @error then the socket has disconnected
           If @error Then ExitLoop
   
          ; Update the Volume Level
           If $recv <> "" Then SoundSetWaveVolume ($recv)
       WEnd

      ;;;nfwu: Close the previous connection before opening a new one
       TCPCloseSocket($ConnectedSocket)

      ;;;nfwu: Mark $ConnectedSocket as being invalid, so the Do...Until loop way up there that waits for a connection actually waits for a connection =P
       $ConnectedSocket = -1

   WEnd;;;nfwu: Jump back to the start of this loop

   TCPShutdown()
EndFunc

; 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]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP

Client Portion

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.13.7 (beta)
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
   ; Set Some reusable info
   ;--------------------------
    Local $ConnectedSocket, $szData
    Local $szIPADDRESS = InputBox("Server IP Address","What is the IP of the Server?","192.168.1.120")
    Local $nPORT = 33891

   ; Start The TCP Services
   ;==============================================
    TCPStartup()

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

   ;Attempt to connect to SERVER at its IP and PORT 33891
   ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

   ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
       ; If there is no error loop an inputbox for data
       ;   to send to the SERVER.
    Else
       ;Loop forever asking for data to send to the SERVER
;       While 1
           ; InputBox for data to transmit
            $szData = InputBox("Volume Percent for Server","What volume Percentage would you like to set for the server?","50")

           ; If they cancel the InputBox or leave it blank we exit our forever loop
;           If @error Or $szData = "" Then ExitLoop

           ; We should have data in $szData... lets attempt to send it through our connected socket.
            TCPSend($ConnectedSocket, $szData)

           ; If the send failed with @error then the socket has disconnected
           ;----------------------------------------------------------------
;           If @error Then ExitLoop
;       WEnd
    EndIf
EndFunc  ;==>Example

once again i know its very messy and some of the funcs are still called "example" but it is in fact functional. please give me your input.

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  
Followers 0