下面的示例程序創建一個連接到服務器的客戶端。該客戶端是用異步套接字生成的,因此在等待服務器返回響應時不掛起客戶端應用程序的執行。該應用程序將字符串發送到服務器,然後在控制檯顯示該服務器返回的字符串。
using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; // State object for receiving data from remote device. public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer. public const int BufferSize = 256; // Receive buffer. public byte[] buffer = new byte[BufferSize]; // Received data string. public StringBuilder sb = new StringBuilder(); } public class AsynchronousClient { // The port number for the remote device. private const int port = 11000; // ManualResetEvent instances signal completion. private static ManualResetEvent connectDone = new ManualResetEvent(false); private static ManualResetEvent sendDone = new ManualResetEvent(false); private static ManualResetEvent receiveDone = new ManualResetEvent(false); // The response from the remote device. private static String response = String.Empty; private static void StartClient() { // Connect to a remote device. try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com"); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); // Send test data to the remote device. Send(client,"This is a test<EOF>"); sendDone.WaitOne(); // Receive the response from the remote device. Receive(client); receiveDone.WaitOne(); // Write the response to the console. Console.WriteLine("Response received : {0}", response); // Release the socket. client.Shutdown(SocketShutdown.Both); client.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete the connection. client.EndConnect(ar); Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); // Signal that the connection has been made. connectDone.Set(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void Receive(Socket client) { try { // Create the state object. StateObject state = new StateObject(); state.workSocket = client; // Begin receiving the data from the remote device. client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void ReceiveCallback( IAsyncResult ar ) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject) ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); // Get the rest of the data. client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); } else { // All the data has arrived; put it in response. if (state.sb.Length > 1) { response = state.sb.ToString(); } // Signal that all bytes have been received. receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void Send(Socket client, String data) { // Convert the string data to byte data using ASCII encoding. byte[] byteData = Encoding.ASCII.GetBytes(data); // Begin sending the data to the remote device. client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); } private static void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = client.EndSend(ar); Console.WriteLine("Sent {0} bytes to server.", bytesSent); // Signal that all bytes have been sent. sendDone.Set(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } public static int Main(String[] args) { StartClient(); return 0; } }
最近需要在uos上部署系統,考慮用docker會簡單一些,但uos默認沒有docker,於是嘗試安裝。兩種安裝方法,1是改安裝源地址然後yum或dnf安裝;2是手動下載靜態包安裝。原則上1簡單一些,但爲了更好理解docker的運行邏輯,選擇
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 前言 單例應用的太廣泛,大家應該都用過,本文主要是想聊聊線程安全的單例以及反序列化破壞單例的情況。 1、概念 確保某一個類只有一個實例,而
1 概述 通道是Golang提供的一種基本類型,它可以實現在協程之間的單向通信和雙向通信、發送和接收數據、以及協程同步。 channel的本質是一個隊列,遵循先進先出原則。channel是線程安全的,在任何給定時間,一個數據被設計爲只有一
簡介 go-python Python提供了豐富的C-API。而C和Go又可以通過cgo無縫集成。所以,直接通過Golang調用libpython,就可以實現Go調Python的功能了。但是過程比較複雜,而go-python提供了針對CPy
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 之前本人在找工作面試時在Redis相關問題上可栽了跟頭。在面試前按常規套路準備了一下,比如 Redis 的常用5種數據結構,Redis持久化
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 本文要點 即將於 2020 年 9 月發佈的 Java SE 15 將引入“封印類(sealed class)”(JEP 360),並將其作
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 簡介 Spring Boot 2.2.0 版本開始引入 JUnit 5 作爲單元測試默認庫,在 Spring Boot 2.2.0 版本之前
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 編者按:作爲一個Java開發者,你是否曾爲在PyTorch上部署模型而苦惱?這篇來自AWS軟件工程師的投稿,結合實例,詳細介紹了DJL這個
阿里雲最佳實踐頻道:【點擊查看更多上雲最佳實踐】這裏有豐富的企業上雲最佳實踐,從典型場景入門,提供一系列項目實踐方案,降低企業上雲門檻的同時滿足您的需求! 場景描述 很多客戶最開始使用雲的時候,會把雲(服務器)簡單地當成普通的物理服務器
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 【導讀】近日,由阿里雲 20+ 位雲原生技術專家共同編撰的《雲原生架構白皮書》正式對外發布。作爲業界第一本全方位構建雲原生架構規劃與實踐全景
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 這是我有關起搏器生態系統安全性研究的系列文章中的第一篇,此研究包括在Marie Moe於2015年在SINTEF發起的一個內部項目。 第一篇
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 沒有一家巨頭會想錯過定義未來世界的機會。這是不可逆的時間流裏不再打開第二次的機會窗口。 2020年初,阿里雲宣佈在未來3年再投2000億元,
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 越來越多的企業如今採用雲計算技術來進行現代應用程序的開發,因此需要了解將雲計算與傳統內部部署IT架構進行區分的關鍵特性。雖然雲計算帶來了越來
雲棲號快速入門:【點擊查看更多雲產品快速入門】不知道怎麼入門?這裏分分鐘解決新手入門等基礎問題,可快速完成產品配置操作! 本文檔介紹連續複製型容災(CDR)在操作系統、基礎架構、數據庫和應用等方面的限制。 操作系統 CDR目前僅支持以下服務
雲棲號資訊:【點擊查看更多行業資訊】在這裏您可以找到不同行業的第一手的上雲資訊,還在等什麼,快來! 之前的時候,阿粉一直在看同事面試,但是呢,阿粉並沒有自己去面試,而無意間打開Boss的時候,發現一家公司私聊了我,我回復了一下之後,竟然說