通过蓝牙发送数据时发出问题

时间:2015-08-09 08:40:57

标签: c#

我使用以下代码让服务器通过蓝牙在客户端之间发送/接收一些字符串,并将一些文件发送到客户端:

  try
  {
    //////////WELCOME DATA MESSAGE TO BE SENT (START)///////////////
    byte[] sync_welcome_sent = new byte[2] { 0x55, 0xAA };
    byte[] message_type_welcome_sent = new byte[1] { 0x00 }; //will need if statement
    byte[] number_of_slides_welcome_sent = BitConverter.GetBytes((System.Int16)Directory.GetFiles(image_path).Length); //count number of pictures in local folder
    byte[] recording_state_welcome_sent = new byte[1] { 0x00 }; //will need if statement

    byte[] welcome_data_message_sent = new byte[sync_welcome_sent.Length + message_type_welcome_sent.Length + number_of_slides_welcome_sent.Length + recording_state_welcome_sent.Length];

    //copy all byte array into one big packet to be sent
    sync_welcome_sent.CopyTo(welcome_data_message_sent, 0);
    message_type_welcome_sent.CopyTo(welcome_data_message_sent, sync_welcome_sent.Length);
    number_of_slides_welcome_sent.CopyTo(welcome_data_message_sent, sync_welcome_sent.Length + message_type_welcome_sent.Length);
    recording_state_welcome_sent.CopyTo(welcome_data_message_sent, sync_welcome_sent.Length + message_type_welcome_sent.Length + number_of_slides_welcome_sent.Length);

    //send the packet
    mStream.Write(welcome_data_message_sent, 0, welcome_data_message_sent.Length);

    //clear the stream for next packet
    mStream.Flush();

   //display the message to the user
   updateUI("Sent: " + Encoding.ASCII.GetString(welcome_data_message_sent));
  ////////////WELCOME DATA MESSAGE TO BE SENT (END)///////////////

  ///////////WELCOME DATA MESSAGE TO BE RECEIVED (START)//////////
  byte[] welcome_data_message_received = new byte[5];
  mStream.Read(welcome_data_message_received, 0, welcome_data_message_received.Length);

  //clear the stream for next packet
  mStream.Flush();

  //instantiate arrays for received data
  byte[] sync_welcome_received = new byte[2];
  byte[] message_type_welcome_received = new byte[1];
  byte[] slideID_welcome_received = new byte[2];

  //copy bytes from received packet to separate arrays
  Array.Copy(welcome_data_message_received, 0, sync_welcome_received, 0, 2);
  Array.Copy(welcome_data_message_received, 2, message_type_welcome_received, 0, 1);
  Array.Copy(welcome_data_message_received, 3, slideID_welcome_received, 0, 2);

  //reverse all byte arrays
  if (BitConverter.IsLittleEndian)
  {
     Array.Reverse(sync_welcome_received);
     Array.Reverse(message_type_welcome_received);Array.Reverse(slideID_welcome_received);
  }

  //retrieve the received data into original format
  int message_type_welcome_received_final = (int)message_type_welcome_received[0]; //then convert all sbyte array's bytes to int
  int slideID_begin_from = BitConverter.ToInt16(slideID_welcome_received, 0); //then convert all sbyte array's bytes to int

  updateUI("Received: " + Encoding.ASCII.GetString(welcome_data_message_received));
  /////////////WELCOME DATA MESSAGE TO BE RECEIVED (END)///////////////

  for (int i = slideID_begin_from; i <= count; i++)
  {
     //////////////SLIDE DATA MESSAGE TO BE SENT (START)//////////////
     byte[] sync_data_sent = new byte[2] { 0x55, 0xAA };
     byte[] message_type_data_sent = new byte[1] { 0x01 }; //will need if statement
     byte[] slide_id_data_sent = BitConverter.GetBytes((System.Int16)i); //slideID that is being sent to the client
     byte[] image_only_data_sent = new byte[1] { 0x00 }; //will need if statement

     byte[] image_data_data_sent = File.ReadAllBytes(picture_paths[i]);
     byte[] image_data_length_data_sent = BitConverter.GetBytes(image_data_data_sent.Length);

     byte[] text_data_data_sent;
     byte[] text_data_length_data_sent;

     if (!is_image_only)
     {
        text_data_data_sent = Encoding.ASCII.GetBytes(slide_notes[i]);
        text_data_length_data_sent = BitConverter.GetBytes(text_data_data_sent.Length);
     }
     else
     {
        text_data_data_sent = new byte[0];
        text_data_length_data_sent = new byte[0];
     }    

     byte[] slide_data_message_sent = new byte[sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length + image_only_data_sent.Length + image_data_data_sent.Length + text_data_data_sent.Length + image_data_length_data_sent.Length + text_data_length_data_sent.Length];

     //copy all byte array into one big packet to be sent
     sync_data_sent.CopyTo(slide_data_message_sent, 0);
     message_type_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length);
     slide_id_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length);
     image_only_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length);
     image_data_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length + image_only_data_sent.Length);
     text_data_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length + image_only_data_sent.Length + image_data_data_sent.Length);
     image_data_length_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length + image_only_data_sent.Length + image_data_data_sent.Length + text_data_data_sent.Length);
     text_data_length_data_sent.CopyTo(slide_data_message_sent, sync_data_sent.Length + message_type_data_sent.Length + slide_id_data_sent.Length + image_only_data_sent.Length + image_data_data_sent.Length + text_data_data_sent.Length + image_data_length_data_sent.Length);


     //send the packet
     mStream.Write(slide_data_message_sent, 0, slide_data_message_sent.Length);

     //clear the stream for next packet
     mStream.Flush();

     //display the message to the user
     updateUI("Sent: " + Encoding.ASCII.GetString(slide_data_message_sent));
     /////////SLIDE DATA MESSAGE TO BE SENT (END)/////////////////

     /////////SLIDE DATA MESSAGE TO BE RECEIVED (START)///////////
     byte[] slide_data_message_received = new byte[5];
     mStream.Read(slide_data_message_received, 0, slide_data_message_received.Length);

     //clear the stream for next packet
     mStream.Flush();

     //instantiate arrays for received data
     byte[] sync_slide_received = new byte[2];
     byte[] message_type_slide_received = new byte[1];
     byte[] slideID_slide_received = new byte[2];

     //copy bytes from preceived packet to separate arrays
     Array.Copy(slide_data_message_received, 0, sync_slide_received, 0, 2);
     Array.Copy(slide_data_message_received, 2, message_type_slide_received, 0, 1);
     Array.Copy(slide_data_message_received, 3, slideID_slide_received, 0, 2);

     //retrieve the received data into original format
     int message_type_slide_received_final = (int)message_type_slide_received[0]; //then convert all sbyte array's bytes to int
     int slideID_data_received = BitConverter.ToInt16(slideID_slide_received, 0); //then convert all sbyte array's bytes to int

     updateUI("Received: " + Encoding.ASCII.GetString(slide_data_message_received));
     ///////////SLIDE DATA MESSAGE TO BE RECEIVED (END)////////////
   }                
}
catch (IOException exception)
{
   updateUI("Client has disconnected");
 }
}

问题出现在这里:

mStream.Write(slide_data_message_sent, 0, slide_data_message_sent.Length);

一旦发送此数据包,它就会捕获异常,客户端会断开连接。因此,第二次将数据放入写入流时会出现问题。有人可以帮帮我吗?

0 个答案:

没有答案