如何解决System.FormatException? [英] How to Solve System.FormatException??

查看:97
本文介绍了如何解决System.FormatException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Tell me how to fix this Exception please, when the transfer occurs between the PC.

引发的异常:

(System.FormatException)

(System.FormatException)

抛出System.FormatException:输入字符串的格式不正确."

A System.FormatException was thrown: "Input string had an incorrect format."

在线:

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

推荐答案

在对

In your call to Socket.Receive, you're ignoring how many bytes are actually read.

这带来了两个问题:

  • 你怎么知道什么时候读够的?
  • 您当前假设字节数组的 all 包含有用的数据,而其中的一些可能是旧"数据.
  • How do you know when you've read enough?
  • You're currently assuming that all of the byte array contains useful data, whereas some of it may well be the "old" data.

这篇关于如何解决System.FormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