问题
I have some code already done some code for the sender.bat and receiver.bat but the problem is, that when I send the message from the sender.bat it calls the receiver.bat and at which point I have two problems: 1)The sender.bat closes (and I want that to stay open so I can send further messages) and receiver.bat opens (which is what I want) but when it opens it brings with it the second problem. 2)It keeps repeating the same (the first) message
So I'll just give you the code and hope someone figures out what i have done wrong
Sender.bat:
@echo off
title "Message Sender"
set /p name=Username:
:x
set /p mes=Message:
set nmes=%name% Says: %mes%
call receiver.bat %nmes%
goto x
and the receiver.bat:
@echo off
:x
echo %1
echo %1>>conversationlogger.txt
pause
goto x
Thanks for any help
回答1:
The only way to "keep both files open" is executing they simultaneously, that is, via start
command or (as the "natural" way for this case), connected via a pipe.
sender.bat:
@echo off
title "Message Sender"
set /p name=Username: > CON
:x
set "mes="
set /p mes=Message: > CON
if not defined mes goto end
set nmes=%name% Says: %mes%
echo %nmes%
goto x
:end
echo Conversation ends > CON
receiver.bat:
@echo off
:x
set "msg="
set /P "msg="
if not defined msg exit /B
echo %msg%
echo %msg%>>conversationlogger.txt
goto x
Execute these files in this (obvious) way:
sender | receiver
EDIT: I added the output of an example session:
C:\> sender | receiver
Username: Antonio
Message: This is the first message
Antonio Says: This is the first message
Message: The second line
Antonio Says: The second line
Message: Last message...
Antonio Says: Last message...
Message:
Conversation ends
C:\> type conversationlogger.txt
Antonio Says: This is the first message
Antonio Says: The second line
Antonio Says: Last message...
C:\>
回答2:
Sender.bat
doesn't close. It waits untilreceiver.bat
finishes - which will not happen because of a endless loop.receiver.bat
doesn't get another message to display, therefore repeats the message (to be exact: the first word of it) in an endless loop.
remove goto x
from receiver.bat
to solve the endless loop. And change %1
to %*
to get the complete message, not the first word only.
EDIT now I understand you want to have two separate windows. Sadly, there is no built-in-way for communication between separate cmd windows, but it can be done "extern", for example via a text file.
Sender.bat
@echo off
title "Message Sender"
start "Receiver" receiver.bat
set /p name=Username:
title "Message Sender: %name%
echo "<<%name% joined>>">%name%sended.txt
>>"conversationlogger.txt" echo %date% %time%: echo "<<%name% joined>>"
:x
set "_message=<nothing>"
set /p mes=Message:
echo "%name% Says: %_message%">%name%_sended.txt
if /i "%_message%"=="quitme" (
title "Sender leaving"
echo "<<%name% left>>">%name%_sended.txt
for /f %%n in ('tasklist /v ^|find /c "Message Sender"') do (
if "%%n"=="0" echo "<<last member left>>">%name%_sended.txt
)
exit
)
goto :x
Receiver.bat
@echo off
echo checking for running receiver...
tasklist /v | find "Message Receiver" && exit
cls
title "Message Receiver"
:x
for %%a in (*_sended.txt) do (
call :say "%%~a" & del "%%a"
)
ping -n 1 -w 200 192.168.254.254 >nul
goto :x
:say
<%1 set /p "_message="
for /f "tokens=2 delims==" %%i in ('set _message') do (
echo %time:~0,8%: %%~i
>>"conversationlogger.txt" echo %date% %time%: %%~i
if "%%~i"=="<<last member left>>" ( pause & exit )
)
goto :eof
I have included a few features, that came to my mind. Keep, what you find useful, delete the rest. Feel free to ask, if you don't understand parts of the code or why I choosed that way to do it.
You can have more than one sender, but there will be only one receiver (per computer; I didn't test it, but it should be possible to chat over different PC's via network, when using a common network drive for *_sended.txt
)
Just start the sender, it starts the receiver itself (which checks, if another receiver is running and exits, if yes)
来源:https://stackoverflow.com/questions/35683551/im-trying-to-make-a-message-system-that-can-theoretically-send-messages-from-on