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


C# NetworkStream.BeginRead方法代码示例

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


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

示例1: RiceClient

        public RiceClient(TcpClient tcp, RiceListener parent, bool exchangeRequired)
        {
            this.tcp = tcp;
            this.parent = parent;
            this.exchangeRequired = exchangeRequired;

            ns = tcp.GetStream();
            Alive = true;

            try
            {
                if (exchangeRequired)
                {
                    buffer = new byte[56];
                    bytesToRead = buffer.Length;
                    ns.BeginRead(buffer, 0, 56, onExchange, null);
                }
                else
                {
                    buffer = new byte[4];
                    bytesToRead = buffer.Length;
                    ns.BeginRead(buffer, 0, 4, onHeader, null);
                }
            }
            catch (Exception ex) { Kill(ex); }
        }
开发者ID:oxykidz,项目名称:RiceEmu,代码行数:26,代码来源:RiceClient.cs

示例2: SocketHandler

 public SocketHandler(TcpClient client)
 {
     commandQueue = new Queue<string>();
     this.client = client;
     data = new byte[bufferSize];
     netStream = this.client.GetStream();
     netStream.BeginRead(data, 0, bufferSize, ReceiveMessage, null);
 }
开发者ID:henrikno,项目名称:CloudWars,代码行数:8,代码来源:SocketHandler.cs

示例3: SocketHandler

 public SocketHandler(TcpClient client)
 {
     commandQueue = new ConcurrentQueue<string>();
     this.client = client;
     data = new byte[bufferSize];
     netStream = this.client.GetStream();
     netStream.BeginRead(data, 0, bufferSize, ReceiveMessage, null);
     separators = new[] { '\n', '\r', '\f', '\0', (char)3 };
 }
开发者ID:henrikno,项目名称:SpaceBumper,代码行数:9,代码来源:SocketHandler.cs

示例4: ConnectComplete

        protected override void ConnectComplete(IAsyncResult result)
        {
            Socket.EndConnect(result);

            NetworkStream = new NetworkStream(Socket);
            NetworkStream.BeginRead(ReadBuffer, ReadBufferIndex, ReadBuffer.Length, DataReceived, null);

            SendRawMessage("PASS oauth:{0}", User.Password);
            SendRawMessage("NICK {0}", User.Nick);
        }
开发者ID:Roytazz,项目名称:TwitchLib,代码行数:10,代码来源:TwitchIrcClient.cs

示例5: Main

        static void Main(string[] args)
        {
            Console.Write("Bitte geben sie den Hostnamen ein: ");
            String host_name = Console.ReadLine();
            TcpClient client;

            try
            {
                client = new TcpClient(host_name, 666);
                stream = client.GetStream();
                stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(read_callback), stream);

                while (client.Connected && stream.CanWrite)
                {
                    String message = Console.ReadLine() + "\n";
                    byte[] outputArray = Encoding.UTF8.GetBytes(message);
                    try
                    {
                        stream.Write(outputArray, 0, outputArray.Length);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                }
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Fehler! Der Hostname darf nicht null sein.");
                Console.ReadLine();
                return;
            }
            catch (ArgumentOutOfRangeException)
            {
                Console.WriteLine("Ungültiger Port.");
                Console.ReadLine();
                return;
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                Environment.Exit(0);
            }
        }
开发者ID:Mrknister,项目名称:MUDEngine,代码行数:53,代码来源:Program.cs

示例6: ReadNetworkData

 private void ReadNetworkData(NetworkStream stream)
 {
     try
     {
         stream.BeginRead(_buffer, 0, _buffer.Length, ar =>
         {
             ParseBuffer(stream.EndRead(ar));
             ReadNetworkData(stream);
         }, stream);
     }
     catch
     {
     }
 }
开发者ID:Lynxa,项目名称:Lambent,代码行数:14,代码来源:NetworkReader.cs

示例7: Connect

 public void Connect(TcpClient tcp)
 {
     Tcp = tcp;
     netStream = tcp.GetStream();
     IsConnected = true;
     try
     {
         if (netStream.CanRead)
             netStream.BeginRead(recSizeBuf, 0, recSizeBuf.Length, ReceiveSize, netStream);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         StopClient();
     }
 }
