⬆️ ⬇️

Multiuser chat on BAT

There was a period when the university was asked to make several chats. Depending on the options, various ways of program interaction from sockets to mailslot were set. When the tasks were completed, the strength remained to take on the BAT scripts. That's what happened ...







All you need to run:

1. Take the code by reference at the end of the article.

2. Save code to * .bat file

3. Put the file in a network folder

4. Run the file from different computers (you can also from one)

')

In the comments suggested to be used in conjunction with DropBox, but they say that it does not work very quickly.



First of all, I want to apologize for the code, decorated with screenshots. Nobody will just highlight BAT-nick better than Notepad ++ . Snapshots of the code are saved in palette (256 colors) PNG - the weight of the code screen is 5-10 Kb.



First about the implementation



Basic elements:

1.% chat% - group name

2.% nick% - username

3. file "% chat% _history" - file with full history of the group

4. file "% chat%" - the file with the last replica in the group

5. file "% chat% _cs" - file used for synchronization



To begin, ask the user to enter the name of the group and your nickname. After receiving the group name and nickname in line No. 5, the script launches itself with three parameters: the word talk_widget, as well as the group name and user name.







Now we display in the window title the name of the group and the name of the user (line No. 3). If there is a file named% chat% _history, print its contents on the screen (line number 4). Next in the loop, we read the contents of the% chat% file and compare it with what was in the previous reading. If the contents of the file has changed - we print it on the screen.







Initially, the code has the following structure: at startup, the parameters are checked, if the first parameter is the word “talk_widget”, the script executes the code related to sending text. In the absence of such a parameter, the code performs the function of receiving and displaying data. Initially, the script is run without parameters, it asks the name of the group and the name of the user, starts the second process with the “talk_widget” parameter, and starts typing the contents of the chat group.







Now how the code accepts input from the user. He first modifies the window title (line number 5). Then it prints a connection notification message to the last replica file and appends the message to the history. When accessing a replica file, a primitive critical section mechanism is used (below we will look at how it works). Further, in the cycle (lines 11-19), data lines are received from the user and written to the file and replicated to the history file.







The function of entering the critical section of the file used for synchronization, cyclically records a random number. Until it is there when re-reading. If the file exists at the time the function is launched, there is also a slight delay. Delays are realized through ping>nul .







The exit function from the critical section simply deletes the synchronization file.







Now, about the result



There is a chat with the following qualities:

1. The division into chat groups (many groups and many users)

2. Getting the communication history of new participants

3. Communication on the network (put the script in a shared folder and run from different computers)



At startup, the script asks the name of the chat group and the user name.







After entering the required data, a second window appears, which serves as a console for input:







Now I will launch the same BAT file (it is in a shared folder) from another computer on the network.







After entering the same chat name and the name of the new user, the second window will again appear to enter the data to be sent.







Now we will try to send messages between subscribers:







Now connect from the third computer:







And we will see the following picture:







Attention! An incomprehensible bug in win7 x64 is noticed when launching the script from the network folder // ***. The third line of the pushd script "% ~ dp0" is interpreted for a very long time. % ~ Dp0 understands a very long time, just try to call echo% ~ dp0 - this is at least strange. If you wait 2-3 minutes, then everything will work fine ... in WinXP there are no such problems.



Warning: each time the script is launched from the network folder in My Computer, network drives with the letters Z: Y: X: etc. will spawn. After rebooting, they will disappear without a trace.



And of course the link to the code: http://codepad.org/rvFN13LI



As well as useful links:

1. Work with paths in BAT: http: //www.microsoft.com/ .....

2. And generally speaking about writing BAT-nicks: http: //www.microsoft.com/ .....



UPDATE:

If you happened to use this chat for its intended purpose, he-khe)) Please leave a comment about the sensations you received.

Source: https://habr.com/ru/post/121903/



All Articles