Jump to content
Sign in to follow this  
ghetek

TCP Volume Control

Recommended Posts

ghetek

Hey guys, im looking for a way to make a server that handles TCP, the server will wait for a client to connect and then take in a percentage number between 1-100 and then it will change the wave volume to the number that it recieved from a client computer on the network. After the volume has been changed, the client should quit out and the server should script should pop back to the top and wait for another connection. so far ive cannibalized the examples and here is what i have.

Here is the 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>

Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================
    Global $szIPADDRESS = @IPAddress1
    Global $nPORT = 33891
    Global $MainSocket, $ConnectedSocket, $szIP_Accepted
    Global $msg, $recv, $me
$me=1
Do
Example()
Until $me=2
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


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

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP

here is the client side

#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

Share this post


Link to post
Share on other sites
nfwu

You want the server to loop.

First ask yourself, what do you need to loop?

Since you want the server to do processing then wait for another connection, you need to loop back to the part where the server accepts a connection.

; 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()

Read my comments. Replacing the appropriate section of code in the server with this should do the trick. Feel free to ask more questions. =P

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.