Exemplo n.º 1
0
        /**数据发送函数*/
        private void sendPacketFun(object clientSocket)
        {
            if (clientSocket == null)
            {
                return;
            }
            Socket client = (Socket)clientSocket;

            if (!client.Connected)
            {
                return;
            }
            NetworkStream stream = new NetworkStream(client);
            BinaryWriter  writer = new BinaryWriter(stream);

            sendMessage2Client("hello,this is server");
            while (isClientRun)
            {
                //TODO
                SendPacket sendpacket = sendPacketQueue.Dequeue();
                if (sendpacket != null)
                {
                    // Console.WriteLine("sendPacketThread***********");
                    try
                    {
                        SendPacket.PacketType packetType = sendpacket.getPacketType();
                        switch (packetType)
                        {
                        case SendPacket.PacketType.BITMAP:
                            writer.Write((byte)packetType);
                            writer.Write((Int32)sendpacket.getbitmapBytesLength());
                            writer.Write((byte)sendpacket.getBitmapType());
                            writer.Write((Int16)sendpacket.getCursorPoint().getXPoint());
                            writer.Write((Int16)sendpacket.getCursorPoint().getYPoint());
                            List <ShortRec> difPointsList = sendpacket.getDifPointsList();
                            Int16           difNum        = 0;
                            if (difPointsList != null)
                            {
                                difNum = (Int16)difPointsList.Count;
                            }
                            writer.Write((Int16)difNum);

                            if (difNum > 0)
                            {
                                List <ShortRec> difPoints = sendpacket.getDifPointsList();
                                foreach (ShortRec dif in difPoints)
                                {
                                    writer.Write(dif.xPoint);
                                    writer.Write(dif.yPoint);
                                    writer.Write(dif.width);
                                    writer.Write(dif.height);
                                }
                            }
                            writer.Write(sendpacket.getBitByts(), 0, sendpacket.getbitmapBytesLength());
                            writer.Flush();
                            break;

                        case SendPacket.PacketType.TEXT:
                            writer.Write((byte)packetType);
                            byte[] sendBytes = Encoding.UTF8.GetBytes(sendpacket.getStringValue());
                            writer.Write(sendBytes.Length);
                            writer.Write(sendBytes);
                            writer.Flush();
                            break;

                        default:
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("sendThread:client is off line");
                        stopClient();/**clean socket*/
                        Console.WriteLine(ex.Message);
                        ErrorInfo.getErrorWriter().writeErrorMassageToFile(ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                    }
                }
            }
        }