SERVER:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace SocketfileServer
{
public partial class Form1 : Form
{
private Thread thrDownload;
private Stream strLocal;
private NetworkStream strRemote;
private TcpListener tlsServer;
private delegate void UpdateStatusCallback(string StatusMessage);
private delegate void UpdateProgressCallback(Int64 BytesRead, Int64 TotalBytes);
private static int PercentProgress;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
thrDownload = new Thread(StartReceiving);
thrDownload.Start();
}
private void StartReceiving()
{
try
{
string hstServer = Dns.GetHostName();
IPAddress ipaLocal = Dns.GetHostEntry(hstServer).AddressList[0];
if (tlsServer == null)
{
tlsServer = new TcpListener(ipaLocal, Convert.ToInt32(txtPort.Text));
}
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Starting the server...\r\n" });
tlsServer.Start();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has started. Please connect the client to " + ipaLocal.ToString() + "\r\n" });
TcpClient tclServer = tlsServer.AcceptTcpClient();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has accepted the client\r\n" });
strRemote = tclServer.GetStream();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has received the stream\r\n" });
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
string FileName = System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize);
strLocal = new FileStream(@"C:\" + FileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
long FileSize = Convert.ToInt64(System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize));
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Receiving file " + FileName + " (" + FileSize + " bytes)\r\n" });
downBuffer = new byte[2048];
while ((bytesSize = strRemote.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgressCallback(this.UpdateProgress), new object[] { strLocal.Length, FileSize });
}
}
finally
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The file was received. Closing streams.\r\n" });
strLocal.Close();
strRemote.Close();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Streams are now closed.\r\n" });
StartReceiving();
}
}
private void UpdateStatus(string StatusMessage)
{
txtLog.Text += StatusMessage;
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
if (TotalBytes > 0)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
strLocal.Close();
strRemote.Close();
txtLog.Text += "Streams are now closed.\r\n";
}
}
}
CLIENT:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace SocketfileClient
{
public partial class Form1 : Form
{
TcpClient tcpClient;
FileStream fstFile;
NetworkStream strRemote;
public Form1()
{
InitializeComponent();
}
private void ConnectToServer(string ServerIP, int ServerPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(ServerIP, ServerPort);
txtLog.Text += "Successfully connected to server\r\n";
}
catch (Exception exMessage)
{
txtLog.Text += exMessage.Message;
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Disconnected from server.\r\n";
}
private void btnSend_Click(object sender, EventArgs e)
{
if (tcpClient.Connected == false)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
if (openFile.ShowDialog() == DialogResult.OK)
{
txtLog.Text += "Sending file information\r\n";
strRemote = tcpClient.GetStream();
byte[] byteSend = new byte[tcpClient.ReceiveBufferSize];
fstFile = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader binFile = new BinaryReader(fstFile);
FileInfo fInfo = new FileInfo(openFile.FileName);
string FileName = fInfo.Name;
byte[] ByteFileName = new byte[2048];
ByteFileName = System.Text.Encoding.ASCII.GetBytes(FileName.ToCharArray());
strRemote.Write(ByteFileName, 0, ByteFileName.Length);
long FileSize = fInfo.Length;
byte[] ByteFileSize = new byte[2048];
ByteFileSize = System.Text.Encoding.ASCII.GetBytes(FileSize.ToString().ToCharArray());
strRemote.Write(ByteFileSize, 0, ByteFileSize.Length);
txtLog.Text += "Sending the file " + FileName + " (" + FileSize + " bytes)\r\n";
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strRemote.Write(downBuffer, 0, bytesSize);
}
txtLog.Text += "File sent. Closing streams and connections.\r\n";
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Streams and connections are now closed.\r\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
}
}