Reputation: 57
So I'm trying to make a program which sends a file that I want to print from my client in my main PC, to my 2nd PC that runs the server and that is connected to a printer. When I was testing my code I ran the client and server on my main pc and it worked fine. However, when I ran the client on my main PC and the server on my 2nd PC the file was corrupted and I'm not sure why.
Here's my Listener
(I removed the parts that I thought were unnecessary):
void Listener()
{
//All of these strings and bools are filled correctly I just removed it because its long
string file="";
string size = "";
bool print;
Socket server = myList.AcceptSocket();
var output = File.Create(file);
Console.WriteLine("Client connected. Starting to receive the file");
int k = 0;
int read = 0;
byte[] buffer = new byte[long.Parse(size)];
NetworkStream stm = new NetworkStream(server);
while ((k = stm.Read(buffer, 0, buffer.Length-read)) > 0)
{
read += k;
}
output.Write(buffer, 0, buffer.Length);
output.Close();
if (print) { PrintFile(file); }
server.Close();
Thread.CurrentThread.Abort();
}
Here is the client code (I removed the parts that I thought were unnecessary):
void startClient()
{
FileInfo f = new FileInfo(file);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(IPAddress.Parse("10.0.0.13"), 27015);
Console.WriteLine("Connected");
byte[] bytes = File.ReadAllBytes(file);
client.SendFile(file);
Console.WriteLine("File Sent");
client.Close();
}
Does anybody know how to solve this? Thanks in advance!
Upvotes: 1
Views: 94
Reputation: 151588
You don't apply the read
offset to the buffer, starting to write at index 0 at every NetworkStream.Read()
call. On local, or when testing with smaller files, this will work fine as everything will arrive in one Read()
. On a real network or when handling larger files, you'll find that you need multiple Read()
calls to read all data.
So change it to:
stm.Read(buffer, read, buffer.Length-read)
You also may want to reconsider reading the entire file in memory at once. You might want to write it to disk in the meantime, especially since allocating large arrays can cause an OutOfMemoryException
way earlier than you expect.
Also consider using existing networking protocols as opposed to rolling your own. Besides this very basic issue, you're prone to running into many other common socket pitfalls.
Upvotes: 3