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);
}
}
}
Related
Server(GUI).png
Server ( GUI )
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Server_ProfiChat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static readonly object _lock = new object();
static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
public static void handle_clients(object o)
{
int id = (int)o;
TcpClient client;
lock (_lock) client = list_clients[id];
while (true)
{
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int byte_count = stream.Read(buffer, 0, buffer.Length);
if (byte_count == 0)
{
break;
}
string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
broadcast(data);
Console.WriteLine(data);
//var chatline = txtChat.Text;
Form1 formObj = new Form1();
formObj.txtChat.Text += data;
}
lock (_lock) list_clients.Remove(id);
client.Client.Shutdown(SocketShutdown.Both);
client.Close();
}
public static void broadcast(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
lock (_lock)
{
foreach (TcpClient c in list_clients.Values)
{
NetworkStream stream = c.GetStream();
stream.Write(buffer, 0, buffer.Length);
}
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
int count = 1;
string serverIP = txtServerIP.Text;
int serverPort = Int32.Parse(txtServerPort.Text);
TcpListener ServerSocket = new TcpListener(IPAddress.Parse(serverIP), serverPort);
ServerSocket.Start();
while (true)
{
TcpClient client = ServerSocket.AcceptTcpClient();
lock (_lock) list_clients.Add(count, client);
Console.WriteLine("Someone connected!!");
Thread t = new Thread(handle_clients);
t.Start(count);
count++;
}
}
}
}
Server ( Console )
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace testServ
{
class Program
{
static readonly object _lock = new object();
static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
static void Main(string[] args)
{
int count = 1;
TcpListener ServerSocket = new TcpListener(IPAddress.Parse("192.168.0.169"), 123);
ServerSocket.Start();
while (true)
{
TcpClient client = ServerSocket.AcceptTcpClient();
lock (_lock) list_clients.Add(count, client);
Console.WriteLine("Someone connected!!");
Thread t = new Thread(handle_clients);
t.Start(count);
count++;
}
}
public static void handle_clients(object o)
{
int id = (int)o;
TcpClient client;
lock (_lock) client = list_clients[id];
while (true)
{
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int byte_count = stream.Read(buffer, 0, buffer.Length);
if (byte_count == 0)
{
break;
}
string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
broadcast(data);
Console.WriteLine(data);
}
lock (_lock) list_clients.Remove(id);
client.Client.Shutdown(SocketShutdown.Both);
client.Close();
}
public static void broadcast(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
lock (_lock)
{
foreach (TcpClient c in list_clients.Values)
{
NetworkStream stream = c.GetStream();
stream.Write(buffer, 0, buffer.Length);
}
}
}
}
}
Client (console)
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace test_Clie
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("192.168.0.166");
int port = 123;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("client connected!!");
NetworkStream ns = client.GetStream();
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
string s;
while (!string.IsNullOrEmpty((s = Console.ReadLine())))
{
byte[] buffer = Encoding.ASCII.GetBytes(s);
ns.Write(buffer, 0, buffer.Length);
}
client.Client.Shutdown(SocketShutdown.Send);
thread.Join();
ns.Close();
client.Close();
Console.WriteLine("disconnect from server!!");
Console.ReadKey();
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
Console.Write(Encoding.ASCII.GetString(receivedBytes, 0, byte_count));
Console.Write(ns);
}
}
}
}
Im a beginner but to me it seems to have to do with the threading?
Also because it freezes I cant see if the rest is working, any ideas on how to improve workflow, as in searching for the exact point the mistakes are? Im wasting alot of time searching anything because I dont know how to properly test, debug and pinpoint the mistakes I made.
Appreciate any help =))
Streams generally do exactly what you ask of them. If you ask for 10 bytes, then a Read will not return until there are 10 bytes to return. What stream.Read will not do, is return fewer bytes just because that is how many have been received.
This code will block until 1024 bytes have been read.
byte[] buffer = new byte[1024];
int byte_count = stream.Read(buffer, 0, buffer.Length);
if (byte_count == 0)
{
break;
}
Is that what you are expecting?
Generally, byte based protocols have a header, which contains how many bytes are in the payload.
Imagine the protocol below:
<stx><soh><len><eoh><payload><etx>
stx = Start of transmission
soh = Start of header
len = length of payload
eoh = End of header
etx = End of transmission
To read a complete packet you MUST read 4 bytes, and then you MUST read enough bytes to complete the payload, then the last byte should be ETX.
byte[5] header;
stream.Read(buffer, 0, 4);
int payloadLength = header[2];
byte[] payload = new byte[payloadLength];
stream.Read(payload, 0, payloadLength);
stream.Read() == ETX;
The TcpListener.AcceptTcpClient method is blocking. You could try using its asynchronous counterpart instead, combined with async/await:
private async void btnConnect_Click(object sender, EventArgs e)
{
/* ... */
TcpClient client = await ServerSocket.AcceptTcpClientAsync();
/* ... */
}
This question already has an answer here:
Why does my client socket not receive what my server socket sends?
(1 answer)
Closed 7 years ago.
I want to send big files with socket in c#. File can be transferred but when I want to open it, I see it is damaged. What's the problem?
I broke the file to 2KB in array in client code and send it. Then, in server code, I received it and put it in byte array and convert to file.
server code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace MyServer
{
class Program
{
static void Main(string[] args)
{
bool full = false;
byte[] data = new byte[2049];
byte[] bsize = new byte[2048];
List<byte> bytes = new List<byte>();
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(100);
Console.WriteLine("Wating For Client ...");
Socket client = newsock.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address,clientep.Port);
Console.Write("Enter the name of file: ");
string Address = Console.ReadLine();
client.Receive(bsize);
int size = BitConverter.ToInt32(bsize, 0);
byte[] bytes2 = new byte[size];
int k = 0;
while (true)
{
client.Receive(data);
for(int i = k,j=0; i < k + 2048 && j<2048; i++,j++)
{
if (i == size)
{
full = true;
break;
}
bytes2[i] = data[j];
//bytes.Insert(i,data[j]);
}
k += 2048;
if (full == true)
break;
/*if (bytes.Count >= size)
break;*/
}
File.WriteAllBytes(#"G:\"+Address,bytes2 /*bytes.ToArray()*/);
Console.WriteLine("Disconnected from Client {0}",clientep.Address);
client.Close();
newsock.Close();
Console.ReadKey();
}
}
}
client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace MyClient
{
class Program
{
static void Main(string[] args)
{
int reza = 0;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
}
catch (SocketException e)
{
Console.WriteLine("Unable to connect to Server");
Console.WriteLine(e.ToString());
Console.ReadKey();
return;
}
Console.Write("Enter Address Of File:");
string Address = Console.ReadLine();
FileStream File = new FileStream(Address, FileMode.Open, FileAccess.Read);
byte[] bytes = System.IO.File.ReadAllBytes(#Address);
File.Close();
byte[] data = new byte[2048];
byte[] Size = BitConverter.GetBytes(bytes.Length);
server.Send(Size, Size.Length, SocketFlags.None);
for (int i = 0; i <= bytes.Length; i +=2048)
{
int k = i + 2048;
for(int j=i,d=0 ;j< k && j<bytes.Length; j++,d++)
{
data[d] = bytes[j];
reza++;
}
if (reza == 2048)
{
server.Send(data, data.Length, SocketFlags.None);
Console.Write("*");
reza = 0;
}
else
{
server.Send(data, reza, SocketFlags.None);
Console.WriteLine("*");
}
}
Console.WriteLine("Disconnecting from server ...");
server.Shutdown(SocketShutdown.Both);
server.Close();
Console.ReadKey();
}
}
}
The problem is that you ignore the return value from Receive(). There is no guarantee what-so-ever that your buffer will be completely filled.
The return value tells you how much data that the socket actually read from the network. Adjust your receiving code so it's taken into account. Same goes for the send. If the internal socket buffer gets full it will report less bytes sent than you have requested to send.
Other than that, I suggest you start to give your variables meaningful names. Your code is not very readable as is.
Here is a better receive routine:
var fileBuffer = new byte[size];
var receiveBuffer = new byte[2048];
var bytesLeftToReceive = size;
var fileOffset = 0;
while (bytesLeftToReceive > 0)
{
//receive
var bytesRead = client.Receive(receiveBuffer);
if (bytesRead == 0)
throw new InvalidOperationException("Remote endpoint disconnected");
//if the socket is used for other things after the file transfer
//we need to make sure that we do not copy that data
//to the file
int bytesToCopy = Math.Min(bytesRead, bytesLeftToReceive);
// copy data from our socket buffer to the file buffer.
Buffer.BlockCopy(receiveBuffer, 0, bytesLeftToReceive, fileBuffer, fileOffset);
//move forward in the file buffer
fileOffset += bytesToCopy;
//update our tracker.
bytesLeftToReceive -= bytesToCopy;
}
However, since you are transferring large files, isn't it better to write it directly to a file stream?
var stream = File.Create(#"C:\path\to\file.dat");
var receiveBuffer = new byte[2048];
var bytesLeftToReceive = size;
while (bytesLeftToReceive > 0)
{
//receive
var bytesRead = client.Receive(receiveBuffer);
if (bytesRead == 0)
throw new InvalidOperationException("Remote endpoint disconnected");
//if the socket is used for other things after the file transfer
//we need to make sure that we do not copy that data
//to the file
int bytesToCopy = Math.Min(bytesRead, bytesLeftToReceive);
// write to file
stream.Write(receiveBuffer, 0, bytesToCopy);
//update our tracker.
bytesLeftToReceive -= bytesToCopy;
}
On the send side I would do something like this:
// read directly from the file to reduce memory usage.
var fileName = #"....";
using (var file = File.OpenRead(fileName))
{
var sendBuffer = new byte[2048];
var fileSize = BitConverter.GetBytes((int)file.Length);
server.Send(fileSize, fileSize.Length, SocketFlags.None);
var bytesLeftToTransmit = fileSize;
while (bytesLeftToTransmit > 0)
{
var dataToSend = file.Read(sendBuffer, 0, sendBuffer.Length);
bytesLeftToTransmit -= dataToSend;
//loop until the socket have sent everything in the buffer.
var offset=0;
while (dataToSend > 0)
{
var bytesSent = socket.Send(sendBuffer, offset, dataToSend);
dataToSend -= bytesSent;
offset += bytesSent;
}
}
}
Finally you have a Socket.SendFile which have been optimized to send files. You can invoke it at any time for an open socket.
I am receiving data from streaming of bytes.
Code of TCPListerner :
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)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 6054);
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;
string fileName = "testing.txt";
byte[] receivedData = new byte[10000];
do
{
bytesReceived = networkStream.Read
(receivedData, 0, receivedData.Length);
totalReceived += bytesReceived;
Console.WriteLine("Total bytes read: " + totalReceived.ToString());
if (!File.Exists(fileName))
{
using (File.Create(fileName)) { };
}
using (var stream = new FileStream(fileName, FileMode.Append))
{
stream.Write(receivedData, 0, bytesReceived);
}
}
while (bytesReceived != 0);
Console.WriteLine("Total bytes read: " + totalReceived.ToString());
socketForClient.Close();
Console.WriteLine("Client disconnected...");
Console.ReadLine();
}
}
I want to add functionality to process only that message which is coming from streaming starting with <Product> and want to check when the message ends. For end we can use </Products>.
How to perform this validation check in existing code, Any Idea ?
Receiving bytes here in this code(server)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Drawing;
namespace ByteLengthReading
{
class Program
{
static void Main(string[] args)
{
StartServer();
}
private static TcpListener _listener;
public static void StartServer()
{
IPAddress localIPAddress = IPAddress.Parse("119.43.29.182");
IPEndPoint ipLocal = new IPEndPoint(localIPAddress, 8001);
_listener = new TcpListener(ipLocal);
_listener.Start();
WaitForClientConnect();
}
private static void WaitForClientConnect()
{
object obj = new object();
_listener.BeginAcceptTcpClient(new System.AsyncCallback(OnClientConnect), obj);
Console.In.ReadLine();
}
private static void OnClientConnect(IAsyncResult asyn)
{
try
{
TcpClient clientSocket = default(TcpClient);
clientSocket = _listener.EndAcceptTcpClient(asyn);
HandleClientRequest clientReq = new HandleClientRequest(clientSocket);
clientReq.StartClient();
}
catch (Exception ex)
{
throw ex;
}
WaitForClientConnect();
}
public class HandleClientRequest
{
TcpClient _clientSocket;
NetworkStream _networkStream = null;
public HandleClientRequest(TcpClient clientConnected)
{
this._clientSocket = clientConnected;
}
public void StartClient()
{
_networkStream = _clientSocket.GetStream();
WaitForRequest();
}
public void WaitForRequest()
{
byte[] buffer = new byte[_clientSocket.ReceiveBufferSize];
_networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
}
private void ReadCallback(IAsyncResult result)
{
NetworkStream networkStream = _clientSocket.GetStream();
byte[] buffer = new byte[16384];
int read = -1;
int totRead = 0;
using (FileStream fileStream = new FileStream(#"C:\Foo" + Guid.NewGuid().ToString("N") + ".txt", FileMode.Create))
{
while ((read = networkStream.Read(buffer, 0, buffer.Length)) > 0)
{
totRead += read;
fileStream.Write(buffer, 0, read);
Console.WriteLine("Total Read" + totRead);
//fileStream.Write(buffer, 0, totRead);
//fileStream.Close();
}
fileStream.Close();
}
}
}
}
Sending bytes (Client), Sending bytes of length 4047810. But the abover server code is recieving only 4039618 bytes. Please help someone. Don't know y? At the time of reading last set of data it is coming out of the while loop. Please test this code and tell me where the problem lies.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Drawing;
using System.Threading;
namespace ByteLengthSending
{
class Program
{
static void Main(string[] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(IPAddress.Parse("119.43.29.182"), 8001);
//IPAddress ipAd = IPAddress.Parse("119.43.29.182");
//TcpClient client = new TcpClient(ipAd.ToString(), 8001);
//NetworkStream stream = client.GetStream();
int totread = 0;
byte[] longBuffer = new byte[3824726];
byte[] buffer = new byte[4096];
using (var fileStream = File.OpenRead("C:/Foo.txt"))
{
while (true)
{
int read = fileStream.Read(buffer, 0, buffer.Length);
totread += read;
if (read <= 0)
{
break;
}
for (int sendBytes = 0; sendBytes < read; sendBytes += client.Send(buffer, sendBytes, read - sendBytes, SocketFlags.None))
{
}
}
}
client.Close();
Console.WriteLine("Total Read" + totread);
Console.In.ReadLine();
}
}
}
Here is a sample which uses my library Griffin.Framework to transmit a file (Apache license).
All you need to do is to install the nuget package "griffin.framework" and then create a console application and replace Program class with the following:
class Program
{
static void Main(string[] args)
{
var server = new ChannelTcpListener();
server.MessageReceived = OnServerReceivedMessage;
server.Start(IPAddress.Any, 0);
var client = new ChannelTcpClient<object>(new MicroMessageEncoder(new DataContractMessageSerializer()),
new MicroMessageDecoder(new DataContractMessageSerializer()));
client.ConnectAsync(IPAddress.Loopback, server.LocalPort).Wait();
client.SendAsync(new FileStream("TextSample.txt", FileMode.Open)).Wait();
Console.ReadLine();
}
private static void OnServerReceivedMessage(ITcpChannel channel, object message)
{
var file = (Stream) message;
var reader = new StreamReader(file);
var fileContents = reader.ReadToEnd();
Console.WriteLine(fileContents);
}
}
The library can send/receive any type of stream of any size (as long as the size is known). The client will automatically create a MemoryStream or FileStream depending on the stream size.
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);
}
}