Subject: File Transfer HELP*
I'm writting an application for file transfer in C# language (client send file to server)
and there are so many problems in my application.
anyone can help me with it please?
namespace CommunicationServer
{
public partial class Form1 : Form
{
private TcpListener tlsServer;
private Thread threadDownload;
private delegate void UpdateStatusCallback(string StatusMessage);
private delegate void UpdateProgressCallback(Int64 BytesRead, Int64 TotalBytes);
private static int PercentProgress;
private string CurrentPath;
private Bitmap MyImage;
public Form1()
{
InitializeComponent();
textBoxIP.Text = GetIP();
ProcessFolder();
listFileItems.SelectedIndexChanged +=
new EventHandler(OnlistFileItemSelected);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonStart_Click(object sender, EventArgs e)
{
threadDownload = new Thread(StartReceiving);
threadDownload.Start();
}
public void StartReceiving()
{
if (textBoxPort.Text == "")
{
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(portStr);
tlsServer = new TcpListener(IPAddress.Any, port);
tlsServer.Start();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Server starts listening." });
try
{
Byte[] bytes = new Byte[2048];
String data = null;
while (true)
{
TcpClient client = tlsServer.AcceptTcpClient();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Connected!" });
NetworkStream stream = client.GetStream();
int i;
data = null;
try
{
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Received: " + data });
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Sent: " + data });
}
}
catch (Exception ex)
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { ex.Message });
}
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "The file has been received. Closing streams." });
}
tlsServer.Stop();
}
private void UpdateStatus(string StatusMessage)
{
textBoxStatus.Text += StatusMessage;
}
private void updateControls(bool listening)
{
buttonStart.Enabled = !listening;
buttonStop.Enabled = listening; ;
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
if (TotalBytes > 0)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
}
}
protected void OnlistFileItemSelected(object Sender, EventArgs e)
{
string SelectedString = listFileItems.SelectedItem.ToString();
string FullPathName = Path.Combine(CurrentPath, SelectedString);
ProcessFile(FullPathName);
ShowImage(FullPathName, 196, 188);
}
public void ShowImage(string fileToDisplay, int xsize, int ysize)
{
if (MyImage != null)
{
MyImage.Dispose();
}
MyImage = new Bitmap(fileToDisplay);
imageBox.ClientSize = new Size(xsize, ysize);
imageBox.Image = (Image)MyImage;
}
protected void ProcessFolder()
{
try
{
DirectoryInfo TheFolder = new DirectoryInfo(@"D:\serverReceive");
if (TheFolder.Exists)
{
ListContentsOfFolder(TheFolder);
return;
}
TheFolder.Create();
ListContentsOfFolder(TheFolder);
}
finally { }
}
protected void ProcessFile(string PathName)
{
try
{
FileInfo currentFile = new FileInfo(PathName);
if (currentFile.Exists)
{
return;
}
throw new FileNotFoundException();
}
catch (FileNotFoundException)
{
textBoxDir.Text = PathName;
}
catch (Exception e)
{
textBoxDir.Text = PathName;
listFileItems.Items.Add("Error Processing");
listFileItems.Items.Add(e.Message);
}
}
protected void ListContentsOfFolder(DirectoryInfo Parent)
{
CurrentPath = Parent.FullName;
textBoxDir.Text = CurrentPath;
foreach (FileInfo NextFile in Parent.GetFiles())
{
if (NextFile.Extension == ".jpg" || NextFile.Extension == ".JPG" ||
NextFile.Extension == ".jpeg" || NextFile.Extension == ".JPEG" ||
NextFile.Extension == ".gif" || NextFile.Extension == ".GIF" ||
NextFile.Extension == ".png" || NextFile.Extension == ".PNG" ||
NextFile.Extension == ".bmp" || NextFile.Extension == ".BMP" ||
NextFile.Extension == ".tif" || NextFile.Extension == ".TIF" ||
NextFile.Extension == ".tiff" || NextFile.Extension == ".TIFF")
{
listFileItems.Items.Add(NextFile.Name);
}
}
}
String GetIP()
{
String strHostName = Dns.GetHostName();
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
String IPStr = "";
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}
private void buttonStop_Click(object sender, EventArgs e)
{
tlsServer.Stop();
textBoxStatus.Text = "Server stops listening";
}
}
}
and there are so many problems in my application.
anyone can help me with it please?
namespace CommunicationServer
{
public partial class Form1 : Form
{
private TcpListener tlsServer;
private Thread threadDownload;
private delegate void UpdateStatusCallback(string StatusMessage);
private delegate void UpdateProgressCallback(Int64 BytesRead, Int64 TotalBytes);
private static int PercentProgress;
private string CurrentPath;
private Bitmap MyImage;
public Form1()
{
InitializeComponent();
textBoxIP.Text = GetIP();
ProcessFolder();
listFileItems.SelectedIndexChanged +=
new EventHandler(OnlistFileItemSelected);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonStart_Click(object sender, EventArgs e)
{
threadDownload = new Thread(StartReceiving);
threadDownload.Start();
}
public void StartReceiving()
{
if (textBoxPort.Text == "")
{
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(portStr);
tlsServer = new TcpListener(IPAddress.Any, port);
tlsServer.Start();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Server starts listening." });
try
{
Byte[] bytes = new Byte[2048];
String data = null;
while (true)
{
TcpClient client = tlsServer.AcceptTcpClient();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Connected!" });
NetworkStream stream = client.GetStream();
int i;
data = null;
try
{
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Received: " + data });
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "Sent: " + data });
}
}
catch (Exception ex)
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { ex.Message });
}
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { "The file has been received. Closing streams." });
}
tlsServer.Stop();
}
private void UpdateStatus(string StatusMessage)
{
textBoxStatus.Text += StatusMessage;
}
private void updateControls(bool listening)
{
buttonStart.Enabled = !listening;
buttonStop.Enabled = listening; ;
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
if (TotalBytes > 0)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
}
}
protected void OnlistFileItemSelected(object Sender, EventArgs e)
{
string SelectedString = listFileItems.SelectedItem.ToString();
string FullPathName = Path.Combine(CurrentPath, SelectedString);
ProcessFile(FullPathName);
ShowImage(FullPathName, 196, 188);
}
public void ShowImage(string fileToDisplay, int xsize, int ysize)
{
if (MyImage != null)
{
MyImage.Dispose();
}
MyImage = new Bitmap(fileToDisplay);
imageBox.ClientSize = new Size(xsize, ysize);
imageBox.Image = (Image)MyImage;
}
protected void ProcessFolder()
{
try
{
DirectoryInfo TheFolder = new DirectoryInfo(@"D:\serverReceive");
if (TheFolder.Exists)
{
ListContentsOfFolder(TheFolder);
return;
}
TheFolder.Create();
ListContentsOfFolder(TheFolder);
}
finally { }
}
protected void ProcessFile(string PathName)
{
try
{
FileInfo currentFile = new FileInfo(PathName);
if (currentFile.Exists)
{
return;
}
throw new FileNotFoundException();
}
catch (FileNotFoundException)
{
textBoxDir.Text = PathName;
}
catch (Exception e)
{
textBoxDir.Text = PathName;
listFileItems.Items.Add("Error Processing");
listFileItems.Items.Add(e.Message);
}
}
protected void ListContentsOfFolder(DirectoryInfo Parent)
{
CurrentPath = Parent.FullName;
textBoxDir.Text = CurrentPath;
foreach (FileInfo NextFile in Parent.GetFiles())
{
if (NextFile.Extension == ".jpg" || NextFile.Extension == ".JPG" ||
NextFile.Extension == ".jpeg" || NextFile.Extension == ".JPEG" ||
NextFile.Extension == ".gif" || NextFile.Extension == ".GIF" ||
NextFile.Extension == ".png" || NextFile.Extension == ".PNG" ||
NextFile.Extension == ".bmp" || NextFile.Extension == ".BMP" ||
NextFile.Extension == ".tif" || NextFile.Extension == ".TIF" ||
NextFile.Extension == ".tiff" || NextFile.Extension == ".TIFF")
{
listFileItems.Items.Add(NextFile.Name);
}
}
}
String GetIP()
{
String strHostName = Dns.GetHostName();
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
String IPStr = "";
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}
private void buttonStop_Click(object sender, EventArgs e)
{
tlsServer.Stop();
textBoxStatus.Text = "Server stops listening";
}
}
}