當前位置:首頁 » 雲伺服器 » 電腦連接etp伺服器

電腦連接etp伺服器

發布時間: 2023-07-24 00:26:22

㈠ 如何讓everything 在建立FTP伺服器時指定文件夾共享

現在我用的是當前最新版Everything:ver1.4.1.969


方法:在Everything工具=>索引=>NTFS,選中你想要的共享的分區,找到「僅包含」文本框,輸入文件夾的絕對路徑,多個文件夾用分號分割,並且在工具=>索引=>HTTP伺服器,最好設個密碼,應用即可。


有個缺陷函待官方解決,就是設置之後,在使用Everything搜索的時候除了指定的共享文件夾,其它的目錄都搜索不到結果,分享完後只能手動重新調回原來狀態。目前應該沒有解決辦法。


碼字不易,轉載請註明出處。

求採納。

㈡ socket實現過程,具體用的方法;怎麼實現非同步socket

基於C#的socket編程的TCP非同步實現
一、摘要
本篇博文闡述基於TCP通信協議的非同步實現。

二、實驗平台
Visual Studio 2010

三、非同步通信實現原理及常用方法
3.1 建立連接
在同步模式中,在伺服器上使用Accept方法接入連接請求,而在客戶端則使用Connect方法來連接伺服器。相對地,在非同步模式下,伺服器可以使用BeginAccept方法和EndAccept方法來完成連接到客戶端的任務,在客戶端則通過BeginConnect方法和EndConnect方法來實現與伺服器的連接。

BeginAccept在非同步方式下傳入的連接嘗試,它允許其他動作而不必等待連接建立才繼續執行後面程序。在調用BeginAccept之前,必須使用Listen方法來偵聽是否有連接請求,BeginAccept的函數原型為:

BeginAccept(AsyncCallback AsyncCallback, Ojbect state)
參數:

AsyncCallBack:代表回調函數

state:表示狀態信息,必須保證state中包含socket的句柄

使用BeginAccept的基本流程是:
(1)創建本地終節點,並新建套接字與本地終節點進行綁定;
(2)在埠上偵聽是否有新的連接請求;
(3)請求開始接入新的連接,傳入Socket的實例或者StateOjbect的實例。

參考代碼:

復制代碼
//定義IP地址
IPAddress local = IPAddress.Parse("127.0,0,1");
IPEndPoint iep = new IPEndPoint(local,13000);
//創建伺服器的socket對象
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
server.Bind(iep);
server.Listen(20);
server.BeginAccecpt(new AsyncCallback(Accept),server);
復制代碼
當BeginAccept()方法調用結束後,一旦新的連接發生,將調用回調函數,而該回調函數必須包括用來結束接入連接操作的EndAccept()方法。

該方法參數列表為 Socket EndAccept(IAsyncResult iar)

下面為回調函數的實例:

復制代碼
void Accept(IAsyncResult iar)
{
//還原傳入的原始套接字
Socket MyServer = (Socket)iar.AsyncState;
//在原始套接字上調用EndAccept方法,返回新的套接字
Socket service = MyServer.EndAccept(iar);
}
復制代碼
至此,伺服器端已經准備好了。客戶端應通過BeginConnect方法和EndConnect來遠程連接主機。在調用BeginConnect方法時必須注冊相應的回調函數並且至少傳遞一個Socket的實例給state參數,以保證EndConnect方法中能使用原始的套接字。下面是一段是BeginConnect的調用:

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)
IPAddress ip=IPAddress.Parse("127.0.0.1");
IPEndPoint iep=new IPEndPoint(ip,13000);
socket.BeginConnect(iep, new AsyncCallback(Connect),socket);
EndConnect是一種阻塞方法,用於完成BeginConnect方法的非同步連接誒遠程主機的請求。在注冊了回調函數後必須接收BeginConnect方法返回的IASynccReuslt作為參數。下面為代碼演示:

復制代碼
void Connect(IAsyncResult iar)
{
Socket client=(Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{

}
}
復制代碼

除了採用上述方法建立連接之後,也可以採用TcpListener類裡面的方法進行連接建立。下面是伺服器端對關於TcpListener類使用BeginAccetpTcpClient方法處理一個傳入的連接嘗試。以下是使用BeginAccetpTcpClient方法和EndAccetpTcpClient方法的代碼:

復制代碼
public static void DoBeginAccept(TcpListener listner)
{
//開始從客戶端監聽連接
Console.WriteLine("Waitting for a connection");
//接收連接
//開始准備接入新的連接,一旦有新連接嘗試則調用回調函數DoAcceptTcpCliet
listner.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpCliet), listner);
}

//處理客戶端的連接
public static void DoAcceptTcpCliet(IAsyncResult iar)
{
//還原原始的TcpListner對象
TcpListener listener = (TcpListener)iar.AsyncState;

//完成連接的動作,並返回新的TcpClient
TcpClient client = listener.EndAcceptTcpClient(iar);
Console.WriteLine("連接成功");
}
復制代碼
代碼的處理邏輯為:
(1)調用BeginAccetpTcpClient方法開開始連接新的連接,當連接視圖發生時,回調函數被調用以完成連接操作;
(2)上面DoAcceptTcpCliet方法通過AsyncState屬性獲得由BeginAcceptTcpClient傳入的listner實例;
(3)在得到listener對象後,用它調用EndAcceptTcpClient方法,該方法返回新的包含客戶端信息的TcpClient。

BeginConnect方法和EndConnect方法可用於客戶端嘗試建立與服務端的連接,這里和第一種方法並無區別。下面看實例:

