Boost: ASIO connection management 7

Source: Internet
Author: User

Newlisp provides a simple method for me to create multiple processes. The following program creates 10 processes, each of which sends several 'A' and finally sends a 'q '.

(define (quit-for-error)  ((println (net-error)) (exit)))(define (send-test)  (set 'socket (net-connect "localhost" 8888))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if socket (println (net-send socket "q")) (quit-for-error))  (exit))(spawn 'r1 (send-test))(spawn 'r2 (send-test))(spawn 'r3 (send-test))(spawn 'r4 (send-test))(spawn 'r5 (send-test))(spawn 'r6 (send-test))(spawn 'r7 (send-test))(spawn 'r8 (send-test))(spawn 'r9 (send-test))(spawn 'r10 (send-test))(until (sync 1000))(exit)

Spawn creates a sub-process to execute the send-test function.

A total of 10 processes have been created.

Until is a wait until all sub-processes exit.

Run the test program now:

 newlisp ./parallel_text.lspsend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfully1send a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1

The service program is shown as follows:

 ./cppapplication_4 the number of connections is: 1the new connection object is starting now.correct data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedthe number of connections is: 3the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.the number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connection

After careful check, 10 connections are created and 10 connections are closed. The program is normal. Okay. Now we want to change the server from a single thread to a multi-threaded model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.