Disconnecting from the server after sending file - c#

I'm trying to send file from TCP client to listener. Its all working but after the file is sent, the client is disconnecting from the server. Here is the code I'm currently using for the client:
public static void SendFile(FileInfo file)
{
try
{
long size = file.Length;
using (NetworkStream ns = client.GetStream())
{
using (FileStream Fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
{
int num;
byte[] buffer = new byte[Fs.Length];
while ((num = Fs.Read(buffer, 0, buffer.Length)) != 0)
{
ns.Write(buffer, 0, num);
}
Fs.Close();
ns.Close();
}
}
FileInfo p_c = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\destfile.bin");
p_c.Delete();
} catch(Exception ex)
{
}
}
and for the server:
using (NetworkStream ns = new NetworkStream(current))
{
using (FileStream Fs = new FileStream(full_path, FileMode.OpenOrCreate, FileAccess.Write))
{
while ((RecBytes = ns.Read(RecData, 0, RecData.Length)) > 0)
{
Fs.Write(RecData, 0, RecBytes);
totalrecbytes += RecBytes;
}
{
Fs.Close();
ns.Close();
Console.WriteLine("File received. Path: {0}", full_path);
}
}

Calling NetworkStream.Close() or NetworkStream.Dispose (at the end of the using clause) will terminate the connection.
If you want to keep the socket open, use the NetworkStream(Socket, bool) constructor and and pass false as the second parameter.

Related

XML is saving in multiple files

I developed a listener that receives data every 60 seconds, It is receiving data as XML Data Stream, which is complete XML. In most cases it is working fine, saving complete XML into a single file of 4 kb. However, Sometimes it is saving one complete XML into 2 files. I am not getting why this is happening.
My code is below. Kindly help.
public static void GetXMLStream()
{
TcpListener server = null;
try
{
Int32 port = Int32.Parse(GetAppConfigValues.GetAppConfigValue("Port"));
IPAddress localAddr = IPAddress.Parse(GetAppConfigValues.GetAppConfigValue("IPAddress"));
server = new TcpListener(localAddr, port);
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
WriteToFile(data);
}
}
}
catch (SocketException e)
{}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
public static void WriteToFile(string sMessage)
{
try
{
string fileName = "NiproMI" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".xml";
DirectoryInfo logdirFile = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["XmlFilePath"].ToString());
string filePath = logdirFile.FullName;
if (File.Exists(Path.Combine(filePath, fileName)))
{
StreamWriter sw = null;
FileStream fs = File.Open(Path.Combine(filePath, fileName), FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
else
{
StreamWriter sw = null;
FileStream fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
}
catch (Exception e)
{
NiproEventsLog.WriteLog(e.ToString());
}
}
You should lift the file name out from WriteToFile, because one call of GetXMLStream should have only 1 file name.
Something like this:
public static void GetXMLStream()
{
string fileName = "NiproMI" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".xml";
TcpListener server = null;
try
{
Int32 port = Int32.Parse(GetAppConfigValues.GetAppConfigValue("Port"));
IPAddress localAddr = IPAddress.Parse(GetAppConfigValues.GetAppConfigValue("IPAddress"));
server = new TcpListener(localAddr, port);
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
WriteToFile(data, fileName);
}
}
}
catch (SocketException e)
{
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
public static void WriteToFile(string sMessage, string fileName)
{
try {
DirectoryInfo logdirFile = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["XmlFilePath"].ToString());
string filePath = logdirFile.FullName;
if (File.Exists(Path.Combine(filePath, fileName)))
{
StreamWriter sw = null;
FileStream fs = File.Open(Path.Combine(filePath, fileName), FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
else
{
StreamWriter sw = null;
FileStream fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
}
catch (Exception e)
{
NiproEventsLog.WriteLog( e.ToString());
}
}

How to send large file ( 500MB - 1GB ) over TCP/IP in c#?

I am using below code to send sample data into stream of bytes and is working perfectly fine.
CODE Used
CLIENT
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient("localHost", 5200);
NetworkStream networkStream = tcpClient.GetStream();
BufferedStream bs = new BufferedStream(networkStream);
//Send data to listener
byte[] dataToSend = new byte[100];
new Random().NextBytes(dataToSend);
for (int i = 0; i < 100; i++)
networkStream.Write(dataToSend, 0, dataToSend.Length);
//when the network stream is closed, it also shuts down the connection
networkStream.Close();
Console.WriteLine("Done");
Console.ReadLine();
}
}
SERVER
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener tcpListener = new TcpListener(ip, 5200);
tcpListener.Start();
Console.WriteLine("Waiting for a client to connect...");
//blocks until a client connects
Socket socketForClient = tcpListener.AcceptSocket();
Console.WriteLine("Client connected");
//Read data sent from client
NetworkStream networkStream = new NetworkStream(socketForClient);
int bytesReceived, totalReceived = 0;
byte[] receivedData = new byte[1000];
do
{
bytesReceived = networkStream.Read
(receivedData, 0, receivedData.Length);
totalReceived += bytesReceived;
}
while (bytesReceived != 0);
Console.WriteLine("Total bytes read: " + totalReceived.ToString());
socketForClient.Close();
Console.WriteLine("Client disconnected...");
Console.ReadLine();
}
}
I don't know how to send the data from a file which can be of very large size in the same way.
I tried the below code but it is not working if size of file is 30MB or more.
public void SendTCP(string filePath, string IPA, Int32 PortN)
{
byte[] SendingBuffer = null;
TcpClient client = null;
lblStatus.Text = "";
NetworkStream netstream = null;
try
{
client = new TcpClient(IPA, PortN);
lblStatus.Text = "Connected to the Server...\n";
netstream = client.GetStream();
FileStream Fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize)));
progressBar1.Maximum = NoOfPackets;
int TotalLength = (int)Fs.Length, CurrentPacketLength, counter = 0;
for (int i = 0; i < NoOfPackets; i++)
{
if (TotalLength > BufferSize)
{
CurrentPacketLength = BufferSize;
TotalLength = TotalLength - CurrentPacketLength;
}
else
CurrentPacketLength = TotalLength;
SendingBuffer = new byte[CurrentPacketLength];
Fs.Read(SendingBuffer, 0, CurrentPacketLength);
netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
if (progressBar1.Value >= progressBar1.Maximum)
progressBar1.Value = progressBar1.Minimum;
progressBar1.PerformStep();
}
lblStatus.Text = lblStatus.Text + "Sent " + Fs.Length.ToString() + " bytes to the server";
Fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
netstream.Close();
client.Close();
}
}
It should be as easy as this:
// Pass a file and send it through a socket.
static async Task SendFile(FileInfo file, Socket socket)
{
using (var networkStream = new BufferedStream(new NetworkStream(socket, false)))
using (var fileStream = file.OpenRead())
{
await fileStream.CopyToAsync(networkStream);
await networkStream.FlushAsync();
}
}
// Pass a socket and read the content to copy it to a file.
static async Task ReceiveFile(Socket socket, FileInfo file)
{
using (var fileStream = file.OpenWrite())
using (var networkStream = new NetworkStream(socket, false))
{
await networkStream.CopyToAsync(fileStream);
}
}
If you need to report the progress, you can use buffers and report the amount of bytes copied over:
static async Task SendFile(FileInfo file, Socket socket)
{
var readed = -1;
var buffer = new Byte[4096];
using (var networkStream = new BufferedStream(new NetworkStream(socket, false)))
using (var fileStream = file.OpenRead())
{
while(readed != 0)
{
readed = fileStream.Read(buffer, 0, buffer.Length);
networkStream.Write(buffer, 0, readed);
Console.WriteLine("Copied " + readed);
}
await networkStream.FlushAsync();
}
}
static async Task ReceiveFile(Socket socket, FileInfo file)
{
var readed = -1;
var buffer = new Byte[4096];
using (var fileStream = file.OpenWrite())
using (var networkStream = new NetworkStream(socket, false))
{
while (readed != 0)
{
readed = networkStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, readed);
Console.WriteLine("Copied " + readed);
}
}
}

