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れるバイト数を無視しています。

これにより、次の 2 つの問題が発生します。

  • 十分に読んだことをどうやって知るのですか?
  • 現在、すべてのバイト配列に有用なデータが含まれていると想定していますが、その一部は「古い」データである可能性があります。
于 2012-05-22T12:57:01.703 に答える
2

あなたが電話するべきだと私には思えます

fileSize = byteFileSize.Length;

なのでbyteFileSize_byte[]

于 2012-05-22T12:55:37.950 に答える