开发者ID:Deiwos3,项目名称:IMS,代码行数:16,代码来源:Network.cs

示例8: Connect

        private void Connect(int port, int retryCount)
        {
            if (retryCount >= 5)
                return;
			try {
	            var client = new TcpClient();
	            client.Connect("127.0.0.1", port);
	            _currentPort = port;
	            _stream = client.GetStream();
	            _stream.BeginRead(_buffer, 0, _buffer.Length, ReadCompleted, _stream);
				IsConnected = true;
			} 
			catch 
			{
                Reconnect(retryCount);
			}
        }
开发者ID:jeremywiebe,项目名称:AutoTest.Net,代码行数:17,代码来源:SocketClient.cs

示例9: Listen

 /*public static void md5(string source)
 {
     byte[] data = System.Text.Encoding.UTF8.GetBytes(source);
     MD5 md = MD5.Create();
     byte [] cryptoData = md.ComputeHash(data);
     Console.WriteLine(System.Text.Encoding.UTF8.GetString(cryptoData));
     md.Clear();
 }
 */
 public void Listen()
 {
     try { new BluetoothClient(); }
     catch (Exception ex)
     {
         var msg = "Bluetooth init failed: " + ex;
         MessageBox.Show(msg);
         throw new InvalidOperationException(msg, ex);
     }
     Bluetoothlistener = new BluetoothListener(OurServiceClassId);
     Bluetoothlistener.ServiceName = OurServiceName;
     Bluetoothlistener.Start();
     Bluetoothclient = Bluetoothlistener.AcceptBluetoothClient();
     byte[] data = new byte[1024];
     Ns = Bluetoothclient.GetStream();
     Ns.BeginRead(data, 0, data.Length, new AsyncCallback(ReadCallBack), data);
     DataAvailable(this, "Begin to read");
 }
开发者ID:nickraz,项目名称:probaCrypt,代码行数:27,代码来源:Program.cs

示例10: MqttConnection

        /// <summary>
        /// Initializes a new instance of the <see cref="MqttConnection"/> class.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="port">The port.</param>
        private MqttConnection(string server, int port)
        {
            try
            {
                // connect and save off the stream.
                tcpClient = new TcpClient(server, port);
            }
            catch (SocketException ex)
            {
                throw new ConnectionException(
                    String.Format("The connection to the message broker {0}:{1} could not be made.", server, port),
                    ConnectionState.Faulted, ex);
            }

            networkStream = tcpClient.GetStream();

            // initiate a read for the next byte which will be the header bytes
            networkStream.BeginRead(headerByte, 0, 1, ReadComplete, networkStream);
        }
开发者ID:nuxleus,项目名称:nmqtt,代码行数:24,代码来源:MqttConnection.cs

示例11: OpenServer

        /// <summary>
        /// Otevre pripojeni se zadanym IP a portem serveru
        /// </summary>
        /// <param name="server">Ip adresa serveru napr 192.168.0.10</param>
        /// <param name="port">Port serveru</param>
        /// <param name="asyncRead">Povoleni asynchnoniho cteni </param>
        /// <returns>Vraci true pokud sepripoji</returns>
        public bool OpenServer(string server, int port, bool asyncRead)
        {
            try
                {
                    tcp = new TcpClient(server, port);
                    ns = tcp.GetStream();
                    isConnected = tcp.Connected;

                    if (asyncRead)
                    {
                        AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
                        ns.BeginRead(m_byBuff, 0, m_byBuff.Length, recieveData, null);
                    }
                }
                catch (Exception ex)
                {
                    printOutDbg("\nSetup Recieve Callback failed!\n" + ex.Message);
                    return false;
                }
                return true;
        }
开发者ID:BlueAndi,项目名称:vscp_software,代码行数:28,代码来源:tcpcli.cs