About Parallel.For and Lock

if (File.Exists(copypath)) File.Delete(copypath);
using (FileStream inputstream = new FileStream(plainpath, FileMode.Open))
{
using (FileStream outputstream = new FileStream(copypath, FileMode.CreateNew))
{
Object lock1 = new Object();
long inputlength = inputstream.Length;
Parallel.For((long)0, (long)(inputlength / bufferSize) + ((inputlength%bufferSize)>0?1:0), (i) =>
{
byte[] parallelbuff = new byte[bufferSize];
long cursor = -1;
lock (lock1)
{
cursor = (long)(i * bufferSize);
inputstream.Seek(cursor, SeekOrigin.Begin);
read = inputstream.Read(parallelbuff, 0, buff.Length);
}
if (read > 0)
{
lock (lock1)
{
outputstream.Seek(cursor, SeekOrigin.Begin);
outputstream.Write(parallelbuff, 0, read);
}
}
});
}
}
This is a piece of my .NET 4.5 C# code for copying file from 'plainpath' to 'copypath'.
But it doesn't copy some parts of the original file.
Can you tell me about my mistake in it?

FileStream throws File not found

The program is meant to set the file path and the idea is that when the data is set, it should use this function:
public void SendFile(String fileName, long fileSize, NetworkStream io)
{
SendFileNameToServer();
SendFileSizeToServer();
byte[] fileData;
try
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException("File does not exist!");
}
FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(openFileStream);
Int32 remainingSize = Convert.ToInt32(_fileSize);
do
{
fileData = bReader.ReadBytes(BUFSIZE);
io.Write(fileData, 0, BUFSIZE);
remainingSize -= BUFSIZE;
} while (remainingSize > BUFSIZE);
do
{
fileData = bReader.ReadBytes(remainingSize);
io.Write(fileData, 0, remainingSize);
remainingSize -= remainingSize;
} while (remainingSize > 0);
bReader.Close();
openFileStream.Flush();
openFileStream.Close();
io.Flush();
io.Close();
}
catch (Exception)
{
throw new Exception();
}
}
to send the file given in the file path to a server-side program which receives the file data.
The problem is that when it gets to the line FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); it tells me that the file is not found. The exception it gives is Exception:Thrown: "The process cannot access the file 'D:\StepMania\Songs\Fragma\You Are Alive\Green.avi' because it is being used by another process." (System.IO.IOException)
A System.IO.IOException was thrown: "The process cannot access the file 'D:\*FilePath*\Green.avi' because it is being used by another process."
Time: 04-05-2013 21:11:39
Thread:Main Thread[5532] but I can't think of any process that would use this file when StepMania is not running.
I know that the file is there because I check the file path and it's there as it should be. It works just fine if the file is in the exact same folder as the program but other than that, I can't seem to find the solution to this problem. Does anyone have any ideas as to what could be wrong?
If you need any more of my code, please tell me.
Edit:
My server uses this code to receive the file:
public void ReceiveFile(String fileName, NetworkStream io)
{
// TO DO Din egen kode
byte[] fileData = new byte[BUFSIZE];
FileStream writeFileStream = new FileStream(fileName, FileMode.Create);
BinaryWriter bWrite = new BinaryWriter(writeFileStream);
int bytesRead = 0;
long remainingSize = Convert.ToInt32(_fileSize);
do
{
Console.WriteLine("Remaining number of bytes: {0}", remainingSize);
bytesRead = io.Read(fileData, 0, BUFSIZE); // Read max 1000 bytes from server via socket (actual value is placed in "bytesRead"
bWrite.Write(fileData, 0, bytesRead); // write the received bytes into file. the number of received bytes is placed in "bytesRead"
remainingSize -= bytesRead;
}
while (remainingSize > 0);
writeFileStream.Flush();
writeFileStream.Close();
bWrite.Close();
}
Ok, I found the problem: My server-side program interefered with my client-side program. Here's the fixed code for the SendFile code of my client program:
public void SendFile(String fileName, long fileSize, NetworkStream io)
{
SendFileNameToServer();
SendFileSizeToServer();
byte[] fileData;
try
{
FileStream openFileStream = File.OpenRead(fileName);
BinaryReader bReader = new BinaryReader(openFileStream);
Int32 remainingSize = Convert.ToInt32(_fileSize);
do
{
fileData = bReader.ReadBytes(BUFSIZE);
io.Write(fileData, 0, BUFSIZE);
remainingSize -= BUFSIZE;
} while (remainingSize > BUFSIZE);
do
{
fileData = bReader.ReadBytes(remainingSize);
io.Write(fileData, 0, remainingSize);
remainingSize -= remainingSize;
} while (remainingSize > 0);
openFileStream.Flush();
bReader.Close();
openFileStream.Close();
io.Flush();
io.Close();
}
catch (Exception)
{
throw new Exception();
}
}
Here's the ReceiveFile code for my server:
public void ReceiveFile(String fileName, NetworkStream io)
{
// TO DO Din egen kode
byte[] fileData = new byte[BUFSIZE];
FileStream writeFileStream = new FileStream(LIB.extractFileName(fileName), FileMode.Create);
BinaryWriter bWrite = new BinaryWriter(writeFileStream);
int bytesRead = 0;
long remainingSize = Convert.ToInt32(_fileSize);
do
{
Console.WriteLine("Remaining number of bytes: {0}", remainingSize);
bytesRead = io.Read(fileData, 0, BUFSIZE); // Read max 1000 bytes from server via socket (actual value is placed in "bytesRead"
bWrite.Write(fileData, 0, bytesRead); // write the received bytes into file. the number of received bytes is placed in "bytesRead"
remainingSize -= bytesRead;
}
while (remainingSize > 0);
writeFileStream.Flush();
writeFileStream.Close();
bWrite.Close();
}
Again, thank you everyone who answered my post!

