Page 1 of 1

How to communicate from one computer to another in Batch

Posted: 11 Nov 2021 11:50
by RSP
So I was planning it to be like this:

Sender will send a message
Receiver will receive that message

But I don't know how tho. Can someone pls help me :(

Here's the code for now

Sender:

Code: Select all

@echo off
echo Put message in here to send in the receivers.
set /p msg=">"
(send message code that I don't know how)
Receiver:

Code: Select all

@echo off
:foo
echo Wait for resposes.
pause
if (there is message) (
echo %message%
pause
cls
goto foo
) else (
echo No responses yet!
pause
cls
goto foo
)

Re: How to communicate from one computer to another in Batch

Posted: 12 Nov 2021 06:45
by Joe Caverly
Take a look at the Waitfor command.

Do a search of Waitfor on this forum. search.php?keywords=waitfor

Ref: https://ss64.com/nt/waitfor.html
Microsoft: https://docs.microsoft.com/en-us/previo ... )#examples

Joe

Re: How to communicate from one computer to another in Batch

Posted: 12 Nov 2021 13:35
by RSP
this might be helpful but how to send the signal from one computer to other computers?

Re: How to communicate from one computer to another in Batch

Posted: 12 Nov 2021 19:35
by Joe Caverly
If you read the information contained in the links that I posted, your question will be answered.

Examples are given which show how to send the signal from one computer to another computer.

Joe

Re: How to communicate from one computer to another in Batch

Posted: 12 Nov 2021 20:05
by Joe Caverly
From Windows 10, launch two cmd.exe windows.

From the first cmd.exe window, type;

WaitFor Complete

In the second cmd.exe window, type;

WaitFor /SI Complete

You have now sent a signal from one cmd.exe window to a second cmd.exe window using WaitFor

Code: Select all

e:\utils>waitfor /?

WaitFor has two ways of working: 

Syntax 1: to send a signal
    WAITFOR [/S system [/U user [/P [password]]]] /SI signal

Syntax 2: to wait for a signal
    WAITFOR [/T timeout] signal 

Description:
    This tool sends, or waits for, a signal on a system. When /S is not
    specified, the signal will be broadcasted to all the systems in a
    domain. If /S is specified, then the signal will be sent only
    to the specified system.

Parameter List:
    /S     system         Specifies remote system to send signal to.

    /U     [domain\]user  Specifies the user context under which
                          the command should execute.

    /P     [password]     Specifies the password for the given user context.

    /SI                   Sends the signal across the net to waiting machines

    /T     timeout        Number of seconds to wait for signal. Valid range
                          is 1 - 99999. Default is to wait forever for signal.

    signal                The name of the signal to wait for or to send.

    /?                    Displays this help message.

    NOTE: A system can wait for multiple unique signal names.
    The signal name cannot exceed 225 characters and cannot
    contain characters other than a-z, A-Z, 0-9 and ASCII 
    characters in the range 128-255.

Examples:
    WAITFOR /?
    WAITFOR SetupReady 
    WAITFOR CopyDone /T 100 
    WAITFOR /SI SetupReady 
    WAITFOR /S system  /U user /P password /SI CopyDone
Looking at the help for WaitFor, you can send a signal to another system;

Code: Select all

waitfor /s OtherSystem /SI Test
...where you replace the "OtherSystem" with the name of the system you want to send a signal to.

Joe

Re: How to communicate from one computer to another in Batch

Posted: 13 Nov 2021 12:03
by RSP
ok but how can I know the name of the system of the receiver and the sender? Is that the Device Name?

Re: How to communicate from one computer to another in Batch

Posted: 13 Nov 2021 19:41
by Joe Caverly
Open up a cmd.exe window, and type echo %computername%.

Here's what it displays on my Windows 10 system;

Code: Select all

e:\utils>echo %computername%
DESKTOP-H2JFFTF
This says that the name of my system is DESKTOP-H2JFFTF

Visit the SS64 site;
https://ss64.com/nt/waitfor.html

Scroll down to the bottom of that page.

You will see examples on how to use WAITFOR to send a signal from one system to another.

Joe

Re: How to communicate from one computer to another in Batch

Posted: 14 Nov 2021 08:58
by Joe Caverly
From a cmd.exe window;

Code: Select all

e:\utils>server.cmd
Enter a message on the next line.
>This is a test
server.cmd just created the file r:\temp\Message.txt

Waiting for client.cmd to signal that they have read the file.
From a second cmd.exe window;

Code: Select all

e:\utils>client.cmd
Display the contents of r:\temp\message.txt

This is a test

Sending a signal to server.cmd that client.cmd has read the file.

SUCCESS: Signal sent.
I then switch back to the cmd.exe window from which I ran server.cmd;

Code: Select all

e:\utils>server.cmd
Enter a message on the next line.
>This is a test
server.cmd just created the file r:\temp\Message.txt

Waiting for client.cmd to signal that they have read the file.

SUCCESS: Signal received.

Signal received from client.cmd
Deleting R:\temp\Message.txt
     1 file deleted
Server.cmd

Code: Select all

@setlocal
@echo off
echo Enter a message on the next line.
set /p msg=">"
echo %msg% > %temp%\Message.txt
echo %0 just created the file %temp%\Message.txt
echo.
echo Waiting for client.cmd to signal that they have read the file.
waitfor.exe JobDone
echo.
echo Signal received from client.cmd
del %temp%\Message.txt
endlocal
Client.cmd

Code: Select all

@setlocal
@echo off
echo Display the contents of %temp%\message.txt
echo.
type %temp%\message.txt
echo.
echo Sending a signal to server.cmd that client.cmd has read the file.
waitfor /S %computername% /SI JobDone
endlocal
This will work even if you start a 32-bit cmd.exe for server.cmd, and a 64-bit cmd.exe for client.cmd, or vice-versa.

You could even start the server.cmd in a cmd.exe window, and run the client.cmd in a PowerShell window.

But I digress.

Joe

Re: How to communicate from one computer to another in Batch

Posted: 15 Nov 2021 07:49
by Joe Caverly
I found a post I made a while back on how I use WAITFOR.EXE from TCC (Take Command Console from JPSoft.com) and VBScript.

This could also be made to work with cmd.exe, instead of tcc.exe

Joe

Ref: https://jpsoft.com/forums/threads/waitf ... nge.10403/

Re: How to communicate from one computer to another in Batch

Posted: 15 Nov 2021 15:26
by RSP
So it now works.
However, as I tested it, it doesn't work when you just open the batch file. You need it to run on cmd.exe directly
For example I want to run client.cmd using the call command in another batch script. But when it opens, It crashes.
Even going to explorer and double-clicking (opening) it crashes both client and server.

Code for launcher.cmd:

Code: Select all

@echo off
:prompt
echo Deploy as... [Receiver, Sender]
set /p resen=">"
if %resen%==Receiver call folder\Client.cmd
if %resen%==Sender call folder\Server.cmd
echo Invalid Option
pause
goto prompt
Code for Server.cmd:

Code: Select all

@setlocal
@echo off
title Server
:loop
cls
echo Enter a message on the next line.
set /p msg=">"
echo %msg% > %temp%\Message.txt
echo %0 just created the file %temp%\Message.txt
echo.
echo Waiting for client.cmd to signal that they have read the file.
waitfor.exe JobDone
echo.
echo Signal received from client.cmd
del %temp%\Message.txt
goto loop
endlocal
Code for Client.cmd:

Code: Select all

@setlocal
@echo off
title Client
cls
echo Display the contents of %temp%\message.txt
echo.
type %temp%\message.txt
echo.
echo Sending a signal to server.cmd that client.cmd has read the file.
waitfor /S %computername% /SI JobDone
pause
goto loop
endlocal
If I replaced call to start in launcher.cmd it just opens a cmd window
However launching client.cmd and server.cmd directly with command prompt it works fine

Re: How to communicate from one computer to another in Batch

Posted: 15 Nov 2021 18:18
by Joe Caverly
I ran your code as-is, and it worked on my Windows 10 system.

Try the following;

Code: Select all

@echo off
:prompt
echo Deploy as... [Receiver, Sender, eoj]
set /p resen=">"
if %resen%==Receiver cmd.exe /c e:\utils\Client.cmd
if %resen%==Sender cmd.exe /c e:\utils\Server.cmd
if %resen%==eoj exit /b
if not %resen%==Receiver (
  if not %resen%==Sender (
	  echo Invalid Option
	)
)
pause
goto prompt
Note that instead of using call, I'm running a new cmd.exe with the /c option, which starts a new cmd.exe session, runs the .cmd file, and then terminates.

I've also added the option to enter;

Code: Select all

eoj
... to terminate the .cmd file.

Again, I ran your code as-is, and it worked on my Windows 10 system, so see if my modifications to your code work on your system.

Joe

Re: How to communicate from one computer to another in Batch

Posted: 15 Nov 2021 21:39
by RSP
It now works! Thanks for the help! :D