my program should be able to send and receive files but for some reason whenever i click on the send (button1) and receive (button2) buttons, it keeps hanging. Not sure if its something wrong my my codes? Also, I feel like my codes are pretty long as compared to other examples i found online but I'm not sure how to rectify.
Client codes
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
private void Form1_Load(object sender, EventArgs e)
{
try
{
socket.Connect(remoteEP);
textBox2.Text = "Connected to Server";
}
catch (Exception ex)
{
textBox2.Text = "Unable to connect to Server";
textBox2.Text = ex.Message;
}
}
public const string SEND = "[SEND]";
public const string RECEIVE = "[RECEIVE]";
public const string QUIT = "[QUIT]";
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
NetworkStream stream = new NetworkStream(socket);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
try
{
writer.WriteLine(RECEIVE);
writer.Flush();
writer.WriteLine(textBox1.Text);
writer.Flush();
Bitmap bmp = new Bitmap(#"C:\Users\Y400\Desktop\Lectures\Year 3\WAD\Week 11" + textBox1.Text);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
int sent;
sent = sendData(socket, bmpBytes);
textBox1.Text = "Transferring file complete\r\n";
textBox1.Text += bmpBytes.Length + " bytes sent to Server.";
}
catch (Exception ex)
{
textBox2.Text = ex.Message;
}
}
public static int sendData (Socket s, byte[] data)
{
int total = 0;
int size = data.Length;
int left = size;
int sent;
byte[] datasize = new byte[4];
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);
while(total<size)
{
sent = s.Send(data, total, left, SocketFlags.None);
total += sent;
left -= sent;
}
return total;
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox1.Clear();
byte[] data = new byte[1024];
string fileN = textBox2.Text.Trim();
NetworkStream ns = new NetworkStream(socket);
StreamReader reader = new StreamReader(ns);
StreamWriter writer = new StreamWriter(ns);
writer.WriteLine(SEND);
writer.Flush();
writer.WriteLine(fileN);
writer.Flush();
try
{
while (true)
{
data = receiveData(socket);
MemoryStream ms = new MemoryStream(data);
break;
}
textBox2.Text = ("Receiving file from server ...\r\n" + data.Length + " bytes copied");
}
catch (Exception ex)
{
textBox2.Text = ex.Message;
}
}
public static byte[] receiveData (Socket s)
{
int total = 0;
int recv;
byte[] datasize = new byte[4];
recv = s.Receive(datasize, 0, 4, 0);
int size = BitConverter.ToInt32(datasize, 0);
int dataleft = size;
byte[] data = new byte[size];
while (total < size)
{
recv = s.Receive(data, total, dataleft, 0);
if (recv == 0)
{
break;
}
total += recv;
dataleft -= recv;
}
return data;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox2.Text = "Connection closed";
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
Server codes
class Program
{
public const string SEND = "[SEND]";
public const string RECV = "[RECV]";
public const string QUIT = "[QUIT]";
static void Main(string[] args)
{
runServer();
}
static void runServer()
{
try
{
byte[] data = new byte[1024];
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9000);
server.Bind(localEP);
server.Listen(10);
Console.WriteLine("Waiting for Client ...");
Socket client = server.Accept();
Console.WriteLine("Client connected");
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
try
{
while(true)
{
string request = reader.ReadLine();
string filename = reader.ReadLine();
if (request == QUIT)
{
Console.WriteLine("Client disconnected");
break;
}
else if (request == SEND)
{
getFileFromClient(filename, client);
}
else if (request == RECV)
{
receiveFileFromClient(filename, client);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void getFileFromClient(string filename, Socket client)
{
try
{
FileStream output = File.OpenWrite(filename);
Console.WriteLine(filename + " created");
int count = 0;
while(true)
{
byte[] data = new byte[1024];
int size = client.Receive(data);
output.Write(data, 0, size);
count += size;
if(size<1024)
{
break;
}
}
output.Close();
Console.WriteLine(count + " bytes read from client");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void receiveFileFromClient(string filename, Socket client)
{
int count = 0;
FileStream input = File.OpenRead(filename);
Console.WriteLine("Reading " + filename);
while(true)
{
byte[] data = new byte[1024];
int bytesRead = input.Read(data, 0, 1024);
client.Send(data, bytesRead, SocketFlags.None);
count += bytesRead;
if(bytesRead < 1024)
{
break;
}
}
Console.WriteLine("Transferring file completed\r\n" + count + " bytes sent to Client");
input.Close();
}
}
Generally speaking
...try to approach the problem differently.
You can't just copy paste stuff from the Internet and hope for the best. You need to understand what you're doing thoroughly.
Regarding your exact problem
Take a look at the button2_Click method.
It contains a while loop which apparently never finishes.
while (true)
{
data = receiveData(socket);
MemoryStream ms = new MemoryStream(data);
break;
}
It does finish because of the break command. But this is all very hard to read.
When you copy paste code around and then apply quick fixes you end up with a pile of code which is very hard to debug.
It took me about 10 minutes to notice the fact that the client defines it's "message verbs" like so:
public const string SEND = "[SEND]";
public const string RECEIVE = "[RECEIVE]";
public const string QUIT = "[QUIT]";
while the server defines them like so:
public const string SEND = "[SEND]";
public const string RECV = "[RECV]";
public const string QUIT = "[QUIT]";
This is maybe not the only problem, but it is sufficient to create a deadlock,
because the server never executes the positive branch of this if statement:
else if (request == RECV)
{
receiveFileFromClient(filename, client);
}
so the client believes it is about to receive something, which proves to be false.
Also make sure you send the "SEND" and "RECEIVE" message verbs when you should and not mix them up.
Good luck!
PS: I would suggest you take a look at more simpler to use techniques for sending and receiving data, such as:
WCF
ASP.NET Web Services
Web API
Ignoring any logic errors that may occur in your programs, the way you are handling things in your client whenever it is doing an action it is doing it on the GUI thread. This will make your application seem like it is locking but instead it is executing your logic on the GUI thread.
The same problem is occurring on the server. It accepts a connection, and then goes on to receive the file. It will not be able to receive any other connection until it finished receiving the file.
The server is not without problems either because it is never checking if it receives 0 bytes from the socket. Which means that the client closed its end of the connection. You are simply assuming that if you receive less than 1024 you are receiving your last part of the file. This is simply not true for TCP. You only know you received the last part if you receive 0 bytes. TCP is a byte streaming protocol you cannot assume you will be receiving blocks of 1024 bytes. It is likely that this will be in fact the case, but you should not code it like that. Check for reception of 0 bytes. On the client you did check for 0 bytes, I am puzzled why you did not do the same on the server. The problematic part is this :
byte[] data = new byte[1024];
int size = client.Receive(data);
output.Write(data, 0, size);
count += size;
if(size<1024) //you can only break if the size is 0
{
break;
}
There are probably more bugs. As the other answer also indicated some other issues.
Related
Hi guys I have a big problem: I have createad a windows form that worked as a Server listening for TCP messages sended by some cameras connected to the same private network.
Each camera send a tcp message to the server when the camera detects something, the server has to work continuosly without freezing the gui interface and has to process every TCP message. The processing of this data includes the saving of an image shooted by the sender camera. The server saves these images into a folder and here there is my problem:
the server is not able to save correclty each image: it looks like that some bytes have been lost during the trasmission but I think that all the bytes have riched the server, something deeper has happend. Maybe it could be the way that I have programmed the async / await server?
I have a list of TcpListener because I have to use more server, one for each tab.
Here how the saved images look like
https://imgur.com/xtlgHPk
https://imgur.com/CcvWbDH
As you can see is not completely saved, except someone for some unkown reason
https://imgur.com/G25UPSS
public void TcpServer(int port)
{
IPAddress ipAddress = null;
string hostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
for (int i = 0; i < ipHostInfo.AddressList.Length; ++i)
{
if (ipHostInfo.AddressList[i].AddressFamily ==
AddressFamily.InterNetwork)
{
ipAddress = ipHostInfo.AddressList[i];
_listener.Add( new TcpListener(ipAddress, port));
ReceiveDataAsync();
break;
}
}
if (ipAddress == null)
throw new Exception("No IPv4 address for server");
}
private async void ReceiveDataAsync()
{
try
{
_listener[tbServer.SelectedIndex].Start();
while (true)
{
var tcpClient = await _listener[tbServer.SelectedIndex].AcceptTcpClientAsync();
ReadDataFromClientAsync(tcpClient);
}
}
catch (Exception e)
{
MessageBox.Show("Errore: ", e.Message.ToString());
}
}
private async Task ReadDataFromClientAsync(TcpClient client)
{
try
{
using (NetworkStream stream=client.GetStream())
{
int selectedTabIndex, indexOfPort;
string endPoint;
while (client.Connected)
{
int count = 0;
var countBytes = new byte[4];
for (int i = 0; i < 6; i++)
{
count = await stream.ReadAsync(countBytes, 0, countBytes.Length);
}
//The data dimension of the TCP message is into his header, 24th byte.
if (count == 0)
{
break;
}
byte[] bytes = new byte[BitConverter.ToUInt32(countBytes, 0)];
await stream.ReadAsync(bytes, 0, bytes.Length);
indexOfPort = Settings.getIndexOfPort(client.Client.LocalEndPoint.ToString());
endPoint = client.Client.RemoteEndPoint.ToString();
selectedTabIndex = Settings.getIndexOfPort(client.Client.LocalEndPoint.ToString());
updateGui("entry", indexOfPort,endPoint);
BufferData bufferService = new BufferData();
CameraMessage cameraMessage = await bufferService.writeBufferData(bytes.ToList());
if (cameraMessage == null)
return;
//doing some stuff
msgToShow = await bufferService.SaveImageFromCamera(cameraMessage);
//doing other stuff
}
client.Close();
closeCommunication(selectedTabIndex,indexOfPort,endPoint);
}
}
}
catch (IOException e)
{
updateGui("stop");
}
}
The BufferData class:
public class BufferData
{
public async Task<CameraMessage> writeBufferData(List<byte> bytesList)
{
CameraMessage cameraMessage = new CameraMessage();
try
{
int num = 0;
int startSubData = 0;
for (int startData = 0; startData < bytesList.Count-8; startData = startSubData + num)
{
int codeDataMessage = BitConverter.ToInt32(bytesList.GetRange(startData, 4).ToArray(), 0);
int indexDataSize = startData + 4;
int SizeDataMessage = BitConverter.ToInt32(bytesList.GetRange(indexDataSize, 4).ToArray(), 0);
startSubData = indexDataSize + 4;
byte[] array = bytesList.GetRange(startSubData, SizeDataMessage).ToArray();
num = 0;
switch (codeDataMessage)
{
case 14020:
cameraMessage.image = await this.Base64ToImage(array);
num = this.OffSetStringType(SizeDataMessage);
break;
}
}
return cameraMessage;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return null;
}
}
public async Task<string> SaveImageFromCamera( CameraMessage cameraMessage)
{
string path = Settings.pathFolder + cameraMessage.ld_I_SN + #"\Images\";
string res;
if (!Directory.Exists(Settings.pathFolder+cameraMessage.ld_I_SN))
{
Directory.CreateDirectory(Settings.pathFolder + cameraMessage.ld_I_SN + #"\Images");
}
try
{
await Task.Run(() => { cameraMessage.image.Save(path + cameraMessage.ld_I_FILENAME, ImageFormat.Jpeg); });
res = "#3 - OK - IMMAGINE SALVATA CON SUCCESSO";
cameraMessage.image.Dispose();
return res;
}
catch (Exception ex)
{
res = "#3 - ERR - IMMAGINE NON SALVATA";
return res;
}
}
public async static Task<Image> Base64ToImage(byte[] imageBytes)
{
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
return System.Drawing.Image.FromStream(ms, true);
}
}
Your core problem is that you're assuming a socket read will read all the bytes you ask it to. That's not how socket reads work.
When you call await stream.ReadAsync(bytes, 0, bytes.Length), the stream will read any number of bytes from 1 to bytes.Length. It will return the number of bytes actually read. You need to take this into consideration and read again as long as your buffer hasn't been read completely.
Note that this has to be done both for the header bytes and the payload bytes.
I will get 3mins of delay at the start of the below program.but where as in hyper terminal it shows every data for every 2 secs.
Her is my below code.please let me know where i went wrong .How i can rectify this delay. any suggestion?
private void StartReceiving()
{
// coding part of receiving changed.
try
{
string IPStr = textBox1_IPaddress.Text.Trim();
string portStr = textBox2_Port.Text.Trim();
int port = Convert.ToInt32(portStr);
int bytesRead = 0;
byte[] buffer = new byte[9];
//------------------------------------------------------------------
IPAddress ipAddress = System.Net.IPAddress.Parse(IPStr);
//create server's tcp listener for incoming connection
try
{
textBox3.Visible = true;
client = new TcpClient();
client.Connect(IPStr, port);
ns = client.GetStream();
while (true)
{
if (ns.DataAvailable)
{
byte[] data = ReadNumberBytes(ns, 9);
ASCIIEncoding encoder = new ASCIIEncoding();
msg = encoder.GetString(data);
textBox3.AppendText(msg);
textBox3.AppendText("\n");
GetData(msg);
}
else
{
Thread.Sleep(4000);
byte[] data = ReadNumberBytes(ns, 9);
ASCIIEncoding encoder = new ASCIIEncoding();
msg = encoder.GetString(data);
GetData(msg);
}
}
client.Close();
}
catch (SocketException se)
{
//message box;
}
}
}
public static byte[] ReadNumberBytes(NetworkStream stream, int n)
{
byte[] buffer = new byte[n];
int bytesRead = 0;
int chunk;
while (bytesRead < n)
{
chunk = stream.Read(buffer, (int)bytesRead, buffer.Length - int)bytesRead);
if (chunk == 0)
{
// error out.
throw new Exception("Unexpected disconnect");
}
bytesRead += chunk;
}
return buffer;
}
I need to send a few large files to a computer over the internet. As a result I opened the ports that I plan to use on the router and forward the correspoinding ip addresses. anyways let me show you the classes that I have been working on in order to achieve this. This classes work great with small files but sometimes they fail with large files.
here is the code for the server: (it is a console application)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication20
{
//server
class Program
{
static Server s;
public static void mm()
{
s = new Server("192.168.0.196");
s.startServer();
Console.Read();
}
static void Main(string[] args)
{
// Thread t = new Thread(new ThreadStart(mm));
// t.Start();
mm();
Console.Read();
s.disconnect();
}
}
class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
class Server: MyTCP
{
private System.IO.FileStream _FileStream;
private static TcpListener server;
private static bool disconect;
/// <summary>
/// Constructor
/// </summary>
/// <param name="localAddr">The ip address of the server</param>
/// <param name="port">on what port the server going to be listening to?</param>
/// <param name="autoStartServer">start listening for connections now? you may call the startserver() method latter...</param>
public Server(string localAddr, Int32 port = 13000, bool autoStartServer = false)
{
this.port = port;
this.serverIP = localAddr;
if (autoStartServer)
start();
}
/// <summary>
/// Start listening for connections
/// </summary>
public void startServer()
{
start();
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
server.Stop();
disconect = true;
}
void start()
{
server = null;
try
{
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Parse(serverIP), port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[MaxChunkSize];
String data = null;
// Enter the listening loop.
while (disconect==false)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
stream = client.GetStream();
int i;
try
{
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
if (data.ToUpper().Contains("<sendFile>".ToUpper()))
{
receiveFile(bytes);
}
continue;
}
}
catch { }
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
void receiveFile(Byte[] bytes)
{
// send 1
sendData(stream, textToBytes("<1>"));
// receive 2
int length = stream.Read(bytes, 0, bytes.Length);
byte[] tempA = new byte[length];
for (int k = 0; k < length; k++)
tempA[k] = bytes[k];
Stream ms = new MemoryStream(tempA);
FileProperties p = new FileProperties();
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
try
{
p = (FileProperties)x.Deserialize(ms);
if (Directory.Exists(p.DestPath))
{
//send 3
sendData(stream, textToBytes("<3>"));
}
else
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
}
catch
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
int i;
string temp = Path.Combine(new string[]{ p.DestPath, p.FileName + ".temp"});
_FileStream = new System.IO.FileStream(temp, System.IO.FileMode.Create, System.IO.FileAccess.Write);
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
if (i == 11 & System.Text.Encoding.ASCII.GetString(bytes, 0, i).ToUpper().Equals("</sendFile>".ToUpper()))
{
_FileStream.Close();
Console.WriteLine("D!");
File.SetAttributes(temp, p.fileAttributes);
File.SetAccessControl(temp, p.FileSecurity);
File.SetCreationTime(temp, p.creationTime);
File.SetLastAccessTime(temp, p.lastAccessTime);
File.SetLastWriteTime(temp, p.lastWriteTime);
if(File.Exists(temp.Substring(0, temp.Length - 4)))
File.Delete(temp.Substring(0, temp.Length - 4));
File.Move(temp, temp.Substring(0, temp.Length - 4));
//sendData(stream, textToBytes("<done>"));
Console.WriteLine("Done!");
return;
}
_FileStream.Write(bytes, 0, i);
}
return;
}
}
}
and the code for my client is:
using System;
using System.Net.Sockets;
using System.Windows;
using System.IO;
namespace WpfApplication23sdfd
{
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
abstract class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
//client
class Client: MyTCP
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="serverIP">the ip address of the server</param>
/// <param name="port">through what port is the connection going to be established</param>
public Client(string serverIP, Int32 port = 13000, bool autoConnect = false)
{
this.port = port;
this.serverIP = serverIP;
if (autoConnect)
connect();
}
public bool connect()
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes("connect");
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
try
{
client = new TcpClient(serverIP, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
stream = client.GetStream();
return true;
}
catch
{
return false;
}
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
}
static void ConnectOld(String server, Byte[] data)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
public void sendFile(string file, string destPath = "c:\\")
{
//let server know what you are going to be doing...
sendData(stream, textToBytes("<sendFile>"));
FileProperties p = new FileProperties {
creationTime = File.GetCreationTime(file),
fileAttributes = File.GetAttributes(file),
FileSecurity = File.GetAccessControl(file),
lastAccessTime = File.GetLastAccessTime(file),
lastWriteTime = File.GetLastWriteTime(file),
DestPath = destPath,
FileName = Path.GetFileName(file)
};
// receive 1
if (!receiveData(stream).ToUpper().Contains("<1>".ToUpper()))
{
MessageBox.Show("Error comunicating with server");
return;
}
// send object p to server
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(stream, p); // send 2
//recieve 3
if (!receiveData(stream).ToUpper().Contains("<3>".ToUpper()))
{
MessageBox.Show("Error incorrect parameters sent to server");
return;
}
System.IO.FileStream streamFile = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
while (true)
{
byte[] chunk = new byte[MaxChunkSize];
int index = 0;
// There are various different ways of structuring this bit of code.
// Fundamentally we're trying to keep reading in to our chunk until
// either we reach the end of the stream, or we've read everything we need.
while (index < chunk.Length)
{
int bytesRead = streamFile.Read(chunk, index, chunk.Length - index);
if (bytesRead == 0)
{
break;
}
if (bytesRead < MaxChunkSize)
{
byte[] temp = new byte[bytesRead];
for (var i = 0; i < bytesRead; i++)
temp[i] = chunk[i];
chunk = temp;
}
index += bytesRead;
}
if (index != 0) // Our previous chunk may have been the last one
{
sendData(stream,chunk); // index is the number of bytes in the chunk
}
if (index != chunk.Length) // We didn't read a full chunk: we're done
{
sendData(stream, textToBytes("</sendFile>".ToUpper()));
//receiveData(stream);//wait recall missing to check results
return;
}
}
}
}
}
the way I instantiate the client class is by providing the IP address of the server as:
Client c = new Client("192.168.0.196");
c.sendFile(#"A:\Users\Tono\Desktop\a.mp4");
the server has to be running first before executing that code.
I don't know why is so complicated to send a file over the internet using sockets. I don't konw WCF that's why I been loosing a lot of time creating this classes. Maybe there are already some built in classes that will enable me to send files over the internet to a different computer. I know just the basics of networking thereofore it will be nice if I could do it with a simple class. I don't undersatnd why my classes do not work all the time? if I increase the buffer size will my classes be more efficient? do I have to wait or pause my program a little bit before sending more bytes? It will be nice if someone can tell me what is wrong with this classes. They work nice with small files but with large files sometimes it does not work...
Here is my code to send a large file.
Some tips:
Check your buffer size. If it's too big it will fail.
Socket flags. The partial flag works the best.
Socket timeout is needed due to the transfer time.
Client :
string IPAddress = "";
int Port = 500;
string Filename = #"C:\Users\Ben\Desktop\TT.zip";
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
FileStream fs = new FileStream(Filename, FileMode.Open);
bool read = true;
int bufferCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / (double)bufferSize));
TcpClient tcpClient = new TcpClient(IPAddress, Port);
tcpClient.SendTimeout = 600000;
tcpClient.ReceiveTimeout = 600000;
string headerStr = "Content-length:" + fs.Length.ToString() + "\r\nFilename:" + #"C:\Users\Administrator\Desktop\" + "test.zip\r\n";
header = new byte[bufferSize];
Array.Copy(Encoding.ASCII.GetBytes(headerStr), header, Encoding.ASCII.GetBytes(headerStr).Length);
tcpClient.Client.Send(header);
for (int i = 0; i < bufferCount; i++)
{
buffer = new byte[bufferSize];
int size = fs.Read(buffer, 0, bufferSize);
tcpClient.Client.Send(buffer,size,SocketFlags.Partial);
}
tcpClient.Client.Close();
fs.Close();
Server :
int Port = 500;
TcpListener listener = new TcpListener(IPAddress.Any, Port);
listener.Start();
Socket socket = listener.AcceptSocket();
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
string headerStr = "";
string filename = "";
int filesize = 0;
header = new byte[bufferSize];
socket.Receive(header);
headerStr = Encoding.ASCII.GetString(header);
string[] splitted = headerStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string s in splitted)
{
if (s.Contains(":"))
{
headers.Add(s.Substring(0,s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
}
}
//Get filesize from header
filesize = Convert.ToInt32(headers["Content-length"]);
//Get filename from header
filename = headers["Filename"];
int bufferCount = Convert.ToInt32(Math.Ceiling((double)filesize / (double)bufferSize));
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
while(filesize > 0)
{
buffer = new byte[bufferSize];
int size = socket.Receive(buffer,SocketFlags.Partial);
fs.Write(buffer,0,size);
filesize -= size;
}
fs.Close();
Hope this will help someone.
There are a few issues that I can see immediately. The one that may be causing your program to only work some of the time is the fact that sending via TCP will not guarantee that every send will result in an identically-sized receive on the other side.
Your protocol seems to assume it will, because you're waiting for a read of exactly 11 bytes for the </sendFile>, whereas it could be received in multiple separate reads. E.g.: "[file data...]". If this happens, your code will not correctly finish.
It's also worth noting that the ASCII encoding is 7-bit, and so binary files (such as the MP4) will be received incorrectly (even if you fix the above). If it is binary data, you should not attempt to convert it to a string, but instead write it to file directly from the byte[].
If you wish to contine down this route (rather than using the many existing file transfer systems already available as mentioned in another answer) then you may also want to change your protocol so that instead of delimiting the file with <sendFile>...</sendFile>, you send the file's length initially, which will allow you to send files that may contain one of these special tags.
Sending files / directories over sockets is not trivial. I would recommend using some file transfer library (over sockets, or maybe higher level protocol, i.e. rsync, ftp, http, etc), rather than trying to code the thing from scratch.
After skimming over the code - try sending a large file containing some uniform content (filled with '0's or something). If it would pass through - your xml thing doesn't work.
Basically what you are doing is correct. The main point I could suggest for improvement is setting the MaxChunkSize to a larger value, as much as 65000. This will enable the socket code to handle any fragmentation, which will be much more efficient than any splitting you will do by yourself.
Also, you are aware to the fact that sending a large file should take some time. At a 100 Mbit LAN, the bandwidth would be (theoretically) 12.5 MByte per second. Thus sending a 700 MByte file will still take 56 seconds. Of course that the actual throughput depends and many factors in the network and the computers and network hardware involved, so it would be expected to be slower.
Finally, Wireshark (or any other sniffer program) is the most basic and invaluable tool in the networking programmer toolbox. Use it on both the server and the client to see how the TCP packets are transmitted, to see if you can identify any causes for the slow trasnfer.
First i am n00b in socket programming. So i decided to write simple data over lan tcp server
My server code that handles incomming data is
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
clientStream.ReadTimeout = 10;
int size = 4096 * 1000;
byte[] message = new byte[size];
byte[] All = new byte[0];
int bytesRead;
string error = "";
lock (this)
{
while (true)
{
All = new byte[0];
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, size);
All = AddBArrays(All, message, bytesRead);
}
catch
{
break;
}
if (bytesRead == 0)
{
break;
}
}
if (All.Length > 0)
{
Message m = (Message)Tools.ByteArrayToObject(All);
OnRecived(new RecivedArgs("localhost", (Message)Tools.ByteArrayToObject(All)));
}
}
tcpClient.Close();
}
}
byte[] AddBArrays(byte[] ar1, byte[] ar2, int read)
{
byte[] concat = new byte[ar1.Length + read];
if (ar1.Length != 0)
System.Buffer.BlockCopy(ar1, 0, concat, 0, ar1.Length);
System.Buffer.BlockCopy(ar2, 0, concat, ar1.Length, read);
return concat;
}
it works but have some issues. It fales receiving files bigger then 100 mbs or smthng and also if i send data very often interval < 800 then data is lost. how should i improve my code? The large file issue is not so important the primary issue is the data loss in fast data sending.
tnx for help
Ok i now updated the code by the suggestions
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
clientStream.ReadTimeout = 10;
int size = 4096 * 1000;
List<byte> Test = new List<byte>();
byte[] message = new byte[size];
byte[] All = new byte[0];
int bytesRead;
while (true)
{
//All = new byte[0];
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, size);
for (int i = 0; i < bytesRead; i++)
{
Test.Add(message[i]);
}
}
catch
{
break;
}
if (bytesRead == 0)
{
break;
}
}
if (Test.Count > 0)
{
Message m = (Message)Tools.ByteArrayToObject(Test.ToArray());
OnRecived(new RecivedArgs("localhost", m));
Test = new List<byte>();
}
}
tcpClient.Close();
}
but the issues still there
Edit--> large file issue fixed it was just a 'System.OutOfMemoryException' but it didn't throw a error.
The All byte array you should change to a List<byte>. You are creating instances like crazy right now. The GC is probably working a lot more than it needs to. This might be slowing it down so much that it can't keep up.
Not really related to sockets:
Make size a const
NEVER lock this. Create a private field that you can lock. In fact, I don't even think you need a lock here.
remove the error string.
OK i solved the problem.
I simple send to much data to fast so data loss was unavoidable.
My optimized code is
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
int size = 1;
byte[] message = new byte[1];
int bytesRead;
while (true)
{
bytesRead = 0;
if (clientStream.DataAvailable)
bytesRead = clientStream.Read(message, 0, 1);
if (bytesRead > 0)
{
OnRecived(new RecivedArgs("tick", null));
}
Thread.Sleep(1);
}
}
i have tested intervals as low as 1 ms and no data loss :)
thanks for your help
i have two Application to listen network Stream : Server.cs on the other hand; send file Client.cs. But i want to send more files on a stream from any folder. For example. i have C:/folder whish has got 3 jpg files. My client must run. Also My server.cs get files on stream:
Client.cs:
private void btn_send2_Click(object sender, EventArgs e)
{
string[] paths= null;
paths= System.IO.Directory.GetFiles(#"C:\folder" + #"\", "*.jpg", System.IO.SearchOption.AllDirectories);
byte[] Dizi;
TcpClient Gonder = new TcpClient("127.0.0.1", 51124);
FileStream Dosya;
FileInfo Dos;
NetworkStream Akis;
foreach (string path in paths)
{
Dosya = new FileStream(path , FileMode.OpenOrCreate);
Dos = new FileInfo(path );
Dizi = new byte[(int)Dos.Length];
Dosya.Read(Dizi, 0, (int)Dos.Length);
Akis = Gonder.GetStream();
Akis.Write(Dizi, 0, (int)Dosya.Length);
Gonder.Close();
Akis.Flush();
Dosya.Close();
}
}
Also i have Server.cs
void Dinle()
{
TcpListener server = null;
try
{
Int32 port = 51124;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[1024 * 250000];
// string ReceivedPath = "C:/recieved";
while (true)
{
MessageBox.Show("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
MessageBox.Show("Connected!");
NetworkStream stream = client.GetStream();
if (stream.CanRead)
{
saveFileDialog1.ShowDialog();
// burası degişecek
string pathfolder = saveFileDialog1.FileName;
StreamWriter yaz = new StreamWriter(pathfolder);
string satir;
StreamReader oku = new StreamReader(stream);
while ((satir = oku.ReadLine()) != null)
{
satir = satir + (char)13 + (char)10;
yaz.WriteLine(satir);
}
oku.Close();
yaz.Close();
client.Close();
}
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
Please look Client.cs: icollected all files from "c:\folder"
paths= System.IO.Directory.GetFiles(#"C:\folder" + #"\", "*.jpg", System.IO.SearchOption.AllDirectories);
My Server.cs how to get all files from stream?
It's not a typical way of doing things.
Usually you would write a single file in a single stream and repeat the process for multiple files. You risk data corruption this way as you'll have to place some kind of marker in the stream to know where to split it.
The easiest way is to use multiple sockets, one for commands and one (or more) for sending files.
The below code have NOT been tested, just wrote it up to show what I mean. It's multithreaded and can receive multiple files from the same client, just call sendfiles multiple times in the client.
You might want to add error/exception handling if you decide to finish the code.
Server.cs
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Test2
{
public class Server
{
private readonly TcpListener _listener = new TcpListener(1234);
public void Start()
{
_listener.BeginAcceptTcpClient(OnClient, null);
}
private void OnClient(IAsyncResult ar)
{
// End async accept and start wait for a new connection again
TcpClient client = _listener.EndAcceptTcpClient(ar);
_listener.BeginAcceptTcpClient(OnClient, null);
// Let's start receiving files from the accepted client.
var context = new Context {Client = client, Buffer = new byte[8196]};
client.GetStream().BeginRead(context.Buffer, 0, context.Buffer.Length, OnReceive, context);
}
/// <summary>
/// Got some stuff from a client
/// </summary>
/// <param name="ar"></param>
private void OnReceive(IAsyncResult ar)
{
// got a file command
var context = (Context) ar.AsyncState;
int bytesRead = context.Client.GetStream().EndRead(ar);
string cmd = Encoding.UTF8.GetString(context.Buffer, 0, bytesRead);
string[] parts = cmd.Split(';');
string command = parts[0];
// want to send another file
if (command == "sendfile")
{
// context info for receiving files
var client = new FileClient();
client.FileName = parts[1];
client.Size = long.Parse(parts[2]);
client.FileStream = new FileStream("C:\\" + client.FileName, FileMode.CreateNew, FileAccess.Write);
// startup listener where we are going to receive the file.
var listener = new TcpListener(IPAddress.Any, 0); // get a kernelassigned number
client.Listener = listener;
listener.Start();
listener.BeginAcceptTcpClient(OnFileSocket, client);
// send reply
var ep = (IPEndPoint) listener.LocalEndpoint;
byte[] reply = Encoding.UTF8.GetBytes(ep.Port.ToString());
context.Client.GetStream().Write(reply, 0, reply.Length);
}
}
// Receiving the actual files.
private void OnFileSocket(IAsyncResult ar)
{
var client = (FileClient) ar.AsyncState;
client.Socket = client.Listener.EndAcceptTcpClient(ar);
var buffer = new byte[8192];
client.Buffer = buffer;
client.Socket.GetStream().BeginRead(buffer, 0, buffer.Length, OnReceiveFile, client);
}
private void OnReceiveFile(IAsyncResult ar)
{
var client = (FileClient) ar.AsyncState;
int bytesRead = client.Socket.GetStream().EndRead(ar);
client.Received += bytesRead;
client.FileStream.Write(client.Buffer, 0, bytesRead);
// recieved complete file, disconnect and exit.
if (client.Received == client.Size)
{
client.FileStream.Close();
client.Socket.Close();
return;
}
client.Socket.GetStream().BeginRead(client.Buffer, 0, client.Buffer.Length, OnReceiveFile, client);
}
#region Nested type: Context
private class Context
{
public byte[] Buffer;
public TcpClient Client;
}
#endregion
#region Nested type: FileClient
private class FileClient
{
public byte[] Buffer;
public string FileName;
public FileStream FileStream;
public TcpListener Listener;
public long Received;
public long Size;
public TcpClient Socket;
}
#endregion
}
}
Client.cs
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Test2
{
internal class Client
{
private readonly IPAddress _server;
private readonly TcpClient _tcpClient = new TcpClient();
public Client(IPAddress server)
{
_server = server;
_tcpClient = new TcpClient();
}
public void Connect()
{
_tcpClient.Connect(new IPEndPoint(_server, 1234));
}
// asks server on which port the file should be sent.
private int RequestPort(string fileName, long length)
{
// lock tpcClient for each request, so we dont mix up the responses.
lock (_tcpClient)
{
// send request
byte[] bytes = Encoding.UTF8.GetBytes("sendfile;" + fileName + ";" + length);
_tcpClient.GetStream().Write(bytes, 0, bytes.Length);
// get reply
var buffer = new byte[1024];
int bytesRead = _tcpClient.GetStream().Read(buffer, 0, buffer.Length);
string reply = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// error message or port?
int port;
if (!int.TryParse(reply, out port))
throw new InvalidOperationException("Server sent an error:" + reply);
return port;
}
}
public void SendFiles(string[] fileNames)
{
// Use a buffer to not preserve memory (instead of reading whole file into memory)
var buffer = new byte[8192];
foreach (string fileName in fileNames)
{
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Send on commandchannel that we want to send a file.
int filePort = RequestPort(Path.GetFileName(fileName), fileStream.Length);
var client = new TcpClient(new IPEndPoint(_server, filePort));
NetworkStream stream = client.GetStream();
// repeat until there are no more bytes to read.
int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
stream.Write(buffer, 0, bytesRead);
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
}
stream.Close();
client.Close();
}
}
}
}
}