0

当 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, "Клиент сообщает");
    }
}
4

2 回答 2

4

在您对 的调用中Socket.Receive,您忽略了实际读取了多少字节。

这引发了两个问题:

  • 你怎么知道你什么时候读够了?
  • 您当前假设所有字节数组都包含有用的数据,而其中一些很可能是“旧”数据。
于 2012-05-22T12:57:01.703 回答
2

在我看来你应该打电话给

fileSize = byteFileSize.Length;

由于byteFileSize是一个byte[]

于 2012-05-22T12:55:37.950 回答