The right way to use constructors when using Streamwriter - c#

My code as follows:
static void Main(string[] args)
{
TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 8081);
listener.Start();
Console.WriteLine("- Waiting for a connection..... .");
TcpClient client = listener.AcceptTcpClient();
while (true)
{
//Console.WriteLine("Waiting for a connection.");
//TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client accepted.");
NetworkStream stream = client.GetStream();
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
try
{
byte[] buffer = new byte[4096];
stream.Read(buffer, 0, buffer.Length);
int recv = 0;
foreach (byte b in buffer)
{
if (b != 0)
{
recv++;
}
}
string data = Encoding.UTF8.GetString(buffer, 0, recv);
//
var result = data.Split(new string[] { cr }, StringSplitOptions.None);
System.Threading.Thread.Sleep(1000);
sw.WriteLine("Hello Client, This is server");
sw.Flush();
sw.WriteLine works just fine here, but when I try to call it from another function
public static void Send()
{
sw.Writeline("Sending this from send function");
}
I get an error saying
(Error 9 The name 'sw' does not exist in the current context).
I couldn't figure out the right way to call it since sw StreamWriter uses stream() and stream uses (client) while the client uses (listener), this got beyond my very humble understanding of c#, I spent 2 days trying and researching before posting but with no luck.
Thanks

You are defining the StreamWriter in the scope of the while loop. Outside of it it does not exist. Try defining the StreamWriter as a parameter part of the class and only assign it later.
Change this:
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
To This:
static StreamReader sr = null;
static StreamWriter sw = null;
static void Main(string[] args)
{
...
while (true){
...
sr = new StreamReader(client.GetStream());
sw = new StreamWriter(client.GetStream());
}
}

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());
}
}

Correct use of StreamWriter