Sending Binary File TcpClient - File Is Larger Than Source

To put my toe in the water of Network programming, I wrote a little Console App to send a png file to a server (another console app). The file being written by the server is slightly bigger than the source png file. And it will not open.
The code for the client app is:
private static void SendFile()
{
using (TcpClient tcpClient = new TcpClient("localhost", 6576))
{
using (NetworkStream networkStream = tcpClient.GetStream())
{
//FileStream fileStream = File.Open(#"E:\carry on baggage.PNG", FileMode.Open);
byte[] dataToSend = File.ReadAllBytes(#"E:\carry on baggage.PNG");
networkStream.Write(dataToSend, 0, dataToSend.Length);
networkStream.Flush();
}
}
}
The code for the Server app is :
private static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(Listen));
thread.Start();
Console.WriteLine("Listening...");
Console.ReadLine();
}
private static void Listen()
{
IPAddress localAddress = IPAddress.Parse("127.0.0.1");
int port = 6576;
TcpListener tcpListener = new TcpListener(localAddress, port);
tcpListener.Start();
using (TcpClient tcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream networkStream = tcpClient.GetStream())
{
using (Stream stream = new FileStream(#"D:\carry on baggage.PNG", FileMode.Create, FileAccess.ReadWrite))
{
// Buffer for reading data
Byte[] bytes = new Byte[1024];
var data = new List<byte>();
int length;
while ((length = networkStream.Read(bytes, 0, bytes.Length)) != 0)
{
var copy = new byte[length];
Array.Copy(bytes, 0, copy, 0, length);
data.AddRange(copy);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
stream.Position = 0;
binaryFormatter.Serialize(stream, data.ToArray());
}
}
}
tcpListener.Stop();
The size of the written file is 24,103Kb, whereas the source file is only 24,079Kb.
Is it apparent to anyone why this operation is failing?
Cheers
You are writing your output using a BinaryFormatter. I'm pretty sure that this will add some bytes at the start of the output to indicate the type that you're outputting (in this case System.Byte[]).
Just write the bytes out directly to the file without using the formatter:
using (Stream stream = new FileStream(#"D:\carry on baggage.PNG", FileMode.Create, FileAccess.ReadWrite))
{
// Buffer for reading data
Byte[] bytes = new Byte[1024];
int length;
while ((length = networkStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, length);
}
}

Categories

Resources