AutoCAD.net supports background thread-Socket communication and autocad.net-socket

Source: Internet
Author: User

AutoCAD.net supports background thread-Socket communication and autocad.net-socket
Recently, due to the needs of the company's projects, CAD serves as the server to run in the server, waiting for the client to remotely send the command to execute the task, and finally confirms the use of Socket-tcp communication, CAD needs to monitor the messages sent by the client in real time. In this case, it needs to enable the thread to execute the Socket Listening Task. This problem occurs. CADAPI does not support multithreading. You can only call API functions from the main thread. If you are in a different thread, you must concentrate the calls on the main thread. Finally, I checked the data for half a day and found that the simplest implementation method is based on the main thread system. windows. forms. control the object and call its startup function to final the invoke () function. The demo of the client and the server is as follows:

1 class Program 2 {3 private static byte [] result = new byte [1024]; 4 static void Main (string [] args) 5 {6 // set the Server ip address 7 IPAddress ip = IPAddress. parse ("10.19.10.237"); 8 // IPAddress ip = IPAddress. parse ("127.0.0.1"); 9 Socket clientSocket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); 10 try11 {12 clientSocket. connect (new IPEndPoint (ip, 8885); // Configure the server IP address and port 13 Conso Le. WriteLine ("successfully connected to the server"); 14} 15 catch16 {17 Console. WriteLine ("failed to connect to the server. Press enter to exit! "); 18 return; 19} 20 // receive data through clientSocket 21 int accept elength = clientSocket. receive (result); 22 Console. writeLine ("receive Server Message: {0}", Encoding. UTF8.GetString (result, 0, cancelength); 23 // send data through clientSocket 24 for (int I = 0; I <13; I ++) 25 {26 try27 {28 Thread. sleep (1000); // wait for 1 second 29 string sendMessage = "C: \ Users \ Administrator \ Desktop \ original CAD file \ 14412-kujiale.dxf"; 30 clientSocket. send (Encoding. UTF8.GetBytes (sendMessage); 31 Console. writeLine ("send message to server: {0}" + sendMessage); 32 bytes elength = clientSocket. receive (result); 33 Console. writeLine ("receive Server Message: {0}", Encoding. UTF8.GetString (result, 0, cancelength); 34} 35 catch36 {37 clientSocket. shutdown (SocketShutdown. both); 38 clientSocket. close (); 39 break; 40} 41} 42 clientSocket. shutdown (SocketShutdown. both); 43 clientSocket. close (); 44 Console. writeLine ("sent, press enter to exit"); 45 Console. readLine (); 46} 47}
Socket Client-console Test

 

1 public class SocketServer 2 {3 // use the winform Control for background processing. CADAPI does not support multithreading 4 static Control syncCtrl; 5 // host the received data 6 private static byte [] result = new byte [1024]; 7 // port 8 private static int myProt = 8885; 9 10 private static Socket serverSocket; 11 12 delegate void FinishedProcessingDelegate (); 13 public static void InitSocket () 14 {15 // create Control 16 syncCtrl = new Control (); 17 syncCtrl. createControl (); 18 // create communication object 19 serverSocket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); 20 // bind the IP Address: port 21 serverSocket. bind (new IPEndPoint (IPAddress. any, myProt); 22 // set up to 10 queuing connection requests 23 serverSocket. listen (10); 24 // send data through Clientsoket 25 Thread myThread = new Thread (ListenClientConnect); 26 myThread. start (); 27} 28 /// <summary> 29 // listen to client connection 30 /// </summary> 31 private static void ListenClientConnect () 32 {33 while (true) 34 {35 Socket clientSocket = serverSocket. accept (); 36 try 37 {38 39 clientSocket. send (Encoding. UTF8.GetBytes ("I am the Lord of the Ox"); 40 Thread receiveThread = new Thread (ReceiveMessage); 41 receiveThread. start (clientSocket); 42} 43 catch (Exception) 44 {45 46 clientSocket. shutdown (SocketShutdown. both); 47 clientSocket. close (); 48} 49 50} 51} 52 53 // <summary> 54 // receive message 55 /// </summary> 56 /// <param name = "clientSocket"> </param> 57 private static void ReceiveMessage (object clientSocket) 58 {59 Socket myClientSocket = (Socket) clientSocket; 60 while (true) 61 {62 try 63 {64 // use clientSocket to receive data 65 int receiveNumber = myClientSocket. receive (result); 66 if (receiveNumber> 0) 67 {68 // get the object name 69 string filePath = Encoding. UTF8.GetString (result, 0, receiveNumber); 70 // determine whether the object exists 71 if (FileOperate. fileExists (filePath) 72 {73 FileStandardization. filePathByJava = filePath; 74 if (syncCtrl. invokeRequired) 75 {76 // start the task of performing CAD data processing 77 syncCtrl. invoke (new FinishedProcessingDelegate (FileStandardization. transMianByJava); 78} 79 myClientSocket. send (Encoding. UTF8.GetBytes ("I am the devil of the ox: The processing result is" + FileStandardization. resultJava); 80 FileOperate. log (SystemData. logPath, "I am the ox devil, file processing is complete:" + filePath); 81} 82 else 83 {84 myClientSocket. send (Encoding. UTF8.GetBytes ("I am the magic lord of the ox: the file does not exist"); 85} 86} 87 else 88 {89 FileOperate. log (SystemData. logPath, "No data received"); 90 myClientSocket. shutdown (SocketShutdown. both); 91 myClientSocket. close (); 92 break; 93} 94 95 96} 97 catch (Exception ex) 98 {99 FileOperate. log (SystemData. logPath, "File Processing exception:" + ex. message); 100 myClientSocket. shutdown (SocketShutdown. both); 101 myClientSocket. close (); 102 break; 103} 104} 105} 106 107}
Socket server

 

Related Article

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.