问题
Tell me how to fix this Exception please, when the transfer occurs between the PC.
Exception Thrown :
(System.FormatException)
A System.FormatException was thrown: "Input string had an incorrect format."
In Line :
fileSize = Convert.ToInt32(Encoding.UTF8.GetString(byteFileSize));
My whole Code :
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, "Клиент сообщает");
}
}
回答1:
In your call to Socket.Receive, you're ignoring how many bytes are actually read.
That raises two problems:
- 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.
回答2:
It seems to me that you should be calling
fileSize = byteFileSize.Length;
Since byteFileSize
is a byte[]
来源:https://stackoverflow.com/questions/10702431/how-to-solve-system-formatexception