1 class Program 2 { 3 private static byte[] result = new byte[1024]; 4 static void Main(string[] args) 5 { 6 //設定伺服器IP地址 7 IPAddress ip = IPAddress.Pa ...
最近因為公司項目的需求,CAD作為服務端在伺服器中常駐運行,等待客戶端遠程發送執行任務的指令,最終確認用Socket-tcp通訊,CAD需要實時監聽客戶端發送的消息,這時就需要開啟線程執行Socket的監聽任務。 這樣問題就來了,CADAPI不支持多線程,您只能從主線程調用API函數,如果您處於不同的線程,則必須將調用集中到主線程上。 最後查了半天的資料發現,最簡單的實現方式,是建立在主線程system.windows.forms.control對象並調用它的啟動功能,做最後處理invoke()功能。 下麵分別是客戶端和服務端的demo
1 class Program 2 { 3 private static byte[] result = new byte[1024]; 4 static void Main(string[] args) 5 { 6 //設定伺服器IP地址 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 try 11 { 12 clientSocket.Connect(new IPEndPoint(ip, 8885)); //配置伺服器IP與埠 13 Console.WriteLine("連接伺服器成功"); 14 } 15 catch 16 { 17 Console.WriteLine("連接伺服器失敗,請按回車鍵退出!"); 18 return; 19 } 20 //通過clientSocket接收數據 21 int receiveLength = clientSocket.Receive(result); 22 Console.WriteLine("接收伺服器消息:{0}", Encoding.UTF8.GetString(result, 0, receiveLength)); 23 //通過 clientSocket 發送數據 24 for (int i = 0; i < 13; i++) 25 { 26 try 27 { 28 Thread.Sleep(1000); //等待1秒鐘 29 string sendMessage = "C:\\Users\\Administrator\\Desktop\\原始CAD文件\\14412-kujiale.dxf"; 30 clientSocket.Send(Encoding.UTF8.GetBytes(sendMessage)); 31 Console.WriteLine("向伺服器發送消息:{0}" + sendMessage); 32 receiveLength = clientSocket.Receive(result); 33 Console.WriteLine("接收伺服器消息:{0}", Encoding.UTF8.GetString(result, 0, receiveLength)); 34 } 35 catch 36 { 37 clientSocket.Shutdown(SocketShutdown.Both); 38 clientSocket.Close(); 39 break; 40 } 41 } 42 clientSocket.Shutdown(SocketShutdown.Both); 43 clientSocket.Close(); 44 Console.WriteLine("發送完畢,按回車鍵退出"); 45 Console.ReadLine(); 46 } 47 }Socket客戶端-控制台測試
1 public class SocketServer 2 { 3 //通過winform控制項進行後臺處理,CADAPI不支持多線程 4 static Control syncCtrl; 5 //承載接收數據 6 private static byte[] result = new byte[1024]; 7 //埠 8 private static int myProt = 8885; 9 10 private static Socket serverSocket; 11 12 delegate void FinishedProcessingDelegate(); 13 public static void InitSocket() 14 { 15 //創建控制項 16 syncCtrl = new Control(); 17 syncCtrl.CreateControl(); 18 //創建通訊對象 19 serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 20 //綁定IP地址:埠 21 serverSocket.Bind(new IPEndPoint(IPAddress.Any, myProt)); 22 //設定最多10個排隊連接請求 23 serverSocket.Listen(10); 24 //通過Clientsoket發送數據 25 Thread myThread = new Thread(ListenClientConnect); 26 myThread.Start(); 27 } 28 /// <summary> 29 /// 監聽客戶端連接 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("我是牛魔王")); 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 /// 接收消息 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 //通過clientSocket接收數據 65 int receiveNumber = myClientSocket.Receive(result); 66 if (receiveNumber>0) 67 { 68 //獲取文件名稱 69 string filePath = Encoding.UTF8.GetString(result, 0, receiveNumber); 70 //判斷文件是否存在 71 if (FileOperate.FileExists(filePath)) 72 { 73 FileStandardization.filePathByJava = filePath; 74 if (syncCtrl.InvokeRequired) 75 { 76 //開始執行CAD數據處理的任務 77 syncCtrl.Invoke(new FinishedProcessingDelegate(FileStandardization.TransMianByJava)); 78 } 79 myClientSocket.Send(Encoding.UTF8.GetBytes("我是牛魔王:處理結果是" + FileStandardization.resultJava)); 80 FileOperate.Log(SystemData.logPath, "我是牛魔王,文件處理完成:" + filePath); 81 } 82 else 83 { 84 myClientSocket.Send(Encoding.UTF8.GetBytes("我是牛魔王:文件不存在")); 85 } 86 } 87 else 88 { 89 FileOperate.Log(SystemData.logPath, "沒有收到數據了"); 90 myClientSocket.Shutdown(SocketShutdown.Both); 91 myClientSocket.Close(); 92 break; 93 } 94 95 96 } 97 catch (Exception ex) 98 { 99 FileOperate.Log(SystemData.logPath,"文件處理異常:"+ex.Message); 100 myClientSocket.Shutdown(SocketShutdown.Both); 101 myClientSocket.Close(); 102 break; 103 } 104 } 105 } 106 107 }Socket服務端