如何解决System.FormatException?

时间:2012-05-22 12:52:41

标签: c# sockets formatexception

请告诉我如何在PC之间进行传输时解决此异常。

异常抛出:

  

(System.FormatException)

     

抛出了System.FormatException:“输入字符串的格式不正确。”

在线:

fileSize = Convert.ToInt32(Encoding.UTF8.GetString(byteFileSize));

我的整个代码:


private void Server()
{
    FileStream fs = null;
    BinaryWriter bw = null;
    int fileSize = 0;
    int bytesReceived = 0;
    int bufferInt = Int32.Parse(textBoxBYTE2.Text);

    byte[] bufferByte = new byte[bufferInt];
    byte[] byteFileName = Encoding.UTF8.GetBytes("empty");
    byte[] byteFileSize = Encoding.UTF8.GetBytes("empty");

    string fileName = "";

    int bytesTmp = 0;

    try
    {   
        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), int.Parse(textBoxPORT1.Text));
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
        socket.Bind(localEndPoint);
        socket.Listen(10);
        Socket listener = socket.Accept();
        //[1] Принимаем имя
        bytesTmp = listener.Receive(byteFileName);
        fileName = Encoding.UTF8.GetString(byteFileName, 0, bytesTmp);
        //[2] Принимаем размер
        listener.Receive(byteFileSize);

        fileSize = Convert.ToInt32(Encoding.UTF8.GetString(byteFileSize)); //Exception Thrown Here

        fs = new FileStream(Path.Combine(textBoxPATH.Text, fileName), FileMode.CreateNew, FileAccess.Write);
        bw = new BinaryWriter(fs);
        //<!--
        while (bytesReceived < fileSize)
        {
            if ((fileSize - bytesReceived) < bufferInt)
            {
                int bytes = (fileSize - bytesReceived);
                byte[] buf = new byte[bytes];
                bytes = listener.Receive(buf);
                bw.Write(buf, 0, bytes);
                bytesReceived = bytesReceived + bytes;
            } else {
                int bytes = listener.Receive(bufferByte);
                bw.Write(bufferByte, 0, bytes);
                bytesReceived = bytesReceived + bytes;
            }
        }
        //-->
        //Закрытие
        bw.Close();
        socket.Close();
        listener.Close();
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show("" + e, "Сервер сообщает");
    }
}
private void Client()
{
    FileStream fs = null;
    BinaryReader br = null;
    int bytesSent = 0;
    int fileSize = 0;            
    int bufferInt = Int32.Parse(textBoxBYTE2.Text);
    byte[] bufferByte = new byte[bufferInt];
    byte[] byteFileName = new byte[512];
    byte[] byteFileSize = new byte[512];
    string fileName = "";

    try
    {
        Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sender.Connect(textBoxADDRESS2.Text, int.Parse(textBoxPORT2.Text));
        FileInfo fileInfo = new FileInfo(textBoxFILE.Text);
        fileName = fileInfo.Name;
        byteFileName = Encoding.UTF8.GetBytes(fileName);
        //[1] Передаем имя
        sender.Send(byteFileName);
        fs = new FileStream(textBoxFILE.Text, FileMode.Open);
        br = new BinaryReader(fs);
        fileSize = (int) fs.Length;
        byteFileSize = Encoding.UTF8.GetBytes(Convert.ToString(fileSize));
        //[2] Передаем размер файла
        sender.Send(byteFileSize);
        //<!--
        while (bytesSent < fileSize)
        {
            if ((fileSize - bytesSent) < bufferInt)
            {
                int bytes = (fileSize - bytesSent);
                byte[] buf = new byte[bytes];
                br.Read(buf, 0, bytes);
                sender.Send(buf);
                bytesSent = bytesSent + bytes;
            } else {
               br.Read(bufferByte, 0, bufferInt);
               sender.Send(bufferByte);
               bytesSent = bytesSent + bufferInt;
            } 
        }
        //-->
        //Закрытие
        br.Close();
        sender.Close();
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show("" + e, "Клиент сообщает");
    }
}

2 个答案:

答案 0 :(得分:4)

在致电Socket.Receive时,您忽略实际读取的字节数。

这引发了两个问题:

  • 你怎么知道什么时候读够了?
  • 您目前假设字节数组的所有包含有用数据,而其中一些可能是“旧”数据。

答案 1 :(得分:2)

在我看来,你应该打电话

fileSize = byteFileSize.Length;

由于byteFileSizebyte[]