示例12: NetIO

        /// <summary>
        /// Create a new netIO class using a given socket.
        /// </summary>
        /// <param name="sock">The socket for this netIO class.</param>
        public NetIO(Socket sock, Dictionary<ushort, Packet> commandTable, Client client ,ClientManager manager)
        {
            this.sock = sock;
            this.stream = new NetworkStream(sock);
            this.commandTable = commandTable;
            this.client = client;
            this.currentClientManager = manager;

            this.callbackSize = new AsyncCallback(this.ReceiveSize);
            this.callbackData = new AsyncCallback(this.ReceiveData);
            this.nlock = new ReaderWriterLock();
            // Use the static key untill the keys have been exchanged
            this.clientKey = new byte[16];
            Encryption.StaticKey.CopyTo(this.clientKey, 0);
            this.serverKey = new byte[16];
            Encryption.StaticKey.CopyTo(this.serverKey, 0);

            this.isDisconnected = false;

            // Receive the size of the next packet and call ReceiveSize when finished
            if (sock.Connected)
            {
                try { stream.BeginRead(buffer, 0, 2, this.callbackSize, null); }
                catch (Exception ex) {
                    Logger.ShowError(ex, null);
                    try//this could crash the gateway somehow,so better ignore the Exception
                    {
                        this.Disconnect();
                    }
                    catch (Exception)
                    {
                    }
                    Logger.ShowWarning("Invalid packet head from:" + sock.RemoteEndPoint.ToString(), null);
                    return;
                }
            }
            else { this.Disconnect(); return; }
        }
开发者ID:Willyham,项目名称:SagaRO2,代码行数:42,代码来源:NetIO.cs

示例13: Read

		private int Read(NetworkStream stream, byte[] buffer, int count)
		{
			var ar = stream.BeginRead(buffer, 0, count, null, null);
			if (!ar.AsyncWaitHandle.WaitOne(SocksTimeout))
			{
				throw new SocksException("The proxy did not respond in a timely manner.");
			}
			count = stream.EndRead(ar);
			if (count < 2)
			{
				throw new SocksException("Unable to negotiate with the proxy.");
			}
			return count;
		}
开发者ID:derrickcreamer,项目名称:Floe,代码行数:14,代码来源:SocksTcpClient.cs

示例14: Connect

 /// <summary>
 /// Connects to TA server and resets internal data
 /// </summary>
 /// <param name="host">server host</param>
 /// <param name="port">server port</param>
 public void Connect(string host, int port)
 {
   tcp = new TcpClient(host, port);
   stream = tcp.GetStream();
   readBuffer = new byte[tcp.ReceiveBufferSize];
   readPosition = 0;
   stream.BeginRead(readBuffer, 0, readBuffer.Length, new AsyncCallback(DataRecieveCallback), this);
 }
开发者ID:genxinzou,项目名称:svn-spring-archive,代码行数:13,代码来源:ServerConnection.cs

示例15: GetResponse

        private static string GetResponse(NetworkStream stream, out string response)
        {
            byte[] readBuffer = new byte[32];
            var asyncReader = stream.BeginRead(readBuffer, 0, readBuffer.Length, null, null);
            WaitHandle handle = asyncReader.AsyncWaitHandle;

            // Give the reader 2seconds to respond with a value
            bool completed = handle.WaitOne(1000, false);
            if (completed)
            {
                int bytesRead = stream.EndRead(asyncReader);

                StringBuilder message = new StringBuilder();
                message.Append(Encoding.ASCII.GetString(readBuffer, 0, bytesRead));

                if (bytesRead == readBuffer.Length)
                {
                    // There's possibly more than 32 bytes to read, so get the next
                    // section of the response
                    string continuedResponse;
                    if (GetResponse(stream, out continuedResponse) != "")
                    {
                        message.Append(continuedResponse);
                    }
                }

                response = message.ToString();
                return response;
            }
            else
            {
                int bytesRead = stream.EndRead(asyncReader);
                if (bytesRead == 0)
                {
                    // 0 bytes were returned, so the read has finished
                    response = string.Empty;
                    return response;
                }
                else
                {
                    StringBuilder message = new StringBuilder();
                    message.Append(Encoding.ASCII.GetString(readBuffer, 0, bytesRead));

                    if (bytesRead == readBuffer.Length)
                    {
                        // There's possibly more than 32 bytes to read, so get the next
                        // section of the response
                        string continuedResponse;
                        if (GetResponse(stream, out continuedResponse) != "")
                        {
                            message.Append(continuedResponse);
                        }
                    }

                    response = message.ToString();
                    return response;
                }
            }
        }
开发者ID:fiLLLip,项目名称:xbmc-light-controller,代码行数:59,代码来源:Form1.cs


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