After several attempts I can't get StreamWriter to build / work corectly so I am doing something fundamentally wrong (C#, Visual Studio)
I have an exisitng TCP Client which connects and acts as a reader, this is working without fault -
private System.Net.Sockets.NetworkStream ns;
private System.IO.StreamReader sr;
private string strIP;
private string strPort;
public System.Net.Sockets.TcpClient tc;
public Socketclient(Reader.Search objSearch)
{
m_search = objSearch;
}
public void EthernetConnection()
{
try
{
bool flag = !System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + "\\IPAddress.txt");
if (!flag)
{
System.IO.TextReader textReader = System.IO.File.OpenText(System.Windows.Forms.Application.StartupPath + "\\IPAddress.txt");
string s = textReader.ReadLine();
textReader.Close();
char[] chArr = new char[] { ';' };
string[] sArr = s.Split(chArr);
strPort = sArr[0];
strIP = sArr[1];
flag = strIP == System.String.Empty || strPort == System.String.Empty;
if (!flag)
{
int i = System.Convert.ToInt16(strPort);
tc = new System.Net.Sockets.TcpClient(strIP, i);
flag = !tc.Connected;
if (!flag)
{
ns = tc.GetStream();
sr = new System.IO.StreamReader(ns);
m_search.threadClient = new System.Threading.Thread(new System.Threading.ThreadStart(ReceiveData));
m_search.threadClient.Priority = System.Threading.ThreadPriority.Lowest;
m_search.threadClient.Name = "Ethernet Thread";
}
I then want to add (in another .cs which is part of the same application) a StreamWriter thread to write back some characters to the same port and then close the StreamWriter thread (leaving the reader running) -
private System.IO.StreamWriter sw;
string line = "TH1/r/n";
using (StreamWriter sw = new StreamWriter(ns));
sw.WriteLine(line);
sw.Flush();
sw.Close();
Which, somehow (I think) needs to refer back to
ns = tc.GetStream();
Any thougts appreciated
Regards
Active
this sort of question has been asked many times already.
Receving and sending data in C#
brief cut and past from example above showing sending of data
using(TcpClient tcpClient = new TcpClient("ADDRESS"))
{
NetworkStream networkStream = tcpClient.GetStream();
using(StreamWriter streamWriter = new StreamWriter(networkStream))
{
streamWriter.WriteLine(data);
}
}
The issue was the using block around StreamWriter, removing it allowed the underlying stream to continue.

C# TCP Read All Data - Server Side Issues

I'm working on a simple TCP server application using C# v4.0 (.Net Framework v4):
I want to accomplish these two steps:
Client sends message1 to Server (client can be .net or java application)
Server sends back message2 to Client as a response to message1
I have a problem with my server, it is not able to read message1 correctly unless I use one of these inappropriate solutions:
1) Use a MemoryStream with a Buffer of only 1 byte (works but slow):
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
MemoryStream memoryStream = new MemoryStream();
int numberOfBytesRead = 0;
byte[] buffer = new byte[1]; // works but slow in case of big messages
do
{
numberOfBytesRead = networkStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, numberOfBytesRead);
} while (networkStream.DataAvailable);
if (memoryStream.Length > 0)
{
string message1 = new StreamReader(memoryStream).ReadToEnd();
if (message1 == "message1")
{
using (StreamWriter streamWriter = new StreamWriter(networkStream))
{
string message2 = "message2";
streamWriter.Write(message2);
streamWriter.Flush();
}
}
}
}
Example: if message1.Length == 12501 and I use a buffer of 1024 the NetworkStream.Read() loop reads only 2048 bytes of message1, I think NetworkStream.DataAvailable does not return the correct value!
2) Use a Thread.Sleep(1000) after reading from NetworkStream to Buffer (works but slow):
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
MemoryStream memoryStream = new MemoryStream();
int numberOfBytesRead = 0;
byte[] buffer = new byte[8192];
do
{
numberOfBytesRead = networkStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, numberOfBytesRead);
Thread.Sleep(1000); // works but receiving gets slow
} while (networkStream.DataAvailable);
if (memoryStream.Length > 0)
{
string message1 = new StreamReader(memoryStream).ReadToEnd();
if (message1 == "message1")
{
using (StreamWriter streamWriter = new StreamWriter(networkStream))
{
string message2 = "message2";
streamWriter.Write(message2);
streamWriter.Flush();
}
}
}
}
3) Use StreamReader.ReadToEnd() and close the client's socket after sending messages1 (works but server cannot response to client with message2):
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream, true);
string message1 = streamReader.ReadToEnd(); // blocks until client close its socket
if (message1 == "message1")
{
using (StreamWriter streamWriter = new StreamWriter(networkStream))
{
string message2 = "message2";
streamWriter.Write(message2); // if client close its sockets, the server cannot send this message
streamWriter.Flush();
}
}
}
4) Loop with StreamReader.ReadLine() and close the client's socket
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
StringBuilder stringBuilder = new StringBuilder();
while (!streamReader.EndOfStream)
{
stringBuilder.AppendLine(streamReader.ReadLine()); // blocks until client close its socket
}
string message1 = stringBuilder.ToString();
if (message1 == "message1")
{
using (StreamWriter streamWriter = new StreamWriter(networkStream))
{
string message2 = "message2";
streamWriter.Write(message2); // if client close its sockets, the server cannot send this message
streamWriter.Flush();
}
}
}
5) Prefix message1 with its length (works but requires the client to add extra bytes to the message and this will not work with existing java clients)
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
MemoryStream memoryStream = new MemoryStream();
byte[] bufferMessageLength = new byte[4]; // sizeof(int)
networkStream.Read(bufferMessageLength, 0, bufferMessageLength.Length);
int messageLength = BitConverter.ToInt32(bufferMessageLength, 4);
byte[] bufferMessage = new byte[messageLength];
networkStream.Read(bufferMessage, 0, bufferMessage.Length);
memoryStream.Write(buffer, 0, bufferMessage.Length);
if (memoryStream.Length > 0)
{
string message1 = new StreamReader(memoryStream).ReadToEnd();
if (message1 == "message1")
{
using (StreamWriter streamWriter = new StreamWriter(networkStream))
{
string message2 = "message2";
streamWriter.Write(message2);
streamWriter.Flush();
}
}
}
}
Regarding to these issues, what is the best method to read all data from the client without using the above mentioned solutions?
instead of using networkStream.DataAvailable append the size of data at the start of your message. for example the length of your message is 12501 use first 4 bytes as message length.
First define a method to read data from buffer
public static void ReadStream(NetworkStream reader, byte[] data)
{
var offset = 0;
var remaining = data.Length;
while (remaining > 0)
{
var read = reader.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
}
and then read data from stream.
var bytesRead = 0;
var offset = 0;
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
var bufferMessageSize = new byte[4]; // int32
ReadStream(networkStream, bufferMessageSize);
var messageSize = BitConverter.ToInt32(bufferMessageSize, 4); // bytesToRead
var bufferMessage = new byte[messageSize];
ReadStream(networkStream, bufferMessage);
// Now Respond back Client here
// networkStream.Write();
If the communication is line oriented, then StreamReader.ReadLine() could be suitable.
ReadLine() Reads a line of characters from the current stream and returns the
data as a string.

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);
}
}
}

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