i am writing code for to transfer one file from one computer to another computer, in my case i am able to transfer file client and server in single system,but i am not able do for between two different systems.
Please help me to resolve this issue,
Thanks in advance.
What I have tried:
Server:
private void Form1_Load(object sender, EventArgs e)
{
nSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(ListenerThread));
thdListener.Start();
}
public void ListenerThread()
{
TcpListener tcpListener = new TcpListener(IPAddress.Parse(GetIp()),8080);
tcpListener.Start();
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
lb_Connections.Invoke((MethodInvoker)delegate
{
saveFileDialog_Server.ShowDialog();
txt_SaveFile_Location.Text = saveFileDialog_Server.FileName;
lb_Connections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");
});
lock (this)
{
nSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}
public void handlerThread()
{
Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
lock (this)
{
Stream fileStream = File.OpenWrite(txt_SaveFile_Location.Text);
while (true)
{
thisRead = networkStream.Read(dataByte, 0, blockSize);
fileStream.Write(dataByte, 0, thisRead);
if (thisRead == 0) break;
}
fileStream.Close();
}
lb_Connections.Invoke((MethodInvoker)delegate
{
lb_Connections.Items.Add("File Written");
});
handlerSocket = null;
}
public string GetIp()
{
string name = Dns.GetHostName();
IPHostEntry entry = Dns.GetHostEntry(name);
IPAddress[] addr = entry.AddressList;
if (addr[1].ToString().Split('.').Length == 4)
{
return addr[1].ToString();
}
return addr[2].ToString();
}
Client:
private void button1_Click(object sender, EventArgs e)
{
DialogResult dialogResult =folderBrowserDialog_openFiles.ShowDialog();
txt_OpenFiles.Text = folderBrowserDialog_openFiles.SelectedPath;
filepath = txt_OpenFiles.Text;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
saveFileDialog_zip.Filter = @"Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
DialogResult dr = saveFileDialog_zip.ShowDialog();
saveFile = saveFileDialog_zip.FileName;
fileName = saveFile;
ZipFile.CreateFromDirectory(filepath, fileName);
MessageBox.Show("Zip File Created at your Saved Location");
}
catch (Exception exception)
{
MessageBox.Show("Zip file not created");
}
}
private void btn_Browse_Zip_Click(object sender, EventArgs e)
{
DialogResult drbrowse_zip=openFileDialog_OpenZip.ShowDialog();
openFileDialog_OpenZip.Filter = "Zip Files (.zip) | *.zip | All Files(*.*) | *.*";
openFileDialog_OpenZip.FilterIndex = 1;
if (drbrowse_zip == System.Windows.Forms.DialogResult.OK)
{
txt_BrowseZip.Text = openFileDialog_OpenZip.FileName;
}
}
private void btn_Transfer_Click(object sender, EventArgs e)
{
Stream fileStream = File.OpenRead(txt_BrowseZip.Text);
byte[] fileBuffer = new byte[fileStream.Length];
fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
TcpClient clientSocket=new TcpClient(txt_Server.Text,8080);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
networkStream.Close();
}