復制代碼
public void doBeginConnect(IAsyncResult iar)
{
Socket client=(Socket)iar.AsyncState;
//開始與遠程主機進行連接
client.BeginConnect(serverIP[0],13000,requestCallBack,client);
Console.WriteLine("開始與伺服器進行連接");
}
private void requestCallBack(IAsyncResult iar)
{
try
{
//還原原始的TcpClient對象
TcpClient client=(TcpClient)iar.AsyncState;
//
client.EndConnect(iar);
Console.WriteLine("與伺服器{0}連接成功",client.Client.RemoteEndPoint);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{

}
}
復制代碼
以上是建立連接的兩種方法。可根據需要選擇使用。

3.2 發送與接受數據
在建立了套接字的連接後,就可以伺服器端和客戶端之間進行數據通信了。非同步套接字用BeginSend和EndSend方法來負責數據的發送。注意在調用BeginSend方法前要確保雙方都已經建立連接,否則會出異常。下面演示代碼:

復制代碼
private static void Send(Socket handler, 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.
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
復制代碼
接收數據是通過BeginReceive和EndReceive方法:

復制代碼
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());
}
}
復制代碼
上述代碼的處理邏輯為:

(1)首先處理連接的回調函數里得到的通訊套接字client,接著開始接收數據;
(2)當數據發送到緩沖區中,BeginReceive方法試圖從buffer數組中讀取長度為buffer.length的數據塊,並返回接收到的數據量bytesRead。最後接收並列印數據。除了上述方法外,還可以使用基於NetworkStream相關的非同步發送和接收方法,下面是基於NetworkStream相關的非同步發送和接收方法的使用介紹。
NetworkStream使用BeginRead和EndRead方法進行讀操作,使用BeginWreite和EndWrete方法進行寫操作,下面看實例:

復制代碼
static void DataHandle(TcpClient client)
{
TcpClient tcpClient = client;
//使用TcpClient的GetStream方法獲取網路流
NetworkStream ns = tcpClient.GetStream();
//檢查網路流是否可讀
if(ns.CanRead)
{
//定義緩沖區
byte[] read = new byte[1024];
ns.BeginRead(read,0,read.Length,new AsyncCallback(myReadCallBack),ns);
}
else
{
Console.WriteLine("無法從網路中讀取流數據");
}
}

public static void myReadCallBack(IAsyncResult iar)
{
NetworkStream ns = (NetworkStream)iar.AsyncState;
byte[] read = new byte[1024];
String data = "";
int recv;

recv = ns.EndRead(iar);
data = String.Concat(data, Encoding.ASCII.GetString(read, 0, recv));

//接收到的消息長度可能大於緩沖區總大小,反復循環直到讀完為止
while (ns.DataAvailable)
{
ns.BeginRead(read, 0, read.Length, new AsyncCallback(myReadCallBack), ns);
}
//列印
Console.WriteLine("您收到的信息是" + data);
}
復制代碼
3.3 程序阻塞與非同步中的同步問題
.Net里提供了EventWaitHandle類來表示一個線程的同步事件。EventWaitHandle即事件等待句柄,他允許線程通過操作系統互發信號和等待彼此的信號來達到線程同步的目的。這個類有2個子類,分別為AutoRestEevnt(自動重置)和ManualRestEvent(手動重置)。下面是線程同步的幾個方法:
(1)Rset方法:將事件狀態設為非終止狀態,導致線程阻塞。這里的線程阻塞是指允許其他需要等待的線程進行阻塞即讓含WaitOne()方法的線程阻塞;
(2)Set方法:將事件狀態設為終止狀態,允許一個或多個等待線程繼續。該方法發送一個信號給操作系統,讓處於等待的某個線程從阻塞狀態轉換為繼續運行,即WaitOne方法的線程不在阻塞;
(3)WaitOne方法:阻塞當前線程,直到當前的等待句柄收到信號。此方法將一直使本線程處於阻塞狀態直到收到信號為止,即當其他非阻塞進程調用set方法時可以繼續執行。

復制代碼
public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddress = ipHostInfo.AddressList[0];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local
//endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
復制代碼

上述代碼的邏輯為:

(1)試用了ManualRestEvent對象創建一個等待句柄,在調用BeginAccept方法前使用Rest方法允許其他線程阻塞;
(2)為了防止在連接完成之前對套接字進行讀寫操作,務必要在BeginAccept方法後調用WaitOne來讓線程進入阻塞狀態。

當有連接接入後系統會自動調用會調用回調函數,所以當代碼執行到回調函數時說明連接已經成功,並在函數的第一句就調用Set方法讓處於等待的線程可以繼續執行

㈢ 您好,我想用Python實現DHCP客戶與伺服器的交互,可否給我講解一下代碼

這個不是講解的問題,你要去看Linux操作系統中DHCP處理的源代碼

熱點內容
租虛擬伺服器有什麼用 發布:2025-02-07 01:17:55 瀏覽:4
視頻腳本策劃 發布:2025-02-07 01:15:37 瀏覽:565
外文免費資料庫 發布:2025-02-07 01:12:26 瀏覽:194
第一滴血ftp 發布:2025-02-07 01:12:24 瀏覽:424
仿草料源碼 發布:2025-02-07 01:08:31 瀏覽:532
python多線程map 發布:2025-02-07 01:04:37 瀏覽:462
python線程時間 發布:2025-02-07 01:04:26 瀏覽:793
精易編程 發布:2025-02-07 00:49:10 瀏覽:464
訪問桂綸鎂 發布:2025-02-07 00:49:00 瀏覽:938
安卓免費掃描哪個好 發布:2025-02-07 00:47:40 瀏覽:90