当前位置: 首页>>代码示例>>C#>>正文


C# Socket.BeginSendFile方法代码示例

本文整理汇总了C#中System.Net.Sockets.Socket.BeginSendFile方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginSendFile方法的具体用法?C# Socket.BeginSendFile怎么用?C# Socket.BeginSendFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.Socket的用法示例。


在下文中一共展示了Socket.BeginSendFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendAsyncFile

		public void SendAsyncFile ()
		{
			Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

			serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
			serverSocket.Listen (1);

			var mReceived = new ManualResetEvent (false);

			serverSocket.BeginAccept (AsyncCall => {
				byte[] bytes = new byte [1024];

				Socket listener = (Socket)AsyncCall.AsyncState;
				Socket client = listener.EndAccept (AsyncCall);
				client.Receive (bytes, bytes.Length, 0);
				client.Close ();
				mReceived.Set ();
			}, serverSocket);
			
			Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			clientSocket.Connect (serverSocket.LocalEndPoint);
			clientSocket.NoDelay = true;
						
			// Initialize buffer used to create testing file
			var buffer = new byte [1024];
			for (int i = 0; i < 1024; ++i)
				buffer [i] = (byte) (i % 256);
			
			string temp = Path.GetTempFileName ();
			try {
				// Testing file creation
				using (StreamWriter sw = new StreamWriter (temp)) {
					sw.Write (buffer);
				}

				var mSent = new ManualResetEvent (false);

				// Async Send File to server
				clientSocket.BeginSendFile(temp, (ar) => {
					Socket client = (Socket) ar.AsyncState;
					client.EndSendFile (ar);
					mSent.Set ();
				}, clientSocket);

				if (!mSent.WaitOne (1500))
					throw new TimeoutException ();
				if (!mReceived.WaitOne (1500))
					throw new TimeoutException ();
			} finally {
				if (File.Exists (temp))
					File.Delete (temp);
					
				clientSocket.Close ();
				serverSocket.Close ();
			}
		}
开发者ID:Numpsy,项目名称:mono,代码行数:56,代码来源:SocketTest.cs

示例2: SendAsyncFile

		public void SendAsyncFile ()
		{
			Socket serverSocket = StartSocketServer ();
			
			Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			clientSocket.Connect (serverSocket.LocalEndPoint);
			clientSocket.NoDelay = true;
						
			// Initialize buffer used to create testing file
			var buffer = new byte [1024];
			for (int i = 0; i < 1024; ++i)
				buffer [i] = (byte) (i % 256);
			
			string temp = Path.GetTempFileName ();
			try {
				// Testing file creation
				using (StreamWriter sw = new StreamWriter (temp)) {
					sw.Write (buffer);
				}

				var m = new ManualResetEvent (false);

				// Async Send File to server
				clientSocket.BeginSendFile(temp, (ar) => {
					Socket client = (Socket) ar.AsyncState;
					client.EndSendFile (ar);
					m.Set ();
				}, clientSocket);

				if (!m.WaitOne (1500))
					throw new TimeoutException ();
				m.Reset ();
			} finally {
				if (File.Exists (temp))
					File.Delete (temp);
					
				clientSocket.Close ();
				serverSocket.Close ();
			}
		}
开发者ID:Therzok,项目名称:mono,代码行数:40,代码来源:SocketTest.cs

示例3: SendFile

        /// <summary>
        /// Attempts to asynchronously serve a file on the specified socket.
        /// Sends a HTTP 404 error on the socket if the file is not found.
        /// </summary>
        /// <param name="filePath">The local path of a file</param>
        /// <param name="handler">The socket to send the file data on</param>
        private static void SendFile(string filePath, Socket handler)
        {
            if (File.Exists(filePath))
            {
                byte[] data = File.ReadAllBytes(filePath);

                // 200 OK
                string header = BuildResponseHeader(GetMimeType(filePath), data.Length, HttpStatusCode.OK);
                Send(handler, header);

                StateObject state = new StateObject();
                state.Socket = handler;
                state.Data.Append(filePath);
                handler.BeginSendFile(filePath, new AsyncCallback(SendFileCallback), state);
            }
            else
            {
                // 404 NotFound
                string code = (int)HttpStatusCode.NotFound + " " + HttpStatusCode.NotFound;
                Send(handler, BuildResponseHeader("text/plain; charset=UTF-8", code.Length, HttpStatusCode.NotFound));
                Send(handler, code);
            }
        }
开发者ID:NpoSaut,项目名称:netTrackRider,代码行数:29,代码来源:GEServer.cs


注:本文中的System.Net.Sockets.Socket.BeginSendFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。