How to communicate from one computer to another in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RSP
Posts: 5
Joined: 11 Nov 2021 11:39

How to communicate from one computer to another in Batch

#1 Post by RSP » 11 Nov 2021 11:50

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
)

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#2 Post by Joe Caverly » 12 Nov 2021 06:45

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

RSP
Posts: 5
Joined: 11 Nov 2021 11:39

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

#3 Post by RSP » 12 Nov 2021 13:35

this might be helpful but how to send the signal from one computer to other computers?

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#4 Post by Joe Caverly » 12 Nov 2021 19:35

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

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#5 Post by Joe Caverly » 12 Nov 2021 20:05

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

RSP
Posts: 5
Joined: 11 Nov 2021 11:39

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

#6 Post by RSP » 13 Nov 2021 12:03

ok but how can I know the name of the system of the receiver and the sender? Is that the Device Name?

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#7 Post by Joe Caverly » 13 Nov 2021 19:41

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

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#8 Post by Joe Caverly » 14 Nov 2021 08:58

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

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#9 Post by Joe Caverly » 15 Nov 2021 07:49

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/

RSP
Posts: 5
Joined: 11 Nov 2021 11:39

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

#10 Post by RSP » 15 Nov 2021 15:26

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

Joe Caverly
Posts: 11
Joined: 11 Jul 2018 05:05

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

#11 Post by Joe Caverly » 15 Nov 2021 18:18

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

RSP
Posts: 5
Joined: 11 Nov 2021 11:39

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

#12 Post by RSP » 15 Nov 2021 21:39

It now works! Thanks for the help! :D

Post Reply