⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 windows ce 程序设计书 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
            // Send name length
            StringOut ("Sending file name");
            if (!SendDWord (s, nLen+1))
            {
                StringOut (string.Format ("Error sending name length"));
                return;
            }
            // Send name
            UTF8Encoding UTF8enc = new UTF8Encoding();
            UTF8enc.GetBytes (strNameOnly, 0, nLen, buff, 0);
            buff[nLen] = 0;
            try 
            {
                s.Write (buff, 0, nLen+1);
            }
            catch (SocketException ex)
            {
                StringOut (string.Format ("Sock Write exception {0}", 
                                          ex.ErrorCode));
            }
            StringOut ("Sending file");
            // Send file length
            nLen = (int)fs.Length;
            if (!SendDWord (s, nLen))
            {
                StringOut ("Error sending file list");
            }

            // Read back file open return code
            StringOut ("Reading file create ack");
            RecvDWord (s, out rc);
            if (rc != 0)
            {
                StringOut (string.Format ("Bad Ack code {0}", rc));
                fs.Close();
                irClient.Close();
                s.Close();
                return;
            }
            StringOut ("ack received");

            // Send file data
            while (nLen > 0) 
            {
                int j = -1;
                try 
                {
                    j = (nLen > buff.Length) ? buff.Length : nLen;
                    StringOut (string.Format("Sending {0} bytes", j));
                    fs.Read (buff, 0, j);
                    s.Write (buff, 0, j);
                    nLen -= j;

                    if (!RecvDWord (s, out j))
                        break;
                    if (j != 0)
                    {
                        StringOut ("Error ack");
                        break;
                    }
                }
                catch (SocketException socex)
                {
                    StringOut (string.Format ("5 Sock Err {0} ({1},{2}",
                        socex.ErrorCode, nLen, j));
                    break;
                }
                catch (IOException ioex)
                {
                    StringOut (string.Format ("File Error {0}", 
                               ioex.Message));
                    break;
                }
                // Allow other events to happen during loop
                Application.DoEvents();
            }
            StringOut (string.Format("File sent"));

            s.Close();          // Close the stream
            irClient.Close();   // Close the socket
            fs.Close();         // close the file
            return;
        }
        /// <summary>
        /// Sends a DWORD to the other device
        /// </summary>
        /// <param name="s"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        bool SendDWord (Stream s, int i) 
        {
            byte[] b = BitConverter.GetBytes (i);
            try 
            {
                s.Write (b, 0, 4);
            }
            catch (SocketException ex)
            {
                StringOut (string.Format ("Err {0} writing dword", 
                                          ex.ErrorCode));
                return false;
            }
            return true;
        }
        /// <summary>
        /// Receiveds a DWORD from the other device
        /// </summary>
        /// <param name="s"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        bool RecvDWord (Stream s, out int i) 
        {
            byte[] b = new byte[4];
            try 
            {
                s.Read (b, 0, 4);
            }
            catch (SocketException ex)
            {
                StringOut (string.Format ("Err {0} reading dword", 
                                          ex.ErrorCode));
                i = 0;
                return false;
            }
            i = BitConverter.ToInt32 (b, 0);
            return true;
        }
        /// <summary>
        /// Server thread
        /// </summary>
        public void SrvRoutine() 
        {
            IrDAListener irListen;
            FileStream fs;
            string strFileName;
            int nLen;
            IrDAClient irClientSrv;
            Stream s;
            byte[] buff = new byte[4096];

            try 
            {
                irListen = new IrDAListener("MySqurt");
                irListen.Start();
            }
            catch (SocketException ex)
            {
                StringOut (string.Format("Err {0} creating IrDAListener",
                                          ex.ErrorCode));
                return;
            }
            StringOut ("IrdaListener created");

            while (isRunning) 
            {
                if (irListen.Pending())
                {
                    try 
                    {
                        StringOut ("Calling AcceptIrDAClient");
                        irClientSrv = irListen.AcceptIrDAClient();
                        StringOut ("AcceptIrDAClient returned");
                        s = irClientSrv.GetStream();
                    }
                    catch (SocketException ex)
                    {
                        StringOut (string.Format ("Sock exception {0}",
                                                  ex.ErrorCode));
                        continue;
                    }

                    // Get name length
                    StringOut ("Getting file name");
                    if (!RecvDWord (s, out nLen))
                    {
                        StringOut ("Error getting name length");
                        s.Close();
                        continue;
                    }
                    // Read name
                    try 
                    {
                        s.Read (buff, 0, nLen);
                    }
                    catch (SocketException ex)
                    {
                        StringOut (string.Format ("Read exception {0}",
                                                  ex.ErrorCode));
                        s.Close();
                        continue;
                    }
                    UTF8Encoding UTF8enc = new UTF8Encoding();
                    //Trim terminating zero
                    char[] ch = UTF8enc.GetChars (buff, 0, nLen-1); 
                    strFileName = new string (ch);
                    StringOut ("Receiving file " + strFileName);

                    // Get file length
                    if (!RecvDWord (s, out nLen))
                    {
                        StringOut ("Error getting file length");
                    }
                    StringOut (string.Format ("File len: {0}", nLen));
                    try 
                    {
                        fs = new FileStream (strFileName, 
                                      FileMode.Create,
                                      FileAccess.Read|FileAccess.Write);
                    }
                    catch (IOException ioex)
                    {
                        StringOut (string.Format("Error opening file"));
                        StringOut (ioex.Message);
                        SendDWord (s, -3);
                        s.Close();
                        continue;
                    }
                    StringOut ("File opened");

                    // Send file open return code
                    StringOut ("Send file create ack");
                    if (!SendDWord (s, 0))
                    {
                        StringOut ("fail sending ack code");
                        fs.Close();
                        s.Close();
                        break;
                    }
                    int nTotal = 0;
                    // Send file data
                    while (nLen > 0) 
                    {
                        int BlkSize = -1;
                        try 
                        {
                            BlkSize = (nLen > buff.Length) ? 
                                                     buff.Length : nLen;
                            int k = 0, BytesRead = 0;
                            while (BlkSize > k)  
                            {
                                // Wait for data
                                if (!((NetworkStream)s).DataAvailable)
                                    Thread.Sleep(100);
                                // Read it
                                BytesRead = s.Read (buff, k, BlkSize-k);
                                StringOut (string.Format ("Bytes: {0}", 
                                    BytesRead));
                                k += BytesRead;
                            }
                            fs.Write (buff, 0, BlkSize);
                            StringOut ("Send Ack");
                            if (!SendDWord (s, 0))
                            {
                                StringOut ("Error sending ack");
                                break;
                            }
                            nLen -= BlkSize;
                            nTotal += BlkSize;
                        }
                        catch (SocketException socex)
                        {
                            StringOut (string.Format ("Sock Err {0}", 
                                                      socex.ErrorCode));
                            break;
                        }
                        catch (IOException ioex)
                        {
                            StringOut (string.Format ("File Err {0}", 
                                ioex.Message));
                            StringOut (ioex.Message);
                            break;
                        }
                    }
                    StringOut (string.Format("File received {0} bytes.",
                                             nTotal));
                    // Receive close code
                    RecvDWord (s, out nLen);
                    fs.Close();
                    s.Close();
                }
                if (isRunning)
                    Thread.Sleep (500);
            }
            irListen.Stop();
            return;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -